2016-01-05 70 views
0

因此,我試圖更新MPNowPlayingInfoCenter在後臺和AVQueuePlayer中的軌道更改。如何更新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背景

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

感謝從未來電didFinish!

回答

0

使用志願:

[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]; 
+0

不行!當應用程序處於前臺,但當鎖定屏幕激活例如圖片/文本在軌道沒有改變時,它就會發生。 – Steeve17

+0

並且您已啓用音頻背景模式? –

+0

是的,謝謝它的工作,只是花了我一段時間才弄清楚。 – Steeve17