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

Jaa*_*nus 45 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;
Run Code Online (Sandbox Code Playgroud)

由于我正在播放,因此在开始播放时我没有持续时间信息.当我从流中获得"就绪"信号时,我会更新Apple TV上正确显示的持续时间:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];
[playingInfo setObject:[NSNumber numberWithFloat:length] forKey:MPMediaItemPropertyPlaybackDuration];
center.nowPlayingInfo = playingInfo;
Run Code Online (Sandbox Code Playgroud)

当用户寻找音轨时,我也可以使用这种技术:

[playingInfo setObject:[NSNumber numberWithFloat:length * targetProgress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
Run Code Online (Sandbox Code Playgroud)

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

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];        
[playingInfo setObject:[NSNumber numberWithFloat:0.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate];            
center.nowPlayingInfo = playingInfo;
Run Code Online (Sandbox Code Playgroud)

这不是暂停,而是将播放头追回到零并继续前进.

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

Dam*_*ito 15

我有解决方案!仅设置MPMediaItemPropertyPlaybackDuration

1 - 启动轨道时,使用轨道的总持续时间设置属性:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = @{
    MPMediaItemPropertyTitle: title,
    MPMediaItemPropertyArtist: artist
    MPMediaItemPropertyPlaybackDuration : [NSNumber numberWithFloat:length]
};
center.nowPlayingInfo = songInfo;
Run Code Online (Sandbox Code Playgroud)

2 - 当你暂停赛道时......什么也不做.

3 - 播放曲目时,使用currentTrackTime设置属性:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];        
[playingInfo setObject:[NSNumber numberWithFloat:player.currentTrackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];            
center.nowPlayingInfo = playingInfo;
Run Code Online (Sandbox Code Playgroud)


Bla*_*eep 3

这是您搜索的内容,这工作得很好,将其添加到您的课程中:

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

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPause:nil];

                break;

            default: break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过添加其他 CASE 代码来添加向前或向后的函数,如下所示:

case UIEventSubtypeRemoteControlBeginSeekingBackward:[self yourBackward:nil];
                break;
Run Code Online (Sandbox Code Playgroud)

要调用播放暂停,您需要创建如下操作:

- (IBAction)playPause:(id)sender {

    if (yourPlayer.rate == 1.0){

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

        [yourPlayer play];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

希望这对您有帮助