【问题标题】:How to update MPNowPlayingInfoCenter when in the background and the track in AVQueuePlayer changes如何在后台更新 MPNowPlayingInfoCenter 并且 AVQueuePlayer 中的轨道发生变化
【发布时间】:2016-01-05 14:13:47
【问题描述】:

所以我试图在后台更新 MPNowPlayingInfoCenter 并且 AVQueuePlayer 中的轨道发生变化。

在前台很容易在

- (id) init{
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

  player = [[AVQueuePlayer alloc]init];
  player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
}

-(void)didFinish{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  // Magic goes here
  //[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying]; 
  [player advanceToNextItem];
  [player play];
}

在后台但是没有 AVPlayerActionAtItemEndAdvance 它不会转到下一个,并且 AVPlayerActionAtItemEndAdvance 永远不会在后台调用 didFinish

- (id) init{
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

  player = [[AVQueuePlayer alloc]init];
  player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
}

-(void)didFinish{ //never called
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  // Magic goes here
  //[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlaying]; 
  //play is not needed in this case
}

谢谢!

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    使用 KVO:

    [self.queuePlayer addObserver:self forKeyPath:@"currentItem" options:NSKeyValueObservingOptionNew context:PlayerItemKVOContext];
    

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
        if (context == PlayerItemKVOContext) {
            AVPlayerItem *playerItem = [_queuePlayer currentItem];
    

    【讨论】:

    • 不起作用!好吧,当应用程序在前台时,例如当锁屏激活时,图片/文本不会在轨道发生变化时发生变化。
    • 您是否启用了音频背景模式?
    • 是的,谢谢它的工作,我花了一段时间才弄清楚。
    • 我所有的代码,完整的工作示例在这里,如果有人需要看一下(有点乱但有效)github.com/etiennea/phonegap-plugin-mediaplayer
    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多