暂停播放时MPNowPlayingInfoCenter不能正确反应(MPNowPlayingInfo

2019-07-30 17:12发布

我试图让MPNowPlayingInfoCenter正常工作暂停播放时。 (我有一个使用AVPlayer播放的流媒体音乐应用程序,我在我的苹果电视上的Airplay播放。)拍卖,但是在暂停似乎在Apple TV UI正确反映。 我初始化它是这样的:

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

因为我流,我没有在开始播放时长信息。 当我从流“准备就绪”的信号,我更新正确显示了我的苹果电视上的时间:

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正确暂停?

Answer 1:

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


Answer 2:

在这里,你搜索一下,这方面的工作非常好给你的类添加:

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

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:[self playPause:nil];

                break;

            default: break;
        }
    }
}

您可以向前或向后添加功能与添加另一种情况下这样的代码:

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为

希望这有助于您



Answer 3:

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

呼叫像

center setnowPlayingInfo = songInfo;


文章来源: MPNowPlayingInfoCenter not reacting properly when pausing playback