iOS 9 中的 AVAudioRecorder 不工作

AVAudioRecorder in iOS 9 Not working

我正在使用 AVAudioRecorder 录制音频文件。我使用的代码在 iOS 8 及以下版本中运行良好,但自从 iOS 9 的最新更新以来,记录似乎已停止工作。

我尝试记录 AVAudioRecorder 对象的属性,甚至在 AVAudioRecorder 中调用 "record" 函数后 isRecording 显示为 NO 并且当 "stop" 函数被调用我在委托中得到回调 - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag; 成功标志为 NO

audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
    NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
    return;
}
[audioSession setActive:YES error:&err];

recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];
[recordSetting setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
[recordSetting setValue :[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];

recorderFilePath = [NSString stringWithFormat:@"%@/%@.m4a", DOCUMENTS_FOLDER,@"sample"];

NSLog(@"RecorderFilePath : %@",recorderFilePath);

NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];

//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;

BOOL audioHWAvailable = audioSession.inputAvailable;

[recorder record];

如果我在代码中做错了什么,请指出。

试试这个:

 AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,nil];


// [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

// Initiate and prepare the recorder
if (recorderHelium)
{
    recorderHelium = nil;
}

recorderHelium = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:recordedAudioFilePath] settings:recordSetting error:nil];
recorderHelium.delegate = self;
recorderHelium.meteringEnabled = YES;
[recorderHelium prepareToRecord];
[recorderHelium record];

同时导入并包含这些文件:

#import <AudioToolbox/AudioServices.h>
#include <AudioToolbox/AudioToolbox.h>