当使用AVPlayer播放视频时,iOS会停止后台音乐

18
我正在使用AVPlayer播放视频,它会停止在背景中运行的iPhone音乐。请帮我解决这个问题。
let item1 = AVPlayerItem.init(URL: NSURL(string:path))
player = AVPlayer(playerItem: item1)
layer?.player = player;
player?.play()

音乐来自于另一个应用程序,比如音乐应用程序或其他什么吗? - siegy22
4个回答

56

我的电影是用于动画的,它们没有声音。为了让其他声音继续播放,在Swift中:

    // None of our movies should interrupt system music playback.
    _ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: .mixWithOthers)

感谢Marcus Adams提供原始答案。

11

在Swift 4.2中,请尝试这个。

do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: .mixWithOthers)
        try AVAudioSession.sharedInstance().setActive(true)
   } catch {
        print(error)
   }

这不是4.2版本,我猜测。由于AVAudioSessionCategoryPlayback,它看起来像是Swift 4.0或其他版本。 - Zaporozhchenko Oleksandr

11
使用允许混音的 AVAudioSession类别,例如AVAudioSessionCategoryPlayback,并添加AVAudioSessionCategoryOptionMixWithOthers
从文档中得知:
  • AVAudioSessionCategoryOptionMixWithOthers

    将此会话的音频与其他活动会话的音频混合。

    仅当会话类别为 AVAudioSessionCategoryPlayAndRecord或AVAudioSessionCategoryPlayback时才有效。 (如果会话类别是AVAudioSessionCategoryAmbient,则隐式设置。)

    如果您在使用此选项时激活了您的会话,则您应用的音频不会打断其他应用程序(例如Music应用程序)的音频。 如果不使用此选项(或隐含可混合的类别),激活您的会话将中断其他不可混合的会话。

    iOS 6.0及更高版本可用。


1
根据苹果公司的说法

https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/playing_audio_from_a_video_asset_in_the_background

如果您想继续播放音频,则在进入后台时将AVPlayer实例与演示文稿断开连接,并在返回前台时重新连接它。

func applicationDidEnterBackground(_ application: UIApplication) {

     // Disconnect the AVPlayer from the presentation when entering background

     // If presenting video with AVPlayerViewController
     playerViewController.player = nil

     // If presenting video with AVPlayerLayer
     playerLayer.player = nil
}

func applicationWillEnterForeground(_ application: UIApplication) {
     // Reconnect the AVPlayer to the presentation when returning to foreground

    // If presenting video with AVPlayerViewController
    playerViewController.player = player

    // If presenting video with AVPlayerLayer
    playerLayer.player = player
}

以您的ViewController为例,使用AVPlayer

步骤:1

在“功能”中启用背景模式,包括音频、AirPlay和画中画

步骤:2

AppDelegate.swift

do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
        print("Playback OK")
        try AVAudioSession.sharedInstance().setActive(true)
        print("Session is Active")
    } catch {
        print(error)
    }

步骤:3

YourViewcontroller.swift

override func viewDidLoad() {
    super.viewDidLoad()
    addPlayerNotifications()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    removePlayerNotifations()
}

 func addPlayerNotifications() {

    NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}

func removePlayerNotifations() {

    NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
    NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}

//App enter in forground.
@objc func applicationWillEnterForeground(_ notification: Notification)        {

    yourPlayerLayer.player = yourplayer

}

//App enter in forground.
@objc func applicationDidEnterBackground(_ notification: Notification) {

    yourPlayerLayer.player = nil

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接