2014-12-29 171 views
0

我有問題,這樣的代碼:背景音樂的問題

-(void)playSong 
{ 
    // ... 
    NSLog(@"playing %i", currentSONG); 
    VKAudio* song = [audios objectAtIndex:currentSONG]; 
    if (player == nil) { // create player object when first called 
     player = [[AVPlayer alloc] initWithURL:[self urlForVKAudio:song]]; 
     [player play]; 
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; // turn on in background 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAVPlayerItemDidPlayToEndTimeNotification) name:AVPlayerItemDidPlayToEndTimeNotification object:nil]; 
    } 
    else { 
     [player replaceCurrentItemWithPlayerItem: 
     [[AVPlayerItem alloc] initWithURL:[self urlForVKAudio:song]]]; 
     [player play]; // <- this is not working when phone is blocked & screen is off 
    } 
} 

這裏的問題是,當我切換到BG模式,同時播放音樂在播放結束當前項目,但下一個項目不在播放,所以在後臺模式下方法

[player play]; 

什麼都沒做......我做錯了什麼? (bg音樂模式開啓)

回答

1

據我所見,你的AudioSession設置正確,但不要激活它。

這是我使用的是什麼:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive:YES error:nil]; 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
[self becomeFirstResponder]; 
+0

非常感謝!現在沒有時間檢查,但我會盡可能報告結果! – maxpovver