當我設置一個錄音機,只需使用一個按鈕來啓動/停止「記錄」功能調用,一切似乎都正常工作。但是,當我使用單獨的按鈕來啓動「recordForDuration」函數調用。它似乎只是繼續錄製,或者也許它認爲它的錄製。無論哪種方式,它都不會在請求的持續時間後停止。調用之下的while循環只是不停地向調試窗口吐出「記錄...」。有任何想法嗎?這段代碼基本上是從我在xcode5中使用ios7.1在iPhone 5s上正常運行的一些工作代碼中提取的。該代碼會掛上運行ios8.1的任何東西(也會在8.1模擬器中掛起)。看到我的代碼如下。感謝您的任何建議。recordForDuration掛斷,不停止錄音
- (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];
}
}
不知道這是否是問題,反而造成其絕對是一個錯誤在主線程上循環。該委託可以提供一種名爲'audioRecorderDidFinishRecording:successfully:'的方法。嘗試等待完成並擺脫while循環。 – danh 2014-12-01 18:23:19
danh,這解決了我的問題,謝謝! while ... isRecording ...循環是問題。有趣的是,我過去曾多次使用過這個沒有問題的東西。你建議的另一種方法就足夠了。如果你「回答」這個問題,我會檢查它,非常感謝。 – user3542888 2014-12-01 18:37:57
很高興聽到。謝謝。我改變了猜測的答案。 – danh 2014-12-01 22:14:42