1

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.

Amro
  • 11
  • 1
  • 2
  • What you're describing is how video files work. Do you know of an app that behaves differently? – matt Oct 21 '19 at 23:47
  • I have seen apps that are able to stream (rather than download audio files) and will still show the lock screen controls when it is paused. For example, the iPhone Podcast app can stream episodes. I assumed that these apps use AVPlayer because AVAudioPlayer doesn't allow streaming. Does this make sense? – Amro Oct 22 '19 at 15:05
  • Those are audio files. I am asking you about video files. Have you seen an app that behaves the way you describe when streaming a video file? – matt Oct 22 '19 at 15:27
  • So right now I am streaming video files but only using the audio (I'm not creating an AVPlayerViewController and assigning its player). Are you saying if I use the same code but instead point to a pure audio file (eg mp3), then this problem with the lock screen controls will go away? – Amro Oct 22 '19 at 15:35
  • I'm not saying anything. I'm asking you a question. I ask for the third time. Can you give an example of _any_ app that streams a video file (I don't care what "part" of the file it plays) that does what you are saying your app should do? – matt Oct 22 '19 at 15:37
  • No I can't. I am trying to mimic the behavior of the Podcast app which can stream audio and show lock screen controls even when it is paused. I thought perhaps you were getting at the point that this is not working because I am using a video file rather than an audio file. – Amro Oct 22 '19 at 15:41
  • I _am_ getting at that point, in a sense. I believe there would not be any trouble getting the your app to behave like the Podcast app, _if_ you were doing what the Podcast app does. But you are not doing what the Podcast app does: you are streaming a video, even though you are just playing its audio track. So there is no comparison to be drawn. = – matt Oct 22 '19 at 16:27
  • Thanks Matt! Not sure how I missed that, I will give that a try – Amro Oct 22 '19 at 16:29

0 Answers0