MPNowPlayingInfoCenter not reacting properly when pausing playback

怎甘沉沦 提交于 2019-12-02 17:51:58

I've the solution! Set only the MPMediaItemPropertyPlaybackDuration

1 - When you start the track, set the property with the total duration of the track:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = @{
    MPMediaItemPropertyTitle: title,
    MPMediaItemPropertyArtist: artist
    MPMediaItemPropertyPlaybackDuration : [NSNumber numberWithFloat:length]
};
center.nowPlayingInfo = songInfo;

2 - when you pause the track... do nothing.

3 - when you play the track, set the property with the currentTrackTime:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];        
[playingInfo setObject:[NSNumber numberWithFloat:player.currentTrackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];            
center.nowPlayingInfo = playingInfo;

Here what you searching, this working very good add this to your Class:

- (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPause:nil];

                break;

            default: break;
        }
    }
}

you can add functions for forward or backward with adding other CASE code like this:

case UIEventSubtypeRemoteControlBeginSeekingBackward:[self yourBackward:nil];
                break;

and to calling play pause you need to create a action like this:

- (IBAction)playPause:(id)sender {

    if (yourPlayer.rate == 1.0){

        [yourPlayer pause];
    } else if (yourPlayer.rate == 0.0) {

        [yourPlayer play];
    }
}

Important: any case you adding need the IBAction

Hope this help you

abymathew
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = @{
    MPMediaItemPropertyTitle: title,
    MPMediaItemPropertyArtist: artist
};

the call like

center setnowPlayingInfo = songInfo;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!