45

我试图让 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 中正确暂停?

4

3 回答 3

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;
于 2014-02-11T01:12:56.607 回答
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

希望这对你有帮助

于 2013-04-30T13:57:16.243 回答
-2
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = @{
    MPMediaItemPropertyTitle: title,
    MPMediaItemPropertyArtist: artist
};

像这样的电话

center setnowPlayingInfo = songInfo;
于 2013-10-01T10:30:40.140 回答