2014-12-01 183 views
0

當我設置一個錄音機,只需使用一個按鈕來啓動/停止「記錄」功能調用,一切似乎都正常工作。但是,當我使用單獨的按鈕來啓動「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]; 
} 
} 
+1

不知道這是否是問題,反而造成其絕對是一個錯誤在主線程上循環。該委託可以提供一種名爲'audioRecorderDidFinishRecording:successfully:'的方法。嘗試等待完成並擺脫while循環。 – danh 2014-12-01 18:23:19

+0

danh,這解決了我的問題,謝謝! while ... isRecording ...循環是問題。有趣的是,我過去曾多次使用過這個沒有問題的東西。你建議的另一種方法就足夠了。如果你「回答」這個問題,我會檢查它,非常感謝。 – user3542888 2014-12-01 18:37:57

+0

很高興聽到。謝謝。我改變了猜測的答案。 – danh 2014-12-01 22:14:42

回答

0

似乎從更新其記錄狀態的主線程塊循環的AVAudioRecorder的某些方面(甚至入門)。通過取消循環,您可以將記錄器釋放以進行正常處理。

該委託提供了一種方法(audioRecorderDidFinishRecording:successfully:),用於表示錄製完成,因此放棄while循環並實現該方法。

0

如果你想連續記錄,可能會出現記錄異常,所以你應該記錄後一秒,我認爲,可以通過音頻緩衝

+0

我不太明白這一點,你能澄清一下,並說明需要更改海報的代碼。 – blm 2015-11-26 02:04:51

+0

我覺得你應該在文件處理完後立即記錄下來,這會影響下一個記錄,如果你只是記錄,這是有效的,所以我認爲它應該記錄在第三段開始時的第一個磁帶,你知道我的意思嗎,我還在嘗試,我的英語很差,請原諒我 – fzzzzy 2015-11-26 04:06:10

+0

也許一些設置轉碼影響了錄音。 – fzzzzy 2015-11-26 06:18:22