gpt4 book ai didi

iphone - 为什么 AVAudioRecorderprepareToRecord 失败?

转载 作者:行者123 更新时间:2023-12-03 19:26:15 26 4
gpt4 key购买 nike

我正在尝试设置一个基本 Controller 来记录用户音频输入(语音)。但是,AVAudioRecorder 的prepareToRecord 方法失​​败了,我不明白为什么。我已在应用程序委托(delegate)中设置了 Audio Session ,并且在实例化 AVAudioRecorder 实例时没有收到错误:

//应用程序委托(delegate)片段

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

- (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];
}

//创建 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;
}

最佳答案

如果您尝试保存文件的目录不存在,则prepareToRecord也会失败(静默地,没有错误)。使用 NSFileManager 检查目录是否已经存在。

关于iphone - 为什么 AVAudioRecorderprepareToRecord 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com