1

I'm trying to get my podcast app to display content in the lock screen, as well as respect the play/pause controls. Here is my code:

- (void)playEpisodeAtIndex:(NSInteger)index {

    _currentIndex = index;
    TalkPodEpisode *episode = self.playlist[index];
    self.player = [[AVPlayer alloc] initWithURL:episode.url];
    [self.player play];
    _playing = YES;

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    NSDictionary *lockScreenInfo = @{MPMediaItemPropertyTitle:self.currentEpisode.title,
                                 MPMediaItemPropertyArtist: @"The Talk Pod",
                                 MPNowPlayingInfoPropertyPlaybackRate:[NSNumber numberWithDouble:self.player.rate],
                                          MPNowPlayingInfoPropertyElapsedPlaybackTime:[NSNumber numberWithDouble:[self currentPlaybackTime]]};
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo;


}

Weirdly enough, here's what happens:

  1. responding and handling events works just fine
  2. the artist and title show up on the lock screen
  3. the progress slider shows --:--, and no playback progress is shown
  4. the duration of the episode doesn't appear either.

Here is the code to get the current elapsed time:

- (NSTimeInterval)currentPlaybackTime {

    CMTime time = self.player.currentTime;
    if (CMTIME_IS_VALID(time)) {

        return time.value/time.timescale;

    }

    return 0;

}

Here is the code for toggling play & pause:

- (void)playPause {

    if (self.player) {

        if (_playing) {

            [self.player pause];
            _playing = NO;

        } else {

            [self.player play];
            _playing = YES;

        }

        NSMutableDictionary *lockScreenInfo = [[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo mutableCopy];
        lockScreenInfo[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithDouble:self.player.rate];
        lockScreenInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:[self currentPlaybackTime]];
        [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo;

    }

}

Anyone know how to fix this? what's confusing me is that Some of the stuff shows up on the screen, but the current playback information doesn't. Again, receiving and handling events works just fine.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
vsanthanam510
  • 360
  • 4
  • 14

1 Answers1

0

May be you need to set Playback Duration key in nowPlayingInfo dictionary. Look at my post for another question, it works fine in my app. Stack : Show seek track duration on control center

Community
  • 1
  • 1
Samir
  • 902
  • 9
  • 23