【问题标题】:MPNowPlayingInfoCenter not reacting properly when pausing playback暂停播放时 MPNowPlayingInfoCenter 没有正确反应
【发布时间】: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


【解决方案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;

【讨论】:

  • 不应该是 MPNowPlayingInfoPropertyElapsedPlaybackTime 而不是 MPNowPlayingInfoPropertyPlaybackRate 吗??
  • 不应该是 player.currentPlaybackTime 而不是 player.currentTrackTime 还是我错了?
  • 是的@Pedro,这取决于您的财产名称
  • 此解决方案在锁屏播放器上将经过时间设置为零,因此根本不起作用
【解决方案2】:

这里是你搜索的内容,这个工作非常好将它添加到你的类:

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

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

希望对你有帮助

【讨论】:

    【解决方案3】:
    MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
    NSDictionary *songInfo = @{
        MPMediaItemPropertyTitle: title,
        MPMediaItemPropertyArtist: artist
    };
    

    电话之类的

    center setnowPlayingInfo = songInfo;
    

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 1970-01-01
      • 2023-03-22
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多