暂停播放时,MPNowPlayingInfoCenter没有正确反应

我正在尝试让MPNowPlayingInfoCenter在暂停播放时正常工作。 (我有一个使用AVPlayer进行播放的stream媒体音乐应用程序,而我正在通过Airplay在Apple TV中播放。)除了暂停以外,其他所有内容似乎均已在Apple TV用户界面中正确反映出来。 我正在像这样初始化它:

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

由于我是stream媒体,我没有开始播放的持续时间信息。 当我从stream中获得“就绪”信号时,我更新Apple TV上正确显示的时间:

 MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo]; [playingInfo setObject:[NSNumber numberWithFloat:length] forKey:MPMediaItemPropertyPlaybackDuration]; center.nowPlayingInfo = playingInfo; 

当用户寻找曲目时,我也可以用这种技术来寻找:

 [playingInfo setObject:[NSNumber numberWithFloat:length * targetProgress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; 

我无法弄清的一件事是,如何暂停Apple TV上的播放头。 当用户在我的用户界面中点击暂停时,我正在尝试执行如下操作:

 MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo]; [playingInfo setObject:[NSNumber numberWithFloat:0.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate]; center.nowPlayingInfo = playingInfo; 

而不是暂停,这寻求播放头归零,并继续前进。

如何让播放头在我的Apple TV用户界面中正确暂停?

我有解决scheme! 只设置MPMediaItemPropertyPlaybackDuration

1 – 当您开始轨道时,请使用轨道的总持续时间来设置属性:

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

2 – 当你暂停轨道…什么也不做。

3 – 当您播放曲目时,请使用currentTrackTime设置属性:

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

在这里你search,这个工作很好添加到您的类:

 - (void)remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { if (receivedEvent.type == UIEventTypeRemoteControl) { switch (receivedEvent.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause:[self playPause:nil]; break; default: break; } } } 

您可以添加前向或后向function,添加其他CASE代码,如下所示:

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

并调用播放暂停,你需要创build一个这样的动作:

 - (IBAction)playPause:(id)sender { if (yourPlayer.rate == 1.0){ [yourPlayer pause]; } else if (yourPlayer.rate == 0.0) { [yourPlayer play]; } } 

重要:您添加的任何情况都需要IBAction

希望这对你有所帮助

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

这个电话就像

 center setnowPlayingInfo = songInfo; 

我从评论(大约一个月前)开始就尝试了我的build议,他们为我工作:

 [playingInfo setObject: [NSNumber numberWithFloat:0.000001f] forKey: MPNowPlayingInfoPropertyPlaybackRate]; 

(也就是慢慢移动播放头)。我添加了一个NSTimer ,在暂停时重置MPNowPlayingInfoPropertyElapsedPlaybackTime

对于以前没有使用过NSTimer ,请参阅如何使用NSTimer? 。