音频录制与iOS10语音识别

苹果在iOS10增加了一个语音识别功能,并且将其封装到了SpeechFramework库中,使用非常方便,在这里我用一个案例进行展示,需求如下图,点击开始录音和停止录音完成录音功能,点击识别录音对录音内容进行识别并展示到Label中
这里写图片描述

配置info.plist文件

想要使用录音和语音识别功能必须先配置info.plist文件,在其中增加授权属性(如果value值不填可能会出现问题)

<key>NSSpeechRecognitionUsageDescription</key>  
<string></string>   
<key>NSMicrophoneUsageDescription</key>  
<string></string> 

或者直接在info.plist中添加
这里写图片描述

录音

1 实例化录音对象(需要引用AVFoundation框架)

- (void)setupAudioRecorder{
    // 设置录音路径
    self.pathRecord = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"record.caf"];
    // 如果这段代码不添加真机运行会出现问题
    AVAudioSession *session = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&setCategoryError];
    // 实例化录音对象
    NSError *errorRecord = nil;
    self.recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:self.pathRecord] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&errorRecord];
    // 准备录音
    [self.recorder prepareToRecord];
}

2 点击开始录音和结束录音完成录音并将其存入沙盒

// 开始录音
- (IBAction)satrtSoundRecording:(UIButton *)sender {
    [self.recorder record];
}
// 停止录音
- (IBAction)stopSoundRecording:(UIButton *)sender {
    [self.recorder stop];
}

语音识别

首先要导入语音识别的头文件Speech/Speech.h,然后想要使用语音识别要请求用户授权,当用户授权成功之后才可以进行识别,具体代码实现如下

// 识别录音
- (IBAction)identificationSoundRecording:(UIButton *)sender {
    //请求权限
    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
        /* 
         status 为请求权限的结果,有以下几个状态:
         1 用户没有选择
         SFSpeechRecognizerAuthorizationStatusNotDetermined,
         2 用户拒绝了语音识别请求
         SFSpeechRecognizerAuthorizationStatusDenied,
         3 使用的设备不支持语音识别
         SFSpeechRecognizerAuthorizationStatusRestricted,
         4 取得了用户的授权
         SFSpeechRecognizerAuthorizationStatusAuthorized,
         */
        // 获取权限成功
        if (status == SFSpeechRecognizerAuthorizationStatusAuthorized) {
            NSURL *url = [NSURL fileURLWithPath:self.pathRecord];
            // 实例化语音识别对象
            SFSpeechRecognizer *recongize = [[SFSpeechRecognizer alloc] init];
            SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
            // 根据请求进行语音识别
            [recongize recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
                if (error) {
                    NSLog(@"%@",error.description);
                    return ;
                }
                if (result) {
                    // 将语音识别结果展示到UI中
                    self.showRecordLabel.text = result.bestTranscription.formattedString;
                }
            }];
        }
    }];
}

备注:
语音识别为iOS10增加的新特性,Xcode8之前的版本没有SpeechFramework库,所以此方法只能在Xcode8中运行。

猜你喜欢

转载自blog.csdn.net/bluecat_1128/article/details/52877270