Closed DrewBourgeois closed 6 years ago
Hi @DrewBourgeois, I just released version 4.1.0 which adds the functionality you need. If you are using ASPVideoPlayer
set the configuration
property with allowBackgroundPlay: true
.
Thanks so much!
Hi @andreipitis, I'm trying to have the background audio of a video asset controllable through MPRemoteCommandCenter at the lock screen, but it doesn't seem to work as videoPlayerLayer.player = nil
when it enters the background. Do you have any tips on how to make this happen? Thanks!
Hi @DrewBourgeois,
The player
object needs to be removed from the videoPlayerLayer
in order to enable background playback. This does not deallocate the player, there is still a strong reference to it and all the functions to control playback should still work.
You should be able to control the playback with methods like playVideo()
, pauseVideo()
if you are using ASPVideoPlayerView
.
If you are using ASPVideoPlayer
you can control playback by accessing the videoPlayerControls
property:
//First method
let controls = videoPlayer.videoPlayerControls as? ASPVideoPlayerControls
controls?.pause()
//Second method
let player = videoPlayer.videoPlayerControls.videoPlayer
player?.pauseVideo()
Did you enable Background Modes for audio playback in Capabilities? I can only speculate without seeing the actual code.
Hi @andreipitis,
Thanks for that!
The command center controls don't control the playback when the app is in the background and videoPlayerLayer.player = nil
is in appDidEnterBackground. The command center controls work fine when the app is in the foreground, or videoPlayerLayer.player = nil
is taken out of appDidEnterBackground and the screen is locked.
The following is in ASPVideoPlayerView:
private func commonInit() {
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
self.pauseVideo()
return .success
}
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
self.playVideo()
return .success
}
}
My AVAudio session is set to AVAudioSessionCategoryPlayback and background audio capabilities are set.
Thanks!
@DrewBourgeois I tried your code and it seems to be working fine in my case. I can put the app in the background while continuing to play audio and I can pause/resume it from command center.
In my case, the MPRemoteCommandCenter
setup was done in a ViewController that had a reference to the player as to not modify the component code. Maybe this change can work for you as well.
UIApplication.shared.beginReceivingRemoteControlEvents()
is not necessary when using MPRemoteCommandCenter
according to the documentation, but if it still doesn't work, you could try moving it in AppDelegate
:
func applicationDidEnterBackground(_ application: UIApplication) {
UIApplication.shared.beginReceivingRemoteControlEvents()
}
Hi @andreipitis I got the code working the way I want.
Thanks so much for your help, and also for developing a really great video player!
Hi there,
How do I configure ASPVideoPlayer to allow audio of video assets to continue when the app enters the background?
Thanks!!! Drew.