我在後臺使用AVAudioPlayer播放音樂。問題是:如果有傳入的呼叫中斷播放器,它將永遠不會恢復,除非切換到前景並手動執行。如何在後臺中斷後恢復AVAudioPlayer
代碼很簡單,在後臺播放:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
url = [[NSURL alloc] initFileURLWithPath:...];
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.delegate = self;
bool ret = [audio_player play];
委託來處理中斷:
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
//tried this, not working [[AVAudioSession sharedInstance] setActive: NO error: nil];
NSLog(@"-- interrupted --");
}
//----------- THIS PART NOT WORKING WHEN RUNNING IN BACKGROUND ----------
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
NSLog(@"resume!");
//--- tried, not working: [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
//--- tried, not working: [[AVAudioSession sharedInstance] setActive: YES error: nil];
//--- tried, not working: [audio_player prepareToPlay];
[audio_player play];
}
任何一個能幫助我嗎?
同一個pb,你找到了解決方案嗎? – fvisticot 2011-12-31 17:26:34
到目前爲止,沒有。但有一個解決方案。示例蘋果商店應用程序已經做到了。 – Paul 2012-01-09 22:13:04