Objective c recordForDuration会挂断,但不会停止录制

Objective c recordForDuration会挂断,但不会停止录制,objective-c,ios8,xcode6,avaudiorecorder,Objective C,Ios8,Xcode6,Avaudiorecorder,当我设置一个录音机,只需使用一个按钮来启动/停止录制功能调用时,一切似乎都很正常。但是,当我使用单独的按钮启动recordForDuration函数调用时。它似乎只是在继续录音,或者它认为自己在录音。无论哪种方式,它都不会在请求的持续时间后停止。调用下面的while循环只是不断地吐出记录。。。永远转到调试窗口。有什么想法吗?这段代码基本上是从我在xcode5中的一些工作代码中提取出来的,这些代码在使用ios7.1的iphone 5s上运行良好。代码挂起运行ios8.1的任何东西,也挂起8.1模拟

当我设置一个录音机,只需使用一个按钮来启动/停止录制功能调用时,一切似乎都很正常。但是,当我使用单独的按钮启动recordForDuration函数调用时。它似乎只是在继续录音,或者它认为自己在录音。无论哪种方式,它都不会在请求的持续时间后停止。调用下面的while循环只是不断地吐出记录。。。永远转到调试窗口。有什么想法吗?这段代码基本上是从我在xcode5中的一些工作代码中提取出来的,这些代码在使用ios7.1的iphone 5s上运行良好。代码挂起运行ios8.1的任何东西,也挂起8.1模拟器。请参阅下面的代码。谢谢你的建议

- (void)viewDidLoad {
[super viewDidLoad];
recordSwitchOn=NO;

//setup generic audio session
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];

NSLog(@"setupRecorder:");
inFileName = [NSString stringWithFormat:@"recordedData.caf"];//5
inFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:inFileName];
inFileURL = [NSURL fileURLWithPath:inFilePath];

recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:40000] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];


//    get audio file and put into a file ID
//    AudioFileID fileID; //in header

AudioFileOpenURL((__bridge CFURLRef)inFileURL, kAudioFileReadWritePermission, kAudioFileCAFType,&inputFileID);

//    initialize my audio recorders
inputRecorder = [[AVAudioRecorder alloc] initWithURL:inFileURL settings:recordSetting error:nil];
if (inputRecorder) {NSLog(@"inputRecorder is setup");
}else{NSLog(@"error setting up inputRecorder");}


}

- (IBAction)rec4durButt:(id)sender {
[statusLabel setText:@"Recording..."];
[inputRecorder setDelegate:self];
[inputRecorder recordForDuration:2.0];
while([inputRecorder isRecording]){
    NSLog(@"recording...");
}
[statusLabel setText:@"Recording stopped"];
}


- (IBAction)recButt:(id)sender {
if(recordSwitchOn==NO){
    [statusLabel setText:@"Recording..."];
    recordSwitchOn=YES;
    [inputRecorder setDelegate:self];
    //    [inputRecorder recordForDuration:(NSTimeInterval)0.35];
    [inputRecorder record];
}else{
    [statusLabel setText:@"Recording stopped"];
    recordSwitchOn=NO;
    [inputRecorder stop];
}
}

似乎主线程上的循环会阻止AVAudioRecorder的某些方面更新其录制状态,甚至阻止其开始。通过移除循环,您可以释放录音机以进行正常处理


代理提供了一个方法audioRecorderDidFinishRecording:successfully:表示录制完成,因此放弃while循环并实现该方法。

如果您想要连续录制,可能会发生录制异常,因此我认为您应该在录制后一秒钟,可能是由音频缓存引起的

不确定这是否是问题所在,但这样在主线程上循环肯定是错误的。委托可以提供名为audioRecorderDidFinishRecording的方法:成功:。尝试等待完成,并摆脱while循环。danh,这解决了我的问题,谢谢!当…正在记录。。。这就是问题所在。有趣的是,我在过去使用过很多次,都没有问题。你建议的另一种方法就足够了。如果你回答这个问题,我会核对的,非常感谢。很高兴听到。谢谢我把猜测改成了答案。我真的不明白这一点,你能澄清一下,并说明海报代码需要做哪些更改。我认为你应该在处理完文件后立即录制,这将影响下一次录制,如果你只是录制,这是有效的,所以我认为应该在第三段第一盘磁带开始时录制,你知道我的意思吗,我还在努力,我的英语很差,请原谅我可能一些设置转码影响了录制。因为我在转码方法AVAudioSession中重写了,这会导致录制时间的问题