当使用AVPLayer播放视频时,它会停止背景音乐ios



我正在使用AVPlayer播放视频,它会停止iPhone在后台播放的音乐。请帮我解决

let item1 = AVPlayerItem.init(URL: NSURL(string:path))
player = AVPlayer(playerItem: item1)
layer?.player = player;
player?.play()

我的电影是为动画制作的;它们没有声音。要让其他声音继续播放,请在Swift中:

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

感谢Marcus Adams的原始答案。

为AVAudioSession使用一个允许混合的类别,例如AVAudioSessionCategoryPlayback和添加AVAudioSessionCategoryOptionMixWithOthers

来自文档:

  • AVAudioSession类别选项与其他混合

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

    仅当会话类别为AVAudioSessionCategoryPlayAndRecord或AVAudioSsessionCategoryPlayback。(如果会话类别为AVAudioSessionCategoryAmbient,则为隐式。)

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

    在iOS 6.0及更高版本中可用。

在swift 4.2中,请尝试这个。

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

根据苹果

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
}

例如,对于使用AVPlayer 的ViewController

步骤: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
}

最新更新