关闭AVPlayerViewController后如何恢复其他应用程序的音频播放? [英] How to resume audio playing of other apps after dismissing AVPlayerViewController?
问题描述
我使用 AVPlayerViewController
在我的应用中播放短视频.
I use AVPlayerViewController
to play short videos in my app.
如果有一个应用在用户在我的应用中播放视频之前在后台播放音频,我希望在关闭我的视频播放器后恢复另一个应用的背景音频播放.我目前使用 AVAudioSession.setActive(false,带有:.notifyOthersOnDeactivation)
来做到这一点.
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.
即使我调用 AVAudioSession.setActive(false,带有:.notifyOthersOnDeactivation)
后,Apple的Music应用程序和Podcasts应用程序仍可以继续播放-没有呼叫就不会继续,因此这意味着该呼叫确实具有效果-我测试过的第3方音乐或播客应用(Spotify,SoundCloud,Amazon Music,Overcast)都没有.
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.
这是我的代码:
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)
}
起初我以为可能是这些第三方应用程序的错.但是后来我用官方的Twitter应用程序对它们进行了测试,发现它们可以在Twitter应用程序中播放视频后恢复音频播放.
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.
那我想念什么?像Twitter应用程序一样,我应该怎么做才能使这些第三方应用程序在我的应用程序中播放视频后恢复音频播放?
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:这是完整的项目,因此任何有兴趣的人都可以尝试.
PS: here is the complete project so anyone interested can try it.
推荐答案
现在我可以确认它是一个错误:https://openradar.appspot.com/31733529 .而且此错误已在iOS 11中修复.
Now I can confirm it is a bug: https://openradar.appspot.com/31733529. And this bug is fixed in iOS 11.
这篇关于关闭AVPlayerViewController后如何恢复其他应用程序的音频播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!