暂停播放时,MPNowPlayingInfoCenter无法正常响应

时间:2012-08-01 03:58:23

标签: ios avplayer airplay mpnowplayinginfocenter

我正在尝试让MPNowPlayingInfoCenter在暂停播放时正常工作。 (我有一个使用AVPlayer播放的流媒体音乐应用程序,我正在通过Airplay播放我的Apple TV。)除了暂停之外的所有内容似乎都在Apple TV UI中正确反映出来。我正在这样初始化它:

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

由于我正在播放,因此在开始播放时我没有持续时间信息。当我从流中获得“就绪”信号时,我会更新在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上的播放头。当用户在我的UI中点击暂停时,我正在尝试执行以下操作:

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

而不是暂停,这使得播放头回到零并继续前进。

如何让播放头在Apple TV UI中正确暂停?

3 个答案:

答案 0 :(得分:15)

我有解决方案!仅设置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;

答案 1 :(得分:3)

在这里搜索,这个工作非常好,将此添加到您的班级:

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

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPause:nil];

                break;

            default: break;
        }
    }
}

您可以添加其他CASE代码来向前或向后添加功能:

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

要调用播放暂停,您需要创建一个这样的动作:

- (IBAction)playPause:(id)sender {

    if (yourPlayer.rate == 1.0){

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

        [yourPlayer play];
    }
}

重要提示:您添加的任何案例都需要IBAction

希望这能帮到你

答案 2 :(得分:-2)

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

这样的电话
center setnowPlayingInfo = songInfo;