avaudiorecorder preparetorecord가 실패한 이유는 무엇입니까?
-
22-07-2019 - |
문제
사용자 오디오 입력 (음성)을 기록하는 기본 컨트롤러를 설정하려고합니다. 그러나 avaudiorecorder의 repiretorecord 메소드가 실패했으며 그 이유를 알 수 없습니다. 앱 대의원에서 오디오 세션을 설정했으며 avaudiorecorder 인스턴스를 인스턴스화 할 때 오류가 발생하지 않습니다.
// 앱 델리게이트 스 니펫
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");
}
// view는 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;
}
해결책
적절한 설정을 사용하여 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];
다른 팁
파일을 저장하려는 디렉토리가 존재하지 않으면 repiretorecord도 실패합니다 (오류없이). NSFILEMANAGER를 사용하여 디렉토리가 이미 존재하는지 확인하십시오.
제휴하지 않습니다 StackOverflow