2013-05-10 93 views
0

我有一個使用AVAudioRecorder和AVAudioPlayer的應用程序。它錄製並播放得很好,但有時錄製會在一分鐘左右切斷。這不會一直髮生,當它發生時,它仍會像錄製一樣(audioRecorderEncodeErrorDidOccur不會被調用,audioRecorderDidFinishRecording會在用戶停止錄製時執行)。聲音文件本身雖然在一分鐘內被截斷。AVAudioRecorder切斷錄音

這裏是我使用的編碼(被appsdocdir/somethingrandom.m4a的文件和文件名得到正確創建soundFileURL):

NSDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey]; 
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; 
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; 

NSError *error = nil; 
self->audioRecorder = nil; 
audioRecorder = [[AVAudioRecorder alloc] 
       initWithURL:soundFileURL 
       settings:recordSetting 
       error:&error]; 

if (error) 
{ 
    NSLog(@"error: %@", [error localizedDescription]); 

} else { 
    audioRecorder.delegate = self; 
    [audioRecorder prepareToRecord]; 
    audioRecorder.meteringEnabled = YES; 
} 

我認爲它的編碼問題,因爲如果我看設備日誌我看到這一點:

5月10日10時17分23秒叔iPhone mediaserverd [39]:10:17:23.451 < 0x282f000> AudioConverterNew返回-50

五月10十時17分23秒叔iPhone mediaserverd [39]:10:17:23.453 < 0x282f000> IO_ChangeIOFormat:錯誤-50

5月10日十時17分23秒叔iPhone mediaserverd [39]:10:17 :23.463 < 0x282f000> Queue_ChangeIOFormat:失敗(-50)

編輯:有人問是什麼soundFileURL。我在上面簡單地提到過,但這裏是實際的代碼。記錄後文件和文件名全都看起來正確:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"MMMMdyyyyA"]; 
NSString *recordDate = [formatter stringFromDate:[NSDate date]]; 

filename = [[NSString alloc] initWithFormat:@"%d-%@.m4a", idNumber, recordDate]; 
NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(
               NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
NSString *soundFilePath = [docsDir 
          stringByAppendingPathComponent:filename]; 
soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
+0

你的設備上進行測試? – matt 2013-05-10 18:05:46

+0

你作爲soundFileURL擁有什麼? – 2013-05-10 18:10:20

+0

亞光:是的。在iPhone上測試5 – tharris 2013-05-10 19:38:26

回答