我有一个使用AVAudioRecorder和AVAudioPlayer的应用程序。它记录和播放很好,但有时录音会在一分钟左右被切断。这并不是所有时候都会发生的,当它发生时,它仍然表现为它在录制(audioRecorderEncodeErrorDidOccur不会被调用,而audioRecorderDidFinishRecording会在用户停止录制时被调用)。但是声音文件本身在一分钟内就会被截断。
下面是我正在使用的编码(soundFileURL是appsdocdir/某某事物随机。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:17:23 t-iPhone mediaserverd39 : 10:17:23.451 <0x282f000> AudioConverterNew返回-50 5月10日10:17:23 t-iPhone mediaserverd39 : 10:17:23.453 <0x282f000> IO_ChangeIOFormat: error -50 5月10日10:17:23 t-iPhone mediaserverd39 : 10:17:23.463 <0x282f000> Queue_ChangeIOFormat: failed (-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];发布于 2013-05-10 22:48:11
我想通了。问题是电话要睡觉了。从睡觉的时候到从锁屏出来的时候,它都会剪掉任何声音。我修正了将UIBackgroundModes ->音频添加到info.plist文件中:
http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
https://stackoverflow.com/questions/16488176
复制相似问题