iOS语音识别

2019-11-23  本文已影响0人  大宝来巡山
  //创建本地化
   NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
     //1.创建一个语音识别对象
   SFSpeechRecognizer *sf =[[SFSpeechRecognizer alloc] initWithLocale:local];
   
   //2.将bundle 中的资源文件加载出来返回一个url
   
   NSURL *url =[[NSBundle mainBundle] URLForResource:@"游子吟.mp3" withExtension:nil];
   //3.将资源包中获取的url 传递给 request 对象
   SFSpeechURLRecognitionRequest *res =[[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
   
   //2.发送一个请求
   [sf recognitionTaskWithRequest:res resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
       if (error!=nil) {
           NSLog(@"语音识别解析失败,%@",error);
       }
       else
       {
           //解析正确
           NSLog(@"---%@",result.bestTranscription.formattedString);
       }
   }];
   
   /**
    语音识别同样的需要真机进行测试 ,因为需要硬件的支持,还需要访问权限
    ***/

上一篇 下一篇

猜你喜欢

热点阅读