【发布时间】:2012-07-29 22:38:15
【问题描述】:
我正在尝试让 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 中正确暂停?
【问题讨论】:
-
您是否只是尝试暂停您的 AVPlayer?
-
@RomanTemchenko 是的,当用户在设备上调用暂停 UI 控件时,除了信息中心实验之外,我还会执行
[player pause];其中player是 AVPlayer 实例。它在 Apple TV UI 中无效。音频实际上按预期停止并恢复,但播放进度存在我描述的问题。 -
我永远无法让它正确暂停,因此我将持续时间、速率和位置设置为零。这样做的效果是完全移除了一段时间内的进度条,这对我的目的来说已经足够了。
-
播放速率为 0 不是“速率”。我不确定它是否会起作用,但是您是否尝试过类似
[playingInfo setObject:[NSNumber numberWithFloat:0.000001f] forKey:MPNowPlayingInfoPropertyPlaybackRate];的方法? -
(也就是说,缓慢移动播放头。)如果有效,您可以使用 NSTimer 在 MPNowPlayingInfoPropertyElapsedPlaybackTime 暂停时定期重置它。
标签: ios avplayer airplay mpnowplayinginfocenter