我有一个使用 AVAudioRecorder 和 AVAudioPlayer 的应用程序。它可以很好地录制和播放,但有时录制会在一分钟左右被切断。这不会一直发生,当它发生时,它仍然像正在录制一样(audioRecorderEncodeErrorDidOccur 不会被调用,而 audioRecorderDidFinishRecording 在用户停止录制时会调用)。声音文件本身,虽然在一分钟内被截断。
这是我正在使用的编码(soundFileURL 是 appsdocdir/somethingrandom.m4a 文件和文件名被正确创建):
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 t-iPhone mediaserverd[39] : 10:17:23.451 <0x282f000> AudioConverterNew 返回 -50
5 月 10 日 10:17:23 t-iPhone mediaserverd[39] : 10:17:23.453 <0x282f000> IO_ChangeIOFormat: 错误 -50
5 月 10 日 10:17:23 t-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];