我正在尝试设置一个记录用户音频输入(语音)的基本控制器。但是,AVAudioRecorder的prepareToRecord方法失败了,我无法弄清楚原因。我在我的app委托中设置了音频会话,当我实例化AVAudioRecorder实例时,我没有收到错误:
// App delegate snippet
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
NSError* audioSessionError = nil;
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord
error: &audioSessionError];
if (audioSessionError) {
NSLog (@"Error setting audio category: %@", [audioSessionError localizedDescription]);
} else {
NSLog(@"No session errors for setting category");
}
[audioSession setActive:YES error:&audioSessionError];
if (audioSessionError) {
NSLog (@"Error activating audio session: %@", [audioSessionError localizedDescription]);
} else {
NSLog(@"no session errors for setActive");
}
//在RECORDERCONTROLLER中查看DID LOAD
- (void)viewDidLoad {
self.navigationItem.title = [NSString stringWithFormat:@"%@", [[MyAppDelegate loadApplicationPlist] valueForKey:@"recorderViewTitle"]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(dismiss)];
[self alertIfNoAudioInput];
[self createAVAudioRecorder];
minutesSecondsFormatter = [[SimpleMinutesSecondsFormatter alloc] init];
currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self selector:@selector(updateAudioDisplay)
userInfo:NULL repeats:YES];
[super viewDidLoad];
}
// CREATE AVAUDIORECORDER
- (NSError *)createAVAudioRecorder {
NSError *recorderSetupError = nil;
[audioRecorder release];
audioRecorder = nil;
NSString *timestamp = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]];
NSString *destinationString = [[MyAppDelegate getAppDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf", timestamp]];
NSLog(@"destinationString: %@", destinationString);
NSURL *destinationUrl = [NSURL fileURLWithPath: destinationString];
audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl
settings:[[AVRecordSettings sharedInstance] getSettings]
error:&recorderSetupError];
if (recorderSetupError) {
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle:@"Can't record"
message:[recorderSetupError localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release];
return recorderSetupError;
} else {
NSLog(@"no av setup error");
}
if ([audioRecorder prepareToRecord]) {
recordPauseButton.enabled = YES;
audioRecorder.delegate = self;
} else {
NSLog(@"couldn't prepare to record");
}
NSLog (@"recorderSetupError: %@", recorderSetupError);
return recorderSetupError;
}
答案 0 :(得分:23)
如果您尝试保存文件的目录不存在,prepareToRecord也会失败(无提示,没有错误)。使用NSFileManager检查目录是否已存在。
答案 1 :(得分:5)
失败是因为您没有使用适当的设置初始化AVAudioRecorder对象。在初始化之前执行此操作:
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
然后你可以使用
实例化它audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl
settings:recordSettings
error:&recorderSetupError];