我有一个 ios 应用程序,当它进入后台时会继续播放音乐。现在,如果有电话,无论是否接听,应用程序都不会继续播放音乐。两天来,我一直在这里阅读有关此问题的帖子。他们都没有解决我的问题。
我正在使用 AVQueuePlayer 对象,因为我也在需要时流式传输我的音乐。现在委托方法自 ios6 以来已被弃用。所以我正在使用通知。
令人惊奇的是,中断结束(电话结束)被通知,播放音乐的代码也被编写,但应用程序直到前台才播放音乐(有另一个通知)
这是我的代码
-(void)viewWillAppear
{.....
........
.....
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:UIApplicationDidBecomeActiveNotification object:nil]
}
-(void)audioInterruptionNotification:(NSNotification *) aNotification {
NSLog(@"Interrupt %@", aNotification);
NSDictionary *dict = [aNotification userInfo];
NSUInteger typeKey = [[dict objectForKey:@"AVAudioSessionInterruptionTypeKey"] unsignedIntegerValue]; NSLog(@"%d", typeKey);
if (typeKey == 0)
{
[avPlayer play];
NSLog(@"1.......");
}
else if (typeKey == 1)
{
[avPlayer play];
NSLog(@"3...............");
;
}
}
此外,我尝试通过调度队列来诱导延迟。委托方法似乎不起作用。但 gaana、saavn 和苹果官方音乐应用程序在通话后恢复。所以这是可能的。我似乎错过了什么。如果我使用核心电话。我将不得不添加一个完整的框架,这将增加应用程序的大小。如果这是唯一的方法。请告诉如何。
谢谢。我真的很感谢你的时间。