我的iPhone应用程序使用“AVAudioRecorder”进行录音。它还使用“UIImagePickerController”记录电影和“MPMoviePlayerController”播放电影。AVAudioRecorder不会记录IF电影以前录制和播放
一切工作正常,直到我做三件事成一排:
- 录制电影中使用的UIImagePickerController
- 播放使用的MPMoviePlayerController
- 尝试拨打语音使用记录AVAudioRecorder 录制的动画
当我在步骤3中调用AVAudioRecorder的“记录”方法时,它返回NO指示失败,但没有提示为什么(来自Appl e!)AVAudioRecorder的audioRecorderEncodeErrorDidOccur委托方法永远不会被调用,并且在设置录像机时我不会收到其他错误。
我的第一个猜测是电影录制/播放正在修改“AVAudioSession”的共享实例,使得它阻止了录音机的工作。但是,我手动将AVAudioSession的category属性设置为“AVAudioSessionCategoryRecord”,并且在尝试录制之前使音频会话处于活动状态。
这里是我创造的记录方法:
- (void)createAudioRecorder
{
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
if (error)
...
[audioSession setActive:YES error:&error];
if (error)
...
NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
// General Audio Format Settings
[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
// Encoder Settings
[settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithInt:96] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitDepthHintKey];
// Write the audio to a temporary file
NSURL *tempURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"Recording.m4a"]];
audioRecorder = [[AVAudioRecorder alloc] initWithURL:tempURL settings:settings error:&error];
if (error)
...
audioRecorder.delegate = self;
if ([audioRecorder prepareToRecord] == NO)
NSLog(@"Recorder fails to prepare!");
[settings release];
}
,这里是我的方法开始录音:
- (void)startRecording
{
if (!audioRecorder)
[self createAudioRecorder];
NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error)
...
BOOL recording = [audioRecorder record];
if (!recording)
NSLog(@"Recording won't start!");
}
有没有人碰到这个问题?
谢谢,我遇到了同样的问题,无法弄清楚发生错误的时间! – 2011-08-27 16:51:55
谢谢,它有帮助。 – 2013-02-14 07:02:27