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.
I doubt it's because none of these popular 3rd party apps supports background audio resuming. There must be something missing in my code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
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)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
}
@IBAction func play(_ sender: Any) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
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)
}
}
Even though someone on Stack Overflow thinks this code is fine and its these 3rd party apps' fault, I still believe there are something that can be done to make them resume audio playback because Twitter app can. But I don't know what Twitter does to achieve that, anyone knows?
PS: here is the complete project so anyone interested can try it.