2013-05-10 88 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

回答