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

时间:2015-04-08 11:57:03

标签: ios swift avplayer

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

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

4 个答案:

答案 0 :(得分:27)

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

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

感谢Marcus Adams的原始答案。

答案 1 :(得分:8)

使用允许混合的category for the AVAudioSession,例如AVAudioSessionCategoryPlayback并添加AVAudioSessionCategoryOptionMixWithOthers

来自文档:

  
      
  • AVAudioSessionCategoryOptionMixWithOthers

         

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

         

    仅在会话类别为时有效   AVAudioSessionCategoryPlayAndRecord或AVAudioSessionCategoryPlayback。   (如果会话类别是AVAudioSessionCategoryAmbient,则隐含。)

         

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

         

    适用于iOS 6.0及更高版本。

  •   

答案 2 :(得分:2)

迅速4.2,请尝试使用此。

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

答案 3 :(得分:0)

根据苹果公司

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

}