我的iPad声音应用程序(iOS 7.1)能够在后台录制.只要录制在后台不中断,一切都可以.例如,如果一个人有(愚蠢的?)想法在录制内容时开始听音乐.
我试图以不同的方式管理这种中断,但没有成功.问题是,
- (void)audioRecorderEndInterruption:(AVAudioPlayer *)p withOptions:(NSUInteger)flags
当应用程序在后台发生中断时,永远不会被触发.然后我尝试在另一个解决方案中实现AVAudioSessionInterruptionNotification和handleInterruption:方法as
- (void) viewDidAppear:(BOOL)animated { ...... AVAudioSession *session=[AVAudioSession sharedInstance]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:session]; ....... } // interruption handling - (void)handleInterruption:(NSNotification *)notification { try { UInt8 theInterruptionType = [[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] intValue]; NSLog(@"Session interrupted > --- %s ---\n", theInterruptionType == AVAudioSessionInterruptionTypeBegan ? "Begin Interruption" : "End Interruption"); .... MANAGE interruption begin interruptionBeganWhileInBackgroundMode = TRUE; } if (theInterruptionType == AVAudioSessionInterruptionTypeEnded) { .... MANAGE interruption end } } catch (CAXException e) { char buf[256]; fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf)); } }
在这种情况下,handleInterruption:在中断开始时被触发,但不是在中断结束时(在中断类型设置为AVAudioSessionInterruptionTypeEnded时触发)
为了解决这个问题,我决定设置一个标志(interruptionBeganWhileInBackgroundMode),当中断开始时,应用程序被告知前进时发生了中断.这样我就可以管理中断的结束.
它可能看起来很聪明,但事实并非如此!这就是为什么......
我试过两个实现.
解决方案1.在handleInterruption中设置[记录器暂停]:当中断开始时,并在设置中断标志时在comingForeground:方法中管理[记录器记录].在这种情况下,当应用程序返回到前台时,记录立即进行,但是不是恢复,记录器擦除文件并重新开始新的记录,以便在中断之前记录的所有内容都丢失.
解决方案2.在handleInterruption中放置[记录器停止]:当中断开始时,保存文件,以确保保留记录的数据.保存此记录,但是当应用程序即将到来时,它会停止大约十秒钟,然后用户才能再次进行交互,就好像有一个进程(文件保存?)使UI保持冻结状态.
最后一点:我在此应用程序的iPhone版本中遇到了完全相同的问题:当应用程序处于前台并且发生电话呼叫时,一切正常.但是当我的应用程序处于后台时发生电话呼叫时,我会看到与iPad版本相同的不良行为.
我注意到iOS7 中的Apple的语音备忘录应用程序正确地管理这些后台中断(它停止并保存录制),尽管它在前景时显示文件长度为00:00:00.该的iTalk应用程序管理它完美,未来的前景时自动恢复录制.
是否有人为背景录制应用找到了音频中断管理的解决方法?我发现很多人在很多开发者网站上都在寻找,但没有回答......谢谢!