MPNowPlayingInfoCenter - elapsed time keeps counting when audio paused
Asked Answered
G

3

16

I'm currently trying to figure out how to specify the elapsed time in the MPNowPlayingInfoCenter, on iOS.

When I start playing, I set the elapsed time to 0 and the playback rate to 1. This works fine.

Then I pause the audio. This is correctly detected by the MPNowPlayingInfoCenter, and it pauses the elapsed time on the interfaces.

It's only when I resume playing that things go wrong: the time is displayed as if it kept playing while paused. Example:

1. Start playback
2. Let it play for 10 seconds
3. Pause for 5 seconds
4. Resume playback

At this point, the actual time in the track is 10 seconds. Yet the info center displays 15.

I tried to set the playback rate to 0 while paused, but this results in a weird behavior: the displayed time randomly changes to a lower value.

Also, I don't really have an opportunity to update the elapsed time before resuming the song, as I only get a chance to do so after I receive the play event.

tl;dr: How to handle pauses in the MPNowPlayingInfoCenter and its time feature?

Godin answered 5/12, 2013 at 16:30 Comment(1)
Got here looking for a solution of the same problem on macOS. As of 11.1 I'm seeing the same problem randomly occur but just wanted to note that it happens with Safari too (playing Youtube videos). So in this case it seems like a bug in the OS.Inconvincible
G
15

Well, actually setting the rate to 0, and resetting the time to its actual value when I pause and play did the trick.

Godin answered 6/12, 2013 at 8:38 Comment(4)
how do u set the rate and time ?Naphthalene
Through the nowPlayingInfo dictionary of MPNowPlayingInfoCenter, on the attribute MPNowPlayingInfoPropertyPlaybackRate. See for details: developer.apple.com/library/ios/documentation/mediaplayer/…Godin
When I set the MPNowPlayingInfoPropertyPlaybackRate to 0, the elapsed time indicator on the lock screen goes back to zero. It seems to work fine if I set the MPNowPlayingInfoPropertyElapsedPlaybackTime to the current elapsed time when pausing and unpausing (in addition to setting the playback rate to 0 and 1, respectively), but the documentation says that updating this property frequently is not necessary. It seems like there should be a better way to do this.Reames
you said in your question that "I tried to set the playback rate to 0 while paused, but this results in a weird behavior: the displayed time randomly changes to a lower value". But you are saying the same in answer also. Can you please explain?Bullivant
M
1

Well, I have put a selector for MPMoviePlayerPlaybackStateDidChangeNotification notification. It is called whenever movie is play, paused, slide up/down infocenter (control center). Or even if the movie is streaming and it do pause/play on receive response.

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieStateChange)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification 

object:nil];

And I do

- (void)movieStateChange{
    [self updateControlCenter];
}

And thats my updateControlCenter, it takes already initlaized playing info and update the MPNowPlayingInfoPropertyElapsedPlaybackTime key.

- (void)updateControlCenter{
    NSMutableDictionary *nowPlayingInfo = [center.nowPlayingInfo mutableCopy];
    [nowPlayingInfo setObject:[NSNumber numberWithDouble:self.player.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    center.nowPlayingInfo = nowPlayingInfo;
}

Good luck!

Minus answered 7/8, 2014 at 10:17 Comment(0)
H
0

I had this problem and solved it by updating the elapsed time every time you play a track. So your 'play' button should update the information

Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
    NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
    [songInfo setObject:currentTrack.trackTitle forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:currentTrack.artist forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:currentTrack.albumTitle forKey:MPMediaItemPropertyAlbumTitle];
    [songInfo setObject:currentTrack.trackLength forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:currentTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];}
Henceforth answered 7/3, 2015 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.