我正在制作录音机应用程序,它运行得非常好。
但我遇到了中断问题。来电时,
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
然后调用此方法并暂停录制。
如果用户拒绝来电:
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder
然后在这里,我想要从中断的位置恢复录制。 但是当我再次调用记录方法时,记录以新文件开始。
答案 0 :(得分:7)
问题解决了!
我重写了录制代码以避免此问题。我使用了AudioQueues,基本上后端代码与SpeakHere应用程序相同,但有一些细微的变化。另外还有两个api:
-(void)resume
{
AudioQueueStart(queueObject, NULL);
}
-(void)pause
{
AudioQueuePause(queueObject);
}
在AudioRecorder类中。基本目的是避免在记录方法中完成录制设置。
设置中断回调,然后在回调中使用此暂停并适当地恢复方法。另外,请注意根据您的应用程序是否需要来激活音频会话。
希望这可以帮助那些人。
编辑:
音频中断监听器回调:
void interruptionListenerCallback (void *inUserData, UInt32 interruptionState)
{
if (interruptionState == kAudioSessionBeginInterruption)
{
[self.audioRecorder pause];
}
else if (interruptionState == kAudioSessionEndInterruption)
{
// if the interruption was removed, and the app had been recording, resume recording
[self.audioRecorder resume];
}
}
听取音频中断:
AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
答案 1 :(得分:2)
操作系统可能在中断期间停止AVAudioRecorder。您可以将两个或多个文件作为单个记录呈现给用户,也可以使用AudioQueue编写代码来处理中断(以及将音频数据保存到文件中)。
答案 2 :(得分:0)
我前一段时间试图这样做,并得出结论,它无法正常完成;操作系统本身不会让你这样做。也许这在3.1.2中有所改变,但是当我在3.0中尝试时它就行不通了。
答案 3 :(得分:0)
要处理中断,您必须使用AVAudioSessionDelegate方法而不是AVAudioRecorderDelegate方法。这是处理中断的代码示例:
/*=================================================================
Interruption Handling Method during Recording
==================================================================*/
- (void) beginInterruption
{
if(self.recorder)
{
//Method to handle UI
}
}
- (void) endInterruption
{
NSError *err = noErr;
[[AVAudioSession sharedInstance] setActive: YES error: &err];
if(err != noErr)
{
NSLog([err description]);
}
//method to handle UI
}
第一种方法会自动停用音频会话。因此,在第二种方法中,您必须重新激活音频会话。第一种方法将暂停录制,当中断结束时,您可以使用第二种方法恢复。我在版本3.0上试过这个上层版。