0

I have an iOS app built in React Native that plays video content using the react-native-video package and allows the use of AirPlay. When the app enters the background (pressing the home button, locking the screen or opening the swipe down menu) the video is paused and you have to resume play from the control center or lock screen media controls. I understand this is the expected behaviour of AVPlayer and I am trying to implement the following solution from the Apple documentation:

Remove/Restore the AVPlayerLayer and its associated AVPlayer

The code snippets provided are in Swift and I cannot seem to find a complete example of achieving this in my AppDelegate.m file using Objective-C.

I found the following snippet but I am unsure about the steps for getting my player view.

/* Remove the AVPlayerLayer from its associated AVPlayer
    once the app is in the background. */
- (void)applicationDidEnterBackground:(UIApplication *)application {
  MyPlayerLayerView *playerView = <#Get your player view#>;
  [[playerView playerLayer] setPlayer:nil]; // remove the player
}

/* Restore the AVPlayer when the app is active again. */
- (void)applicationDidBecomeActive:(UIApplication *)application {
  MyPlayerLayerView *playerView = <#Get your player view#>;
  [[playerView playerLayer] setPlayer:_player]; // restore the player
}
tombottles
  • 11
  • 3
  • No it isn't expected behaviour of AVPlayer especially if you playing through AirPlay. Double check you code, may be you are pausing player manually by entering in background. Also you need check is `AVAudioSession` is setup with `playback` category, also check is background mode for audio and airplay is turned on. – Cy-4AH May 27 '21 at 12:03
  • @Cy-4AH perhaps I have misunderstood the Apple documentation where it states "if the AVPlayer's current item is displaying video on the device's display, playback of the AVPlayer is automatically paused when the app is sent to the background." – tombottles May 27 '21 at 13:29
  • @Cy-4AH I have set background mode for "Audio, AirPlay, and Picture in Picture" and "App plays audio or streams audio/video using AirPlay" in Xcode and also added the following in AppDelegate.m [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; – tombottles May 27 '21 at 13:35
  • Do you have setup `MPRemoteCommandCenter.sharedCommandCenter.togglePlayPauseCommand` command? – Cy-4AH May 28 '21 at 08:56
  • @Cy-4AH no, I haven't used that command in my code. – tombottles May 28 '21 at 10:41

0 Answers0