ios - 关于AVAudioRecorder录音失败的问题
高洛峰
高洛峰 2017-04-17 14:56:03
0
2
1237
- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *docPath=[dirPath objectAtIndex:0];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"test1.wav"];
    _url=[NSURL URLWithString:filePath];

    NSDictionary *recordSettings=[NSDictionary dictionaryWithObjectsAndKeys:
          [NSNumber numberWithInt:AVAudioQualityMin],
          AVEncoderAudioQualityKey,
          [NSNumber numberWithInt:16],
          AVEncoderBitRateKey,
          [NSNumber numberWithInt:2],
          AVNumberOfChannelsKey,
          [NSNumber numberWithFloat:44100.0],
          AVSampleRateKey,
          nil];
    NSError *error;
    //AVAudioRecorder *recorder;
    //AVAudioPlayer *player;

    _player=[[AVAudioPlayer alloc] initWithContentsOfURL:_url error:&error];
    _recorder=[[AVAudioRecorder alloc] initWithURL:_url settings:recordSettings error:nil];
    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    }else{
        [_recorder prepareToRecord];
    }
}

调试的时候显示_player的值为nil,还有报错: The operation couldn’t be completed. (OSStatus error 2003334207.)我没在网上找到答案。

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
黄舟

The value of _player is nil. This should be caused by a bug in Xcode. If you use NSLog to output, _player should have a value.

  1. Remember to set a proxy for AVAudioRecorder
  2. For iOS7 and above, permission control for the recording function has been added. You need to first determine whether the application has recording permission. If the application does not have recording permission, prompt the user to open it.
    , Judgment method:
objectivec/// 新增API,获取录音权限. 返回值,YES为无拒绝,NO为拒绝录音.
+ (BOOL)canRecord
{
    __block BOOL bCanRecord = YES;
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
                if (granted) {
                    bCanRecord = YES;
                } else {
                    bCanRecord = NO;
                }
            }];
        }
    }
    return bCanRecord;
}
  1. The following code turns the device on to recording mode:
objectivecAVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
小葫芦

Owner, has it been resolved? I also encountered the same problem. I guess it was an error in converting to wav format

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template