我有iOS应用程序处理音频中断:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AudioInterrupt:)
name:AVAudioSessionInterruptionNotification
object: NULL];
并在AudioInterrupt:
- (void)AudioInterrupt:(NSNotification*)notification
{
NSDictionary *interuptionDict = notification.userInfo;
// get the AVAudioSessionInterruptionTypeKey enum from the dictionary
NSInteger interuptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
NSNumber* seccondReason = [[notification userInfo] objectForKey:@"AVAudioSessionInterruptionOptionKey"] ;
// decide what to do based on interruption type here...
switch (interuptionType) {
case AVAudioSessionInterruptionTypeBegan:
[[[self pureAudioHandler] audioController] setActive: NO];
NSLog(@"Interruption started");
break;
case AVAudioSessionInterruptionTypeEnded:
NSLog(@"Interruption ended");
break;
}
switch ([seccondReason integerValue]) {
case AVAudioSessionInterruptionOptionShouldResume:
NSLog(@"Resume Audio");
[[[self pureAudioHandler] audioController] configurePlaybackWithSampleRate:44100 numberChannels:2 inputEnabled:NO mixingEnabled:YES];
[[[self pureAudioHandler] audioController] setActive: YES];
break;
default:
break;
}
}
这适用于闹钟和Siri。然而,如果我没有互联网连接,我按下主页按钮,我得到#S; Siri不可用......"。触发AVAudioSessionInterruptionTypeBegan。按两次home键以返回应用程序,也不会触发AVAudioSessionInterruptionTypeEnded或AVAudioSessionInterruptionOptionShouldResume。任何解决方法?
iPad mini视网膜,7.0.3
答案 0 :(得分:2)
许多实验表明,中断不会像他们应该的那样一直被触发。在进入Siri时有时会丢失InterruptionTypeBegan,并且所有这些在当前测试用例(Unity3D& Kalimba(libpd))上随机发生。
而是使用applicationWillResignActive来杀死音频,而使用applicationDidBecomeActive再次启动它,因为它们在100%的时间都在工作。
有趣的事实是,当从Siri返回时(没有wifi,所以“Siri不可用......”出现),它会在一段时间后将采样率更改为机器本机采样率的一半(ipad mini上的24000)。