简体   繁体   English

AVAudioRecorder:无法记录其他记录应用程序

[英]AVAudioRecorder: can not record after other record apps

I know this is a difficult question, because I sometimes see it, sometimes not. 我知道这是一个很难的问题,因为我有时会看到它,有时候看不到。 I use AVAudioRecorder to record audio in my application. 我使用AVAudioRecorder在我的应用程序中录制音频。 It can run fine. 它可以正常运行。 But sometimes when I navigate to other apps and use microphone function of those apps, and back to my app, my record functionality can not record, all the record files have length is 0s. 但有时当我导航到其他应用程序并使用这些应用程序的麦克风功能,并返回到我的应用程序时,我的记录功能无法记录,所有记录文件的长度都是0。

After I debug, I found out that: [recorder prepareToRecord] return TRUE (this method to create record file - for example .caf file) [recorder record] always return FALSE (this is the problem) 调试后,我发现:[recorder prepareToRecord]返回TRUE(此方法创建记录文件 - 例如.caf文件)[记录器记录]总是返回FALSE(这是问题)

I don't know why [recorder record] return FALSE. 我不知道为什么[记录器记录]返回FALSE。 Do anyone see this problem before and can tell me how to fix this? 有没有人之前看到这个问题,可以告诉我如何解决这个问题?

Thank you very much. 非常感谢你。

当您的应用程序变为活动状态时,您需要激活AVAudioSession

Once you loose recording, you must RESTART the audio recording again and you HAVE to be in the foreground to do this. 松开录音后,您必须再次重新录制音频录音,并且必须在前台执行此操作。 You can capture audio interruption events for interrupted and interrupted ended. 您可以捕获中断和中断结束的音频中断事件。 BUT there is currently bugs in this and you many not get the ended events. 但是目前存在缺陷,你很多人都没有得到结束的事件。

 AVCaptureSession            *captureSession;
 .......
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionEnded) name:AVCaptureSessionInterruptionEndedNotification object:captureSession];
 [captureSession addObserver: self forKeyPath:@"running" options:0x3 context:nil];
 [captureSession addObserver: self forKeyPath:@"interrupted" options:0x3 context:nil];

There are two ways to capture audio. 有两种方法可以捕获音频。 Using the higher level AVCapture Session, or the Lower level AudioQueue, but both are restricted by the first paragraph. 使用更高级别的AVCapture会话或更低级别的AudioQueue,但两者都受第一段的限制。

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
    if ([keyPath isEqualToString:@"interrupted"]) 
    { 
          // try restart here if in foreground
          // post local notification if in background to bring to foreground and then restart
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM