I am streaming a video file but only playing the audio for it using AVPlayer. I am successfully able to play/pause, continue playing while in background, and interact with the lock screen controls to control the player. The only issue is if I pause the audio then I lock the phone, the lock screen controls do now show up. However, if I pause the audio from the lock screen, the controls remain showing.
The codebase is extensive so here is some of the relevant code.
-(void)viewDidLoad {
...
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] setMode:AVAudioSessionModeMoviePlayback error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
...
}
-(void) setUpAvPlayer {
...
self.player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:mixComposition]];
self.player.automaticallyWaitsToMinimizeStalling = NO;
[self addObservers];
[self setUpRemoteCommandCenter];
[self.player.currentItem seekToTime:CMTimeMakeWithSeconds(videoSeekPosition, 60000)];
...
}
-(void)setUpRemoteCommandCenter {
// Provides all audio data to be displayed to user in lock screen
self.lockScreenInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
lecture.topicName, MPMediaItemPropertyTitle,
[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.duration)], MPMediaItemPropertyPlaybackDuration,
[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.currentTime)], MPNowPlayingInfoPropertyElapsedPlaybackTime,
[NSNumber numberWithDouble:playerRate], MPNowPlayingInfoPropertyPlaybackRate, nil];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:self.lockScreenInfo];
// For lock screen & remote audio controls
MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[remoteCommandCenter.playCommand setEnabled:YES];
[remoteCommandCenter.playCommand addTarget:self action:@selector(playPauseVideo)];
[remoteCommandCenter.pauseCommand setEnabled:YES];
[remoteCommandCenter.pauseCommand addTarget:self action:@selector(playPauseVideo)];
[remoteCommandCenter.skipBackwardCommand setEnabled:YES];
[remoteCommandCenter.skipBackwardCommand addTarget:self action:@selector(skipBackward)];
remoteCommandCenter.skipBackwardCommand.preferredIntervals = @[@(15)];
[remoteCommandCenter.skipForwardCommand setEnabled:YES];
[remoteCommandCenter.skipForwardCommand addTarget:self action:@selector(skipForward)];
remoteCommandCenter.skipForwardCommand.preferredIntervals = @[@(15)];
// Drag slider to change audio position
// Check for iOS version here (later than iOS 9.0)
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
[remoteCommandCenter.changePlaybackPositionCommand setEnabled:YES];
[remoteCommandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
}
}
Update: I took Matt's advice in the comments and am now using an mp3 file for the audio. I am still having the same problem where when the audio is paused, lock screen controls do not show up. Any other ideas as to how I can fix this? As I said in the comments, I basically want to mimic Apple's Podcast app. Audio files are streamed (rather than downloaded) and lock screen controls show up even when it is paused.