简体   繁体   English

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

[英]When Video plays using AVPLayer, it stops background music ios

I am playing video using AVPlayer , it stops iPhone's music which is on going in background. 我正在使用AVPlayer播放视频,它会阻止iPhone的背景音乐。 Please help me to resolve 请帮我解决

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

My movies are for animations; 我的电影是为了动画; they have no sound. 他们没有声音。 To let other sound continue playing, in Swift: 为了让其他声音继续播放,在Swift:

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

Thanks to Marcus Adams for the original answer. 感谢Marcus Adams的原始答案。

Use a category for the AVAudioSession that allows mixing, such as AVAudioSessionCategoryPlayback and add AVAudioSessionCategoryOptionMixWithOthers . 使用允许混合的AVAudioSession类别 ,例如AVAudioSessionCategoryPlayback和添加AVAudioSessionCategoryOptionMixWithOthers

From the docs: 来自文档:

  • AVAudioSessionCategoryOptionMixWithOthers AVAudioSessionCategoryOptionMixWithOthers

    Mixes audio from this session with audio from other active sessions. 将此会话中的音频与来自其他活动会话的音频混合。

    Valid only if the session category is AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryPlayback. 仅在会话类别为AVAudioSessionCategoryPlayAndRecord或AVAudioSessionCategoryPlayback时有效。 (Implicit if the session category is AVAudioSessionCategoryAmbient.) (如果会话类别是AVAudioSessionCategoryAmbient,则隐含。)

    If you activate your session while using this option, your app's audio will not interrupt audio from other apps (such as the Music app). 如果您在使用此选项时激活会话,则应用的音频不会中断来自其他应用(例如音乐应用)的音频。 If not using this option (or a category that is implicitly mixable), activating your session will interrupt other nonmixable sessions. 如果不使用此选项(或可隐式混合的类别),则激活会话将中断其他不可混合的会话。

    Available in iOS 6.0 and later. 适用于iOS 6.0及更高版本。

In swift 4.2 Please try this one. 在快速4.2请尝试这一个。

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

According to Apple 根据Apple的说法

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 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

If you want to continue playing audio, you disconnect the AVPlayer instance from the presentation when entering the background and reconnect it when returning to the foreground. 如果要继续播放音频,请在进入后台时断开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
}

Eg for your ViewController using AVPlayer 例如,使用AVPlayer的ViewController

Step : 1 步骤1

Enable Background mode In Capabilities for Audio, AirPlay, and Pictures in pictures 启用背景模式用于图片中的音频,AirPlay和图片功能

Step : 2 第2步

AppDelegate.swift 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)
    }

Step : 3 步骤:3

YourViewcontroller.swift 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM