How to resume audio playing of other apps after dismissing AVPlayerViewController?

放肆的年华 提交于 2021-02-18 18:09:13

问题


I use AVPlayerViewController to play short videos in my app.

If there is an app playing audio in background before user plays a video in my app, I want the background audio playing of the other app to resume after my video player is dismissed. I currently use AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation) to do that.

Even though Apple's Music app and Podcasts app do resume playing after I call AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation)—won't resume without the call so it means this call does have effect—none of the 3rd party music or podcast apps that I tested(Spotify, SoundCloud, Amazon Music, Overcast) do.

Here is my code:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // I know it's not the best place to call setActive nor it covers all the cases. It's just a convenient place to put the code to test its effect after dismissing the video player.
    do {
        try AVAudioSession.sharedInstance().setActive(false, with: .notifyOthersOnDeactivation)
    } catch {
        print(error)
    }
}

@IBAction func play(_ sender: Any) {
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print(error)
    }

    let playerController = AVPlayerViewController()
    playerController.player = AVPlayer(url: URL(string: "http://gslb.miaopai.com/stream/UkjiD45ddxZFQ79I2bLaGg__.mp4")!)
    playerController.player?.play()
    present(playerController, animated: true)
}

At first I thought it might be these 3rd party apps' fault. But then I tested them with the official Twitter app and found they can resume audio playback after video playing in Twitter app.

So what did I miss? What should I do to make these 3rd party apps resume audio playback after video playing in my app, just as Twitter app does?

PS: here is the complete project so anyone interested can try it.


回答1:


I want the background audio playing of the other app to resume after my video player is dismissed

You need to accept that that isn't up to you. All you can do is manage your audio session properly. Whether some other app "takes the hint" and resumes its audio is not in your control.

However, you are not quite managing your audio session properly. You should:

  • Use a noninterfering category in general (such as Ambient).

  • Change to your Playback category and activate it only when you need it (i.e. only when we are actually just about to play)

  • When you are done playing, deactivate with notifyOthersOnDeactivation and then switch back to a non-interfering audio category (such as Ambient) and activate it.

Once you've done those things, you've done all you can do. Some apps will resume, others won't.




回答2:


Now I can confirm it is a bug: https://openradar.appspot.com/31733529. And this bug is fixed in iOS 11.




回答3:


You need to implement the AVAudioSession Notifications. . For each interruption or when your app is done with playing the media file, it will fire the notification and from there you can resume the already paused media file from device libraries.



来源:https://stackoverflow.com/questions/42407925/how-to-resume-audio-playing-of-other-apps-after-dismissing-avplayerviewcontrolle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!