美文网首页
iOS 10 语音识别

iOS 10 语音识别

作者: ShenYj | 来源:发表于2017-01-13 11:43 被阅读800次

完整Demo代码:

#import "ViewController.h"
#import <Speech/Speech.h>  // 导入类库

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 0. 请求权限 && 配置Info.plist
    /** 若不配置info.plist会Crash报错:
     This app has crashed because it attempted to access privacy-sensitive data without a usage description.  
     The app's Info.plist must contain an NSSpeechRecognitionUsageDescription key with a string value explaining to the user how the app uses this data.
     */
    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
        switch (status) {
            case SFSpeechRecognizerAuthorizationStatusNotDetermined:
                NSLog(@"SFSpeechRecognizerAuthorizationStatusNotDetermined");
                break;
            case SFSpeechRecognizerAuthorizationStatusDenied:
                NSLog(@"SFSpeechRecognizerAuthorizationStatusDenied");
                break;
            case SFSpeechRecognizerAuthorizationStatusRestricted:
                NSLog(@"SFSpeechRecognizerAuthorizationStatusRestricted");
                break;
            case SFSpeechRecognizerAuthorizationStatusAuthorized:
                NSLog(@"SFSpeechRecognizerAuthorizationStatusAuthorized");
                break;
            default:
                break;
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 1. 初始化一个识别器
    SFSpeechRecognizer *recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    
    // 2. 初始化mp3的url  (我在Bundle中导入了一个本地的MP3文件进行演示)
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"buyao.mp3" withExtension:nil];
    
    // 3. 根据Url创建请求
    SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:fileUrl];
    
    // 4. 发起请求
    [recognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"识别错误:%@",error);
            return ;
        }
        NSLog(@"%@",result.bestTranscription.formattedString);
    }];
}

@end

控制台打印结果:(实际是斗地主的要不起,辨识度有待提高)

2017-01-13 11:36:58.774398 SpeechDemo[964:603117] SFSpeechRecognizerAuthorizationStatusAuthorized
2017-01-13 11:37:01.365989 SpeechDemo[964:603076] 识别错误:Error Domain=kAFAssistantErrorDomain Code=209 "(null)"
2017-01-13 11:37:01.503930 SpeechDemo[964:603076] 识别错误:Error Domain=kAFAssistantErrorDomain Code=209 "(null)"
2017-01-13 11:37:02.338849 SpeechDemo[964:603076] 要不钱
2017-01-13 11:37:02.339523 SpeechDemo[964:603076] 要不钱
2017-01-13 11:37:02.347165 SpeechDemo[964:603076] 要不钱

需要注意的地方就是需请求授权,并配置Info.plist,否则Crash.
NSSpeechRecognitionUsageDescription对应配置的string value便是授权时的提示信息
e.g.我这里这是的是语音识别授权

IMG_0037.PNG

相关文章

网友评论

      本文标题:iOS 10 语音识别

      本文链接:https://www.haomeiwen.com/subject/yyfsbttx.html