我有一个使用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;
}
我认为这是一个编码问题,因为如果我查看设备日志,我会看到:
编辑:有人问到soundFileURL是什么。我在上面简要提到过,但这是实际的代码。录制后文件和文件名都看起来正确: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)
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 :(得分:0)
我明白了。问题是电话要睡觉了。它会切断从睡眠到锁定屏幕时的任何音频。我通过添加UIBackgroundModes修复 - >音频到info.plist文件: