MPNowPlayingInfoCenter在暂停播放时未能正确响应

46

我正在尝试在暂停播放时使MPNowPlayingInfoCenter正常工作。(我有一个使用AVPlayer进行播放的流媒体音乐应用程序,并且我正在通过Airplay在我的Apple TV上进行播放。)除了暂停之外,似乎一切都能正确地反映在Apple TV的用户界面上。我是这样初始化它的:

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中点击暂停时,我正在尝试做类似以下的事情:


The one thing I can NOT figure out is, how to pause the playhead on my Apple TV. When user taps pause in my UI, I am trying to do something like:
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];        
[playingInfo setObject:[NSNumber numberWithFloat:0.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate];            
center.nowPlayingInfo = playingInfo;

与其暂停,这将播放头寻回到零并继续推进。

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


你尝试过暂停你的AVPlayer吗? - Roman Temchenko
@RomanTemchenko 是的,当用户在设备上调用暂停 UI 控件时,除了 infocenter 实验之外,我还会执行 [player pause];,其中 player 是一个 AVPlayer 实例。它在 Apple TV UI 中没有影响。音频实际上停止并按预期恢复播放,但是播放进度存在我描述的问题。 - Jaanus
我从未能够正确地暂停它,因此我将持续时间、速率和位置设置为零。这样做的效果是完全删除了时间进度条,这对我的目的来说已经足够好了。 - David Morton
播放速率为0不是“速率”。我不确定它是否有效,但你是否尝试过类似[playingInfo setObject:[NSNumber numberWithFloat:0.000001f] forKey:MPNowPlayingInfoPropertyPlaybackRate];的东西? - Aaron Brager
(那就把播放头移动得非常缓慢。)如果它能正常工作,你可以使用NSTimer在暂停时定期重置MPNowPlayingInfoPropertyElapsedPlaybackTime。 - Aaron Brager
3个回答

15

我有解决方案!只需设置MPMediaItemPropertyPlaybackDuration属性

1- 开始播放音轨时,使用该属性设置音轨的总持续时间:

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

2 - 当您暂停音轨时...不要做任何事情。

3 - 当您播放音轨时,使用当前音轨时间设置该属性:

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

1
应该使用 MPNowPlayingInfoPropertyElapsedPlaybackTime 而不是 MPNowPlayingInfoPropertyPlaybackRate 吗? - Clement Prem
应该是 player.currentPlaybackTime 而不是 _player.currentTrackTime_,我错了吗? - Pedro Lorente
是的 @Pedro,这取决于你属性的名称。 - Damien Romito
此解决方案在锁屏播放器上将经过时间设置为零,因此根本无法工作。 - hamsternik

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];
    }
}

重要提示:无论您添加什么内容,都需要IBAcion。
希望这能帮到您。

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

这个调用类似于

center setnowPlayingInfo = songInfo;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接