小编Pau*_*aul的帖子

如何在后台中断后恢复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];
Run Code Online (Sandbox Code Playgroud)

委托处理中断:

-(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 …
Run Code Online (Sandbox Code Playgroud)

iphone audio background multitasking ios

7
推荐指数
2
解决办法
7099
查看次数

标签 统计

audio ×1

background ×1

ios ×1

iphone ×1

multitasking ×1