2

So I am working on an app that plays a live video and audio stream using AVPlayer and AVPlayerLayer. I want to allow the audio to play in the background when the user leaves the app or locks the device, and so far it is working pretty well.

My code for this in AppDelegate.m is basically as follows:

-(void)applicationDidEnterBackground:(UIApplication *)application {
    [playerView setPlayer:nil];
}
-(void)applicationDidBecomeActive:(UIApplication *)application {
    [playerView setPlayer:streamPlayer];
}

Where playerView is my AVPlayerLayer and streamPlayer is my AVPlayer.

The problem I'm having is that when the app becomes active again, the audio skips and it takes about a second for the stream to stabilize and playback normally again. What I'm trying to achieve is for the audio to play in the background, then when the app becomes active again, there is no interruption in the audio and the video becomes active again and syncs to the audio.

On applicationDidBecomeActive I've tried creating another instance of AVPlayer connected to the same stream, but with audio muted, and then setting that player as the player for playerView, and that allows the audio from the original AVPlayer to not be interrupted, but then the video and audio are out of sync since they are from different AVPlayers, so this will not work.

Does anyone have any suggestions how to solve this issue?

spacesloth
  • 33
  • 3

0 Answers0