当我设置录音机并只使用按钮开始/停止"记录"函数调用,一切似乎都运行正常。但是,当我使用一个单独的按钮来启动" recordForDuration"功能调用。它似乎只是继续录制或者它认为它的录音。无论哪种方式,它都不会在请求的持续时间后停止。通话下方的while循环只是继续吐出"记录......"永远到调试窗口。有任何想法吗?这个代码基本上是从xcode5中的一些工作代码中提取出来的,它在iphone7.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];
}
}
答案 0 :(得分:0)
似乎循环主线程阻止了AVAudioRecorder的某些方面更新其录制状态(甚至开始)。通过移除循环,您可以释放录像机以进行正常处理。
委托提供了一个方法(audioRecorderDidFinishRecording:successfully:
),表示记录完成,因此删除while循环并实现它。
答案 1 :(得分:0)
如果你想连续录制,可能会发生录制异常,所以你应该在录制后一秒钟,我想,可能是由音频缓存造成的