لماذا AVAudioRecorder prepareToRecord الفشل؟
-
22-07-2019 - |
سؤال
وأنا أحاول أن الإعداد وحدة تحكم الأساسية التي سوف يسجل إدخال الصوت المستخدم (صوت). ومع ذلك، طريقة prepareToRecord وAVAudioRecorder لهو الفشل، وأنا لا يمكن معرفة السبب. لدي الإعداد الدورة الصوت في بلدي مندوب التطبيق وأنا لا تتلقى أخطاء عند I مثيل المثال 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 DID LOAD في 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];
}
و// 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;
}
المحلول
ووفشل لأنك لم تهيئة كائن 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];
نصائح أخرى
وأيضا فشل وprepareToRecord (بصمت، من دون خطأ) إذا كان الدليل حيث كنت في محاولة لحفظ الملف غير موجود. استخدام NSFileManager للتحقق مما إذا كان الدليل موجودا مسبقا.
لا تنتمي إلى StackOverflow