暂停播放时,MPNowPlayingInfoCenter无法正常反应 [英] MPNowPlayingInfoCenter not reacting properly when pausing playback

查看:167
本文介绍了暂停播放时,MPNowPlayingInfoCenter无法正常反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让MPNowPlayingInfoCenter在暂停播放时正常工作。 (我有一个使用AVPlayer进行播放的流式音乐应用程序,我正在通过Airplay在我的Apple TV上播放。)除了暂停之外的所有内容似乎都在Apple TV UI中正确反映出来。我正在这样初始化它:

I am trying to get MPNowPlayingInfoCenter to work properly when pausing playback. (I have a streaming music app that uses AVPlayer for playback, and I am playing back in my Apple TV over Airplay.) Everything but pausing seems to be reflected correctly in the Apple TV UI. I am initializing it like this:

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

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

Since I am streaming, I do not have duration info upon starting the playback. When I get "ready" signal from the stream, I update the duration that shows up correctly on my Apple TV:

MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo];
[playingInfo setObject:[NSNumber numberWithFloat:length] forKey:MPMediaItemPropertyPlaybackDuration];
center.nowPlayingInfo = playingInfo;

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

I can also seek with this technique when the user seeks the track:

[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;

这不是暂停,而是将播放头恢复为零并继续推进它。

Instead of pausing, this seeks the playhead back to zero and keeps advancing it.

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

How do I get the playhead to pause correctly in my Apple TV UI?

推荐答案

我有解决方案!仅设置MPMediaItemPropertyPlaybackDuration

I've the solution! Set only the MPMediaItemPropertyPlaybackDuration

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

1 - When you start the track, set the property with the total duration of the track:

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

2 - 当您暂停音轨时......什么都不做。

2 - when you pause the track... do nothing.

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

3 - when you play the track, set the property with the currentTrackTime:

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

这篇关于暂停播放时,MPNowPlayingInfoCenter无法正常反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆