若有不正之处,希望大家不吝赐教,谢谢!

原生语音识别所需:
首先需要再plist文件中加入:
Privacy - Speech Recognition Usage Description
需要使用siri来进行语音识别

Privacy - Microphone Usage Description
同意后,您可以使用语音翻译的相关功能

百度翻译API所需,百度文档地址http://api.fanyi.baidu.com/api/trans/product/apidoc,官方文档提供:
Privacy - Camera Usage Description
访问相机

Privacy - Photo Library Usage Description
访问相册

原声语音识别

获取语音识别的权限,主要代码如下:

[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {switch (status) {case SFSpeechRecognizerAuthorizationStatusAuthorized:NSLog(@"可以语音识别");break;case SFSpeechRecognizerAuthorizationStatusDenied:NSLog(@"用户被拒绝访问语音识别");if(self.errorHandler){self.errorHandler(self, TSSpeechHelperErrorTypeUserRefuse);}break;case SFSpeechRecognizerAuthorizationStatusRestricted:NSLog(@"不能在该设备上进行语音识别");if(self.errorHandler){self.errorHandler(self, TSSpeechHelperErrorTypeNoNotPossible);}break;case SFSpeechRecognizerAuthorizationStatusNotDetermined:NSLog(@"没有授权语音识别");if(self.errorHandler){self.errorHandler(self, TSSpeechHelperErrorTypeNoPermission);}break;default:break;}}];
  • 使用SFSpeechRecognizer :Speech Kit 框架来进行语音识别 支持ios10 16年苹果推出
- (SFSpeechRecognizer *)speechRecognizer{if (_speechRecognizer == nil){// NSLocale 类返回本地化信息,主要体现在"语言"和"区域格式"这两个设置项NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:self.languageString];//英语就是 en_US_speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:cale];_speechRecognizer.delegate = self;}return _speechRecognizer;
}
  • 开始录音并识别
//开始录音
-(void)startRecording{if (self.recognitionTask) {[self.recognitionTask cancel];self.recognitionTask = nil;}NSError *error = nil;AVAudioSession *audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
[audioSession setMode:AVAudioSessionModeMeasurement error:&error];
[audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];if (error == nil) {//可以使用}else{//提示设备不支持的错误// TSSpeechHelperErrorTypeNoNotPossiblereturn;}self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init];AVAudioInputNode *inputNode = self.audioEngine.inputNode;self.recognitionRequest.shouldReportPartialResults = true;//开始识别任务self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {bool isFinal = false;if (result) {NSString * bestString = [[result bestTranscription] formattedString];isFinal = [result isFinal];NSLog(@"进行了一次语音识别,内容是: %@",bestString);}if (error || isFinal) {[self stopRecording];}}];AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0];[inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {[self.recognitionRequest appendAudioPCMBuffer:buffer];}];[self.audioEngine prepare];if (![self.audioEngine startAndReturnError:nil]) {//打开录音失败 TSSpeechHelperErrorTypeAudioStartError}
}//停止录音
- (void)stopRecording{[_audioEngine stop];[self.recognitionRequest endAudio];[_audioEngine.inputNode removeTapOnBus:0];_speechRecognizer = nil;
}

百度翻译api使用

使用百度翻译API提供的接口进行文字翻译

//翻译语言
- (void)initbdapiParameterDataWithModel:(NSString *)translationFromString  success:(void (^)(NSString * translationString))successfailure:(void (^)(NSError * error))failure  {//将APPID q salt key 拼接一起NSString *appendStr = [NSString stringWithFormat:@"%@%@%@%@",AppIdKey,model.translationFromString,SaltKey,SecretKey];//加密 生成签名  此处有坑:  这里不需要讲文字进行utf-8编码NSString *md5Str = [self md5:appendStr];//将待翻译的文字进行urf-8转码NSString *qEncoding = [translationFromString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//拼接请求url字符串NSString * bdTranslationurlString = [NSString stringWithFormat:@"http://api.fanyi.baidu.com/api/trans/vip/translate?q=%@&from=%@&to=%@&appid=%@&salt=%@&sign=%@",qEncoding,model.translationfromCodeString,model.translationToCodeString,AppIdKey,SaltKey,md5Str];//创建网络请求AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];__weak typeof(model)weakModel = model;//发起网络请求[manager GET:bdTranslationurlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {__strong typeof(model)strongModel = weakModel;// 判断是否翻译成功if (responseObject == nil) {success(@"翻译失败,请稍后再试!");translationToString = @"";return ;}//获取翻译后的字符串 eg: {"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]}NSString *resStr = [[responseObject objectForKey:@"trans_result"] firstObject][@"dst"];success(resStr);} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {//翻译失败 返回失败blockfailure(error);}];}

文字转语音播报

 ```   NSString *string = @"你好"NSString *yzString = @"zh-CN";AVSpeechSynthesizer *player  = [[AVSpeechSynthesizer alloc]init];AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc]initWithString:string];//设置语音内容speechUtterance.voice  = [AVSpeechSynthesisVoice voiceWithLanguage:yzString];//设置语言speechUtterance.rate   = 0.5;  //设置语速speechUtterance.volume = 1.0;  //设置音量(0.0~1.0)默认为1.0speechUtterance.pitchMultiplier    =  0.5;  //设置语调 (0.5-2.0)speechUtterance.postUtteranceDelay = 1; //目的是让语音合成器播放下一语句前有短暂的暂停[player speakUtterance:speechUtterance];

备注:主要播报文字代码如下,需要注意的是如果来语音识别后立刻想要播报,需要在播报之前开启Seeeion


AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setMode:AVAudioSessionModeMeasurement error:nil];
[audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
[audioSession setCategory:AVAudioSessionCategoryAmbient

ios 原生语音识别,百度翻译API使用,原生文字转语音播报相关推荐

  1. python调用百度翻译-Python 调用百度翻译API

    由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...

  2. php 百度收录api_php使用百度翻译api示例分享

    这篇文章主要介绍了php使用百度翻译api示例,需要的朋友可以参考下 百度翻译API的PHP代码,测试可以实现,不过英译中可能需要转换编码. function language($value,$fro ...

  3. 百度翻译API的使用

    百度翻译API 使用 关注 "弋凡"(YiFan)微信公众号吧 记录简单笔记 做你的最爱 1,注册百度翻译平台账号 http://api.fanyi.baidu.com/ 2,得到 ...

  4. python百度翻译接口_python3 调用百度翻译API翻译英文

    自行申请百度开发者账号import importlib,sys,urllib importlib.reload(sys) import urllib.request import json #导入js ...

  5. Android使用百度翻译api

    一直想试用一下百度翻译的接口,前些天从网上看了一篇关于json的博客,突然想试一下百度翻译api,于是我做了一个使用百度翻译api的Android小程序. 首先要使用百度翻译(也可以使用有道翻译)必须 ...

  6. 精简版_翻译软件,基于百度翻译api

    软件开发动机 虽然现在的翻译软件很多,但是而国内的翻译软件极为臃肿,包含了大量不需要的功能,以及各种广告及推广(呵呵--),所以自己用百度翻译api做了一个精简版的翻译软件,平常自用. 待实现的功能: ...

  7. python 英语翻译 excel_Python调用百度翻译api批量翻译Excel这工作效率

    目录 背景 实现Excel表批量翻译,解除百度翻译一次只能5000字数的限制. 源代码 百度翻译api 这里直接使用大佬已经写好的代码,自己稍加改动.账号密码需要自行去百度翻译开放平台注册.翻译语言 ...

  8. Go语言使用百度翻译api

    Go语言使用百度翻译api 之前做过一个使用百度翻译api的工具,这个工具用于用户的自动翻译功能,是使用C#调用百度翻译api接口,既然在学习Go语言,那必然也是要使用Go来玩耍一番.这里我是这么安排 ...

  9. python百度翻译api申请网页版_python3调用百度翻译api接口实现全过程

    现在很都平台的翻译api接口都开始收费了,比如谷歌.微软.yandex等等,注册非常麻烦,而且要钱,目前就百度还剩下一个最基础的翻译api不需要钱,今天我要和大家分享的是怎样使用python3调用百度 ...

最新文章

  1. python如何保存列表_Python 基础知识全篇-列表(Lists)
  2. 应用PlanAhead进行I/O规划
  3. Redis常见面试题总结
  4. 计算机原理及其应用的题,微机原理及应用习题与答案(4)
  5. 23装饰模式(Decorator Pattern)
  6. 下载 | 李宏毅:1 天搞懂深度学习,我总结了 300 页 PPT
  7. c语言队列作用,队列(C语言)
  8. DR.com客户端解密过程逆向分析
  9. 我的Android 4 学习系列之使用 Internet 资源
  10. weblogic 下载地址
  11. 阿里巴巴全资收购中国网络配送平台饿了么
  12. matlab心率和呼吸相关性分析,动态心电图的睡眠呼吸监测与心率变异性的相关性分析...
  13. 纳兰容若最经典的20首诗词,每一首都是经典,读完眼泪掉下来
  14. NVI(Non-Virtual Interface)手法
  15. python pandas 实现Excel自动填充功能
  16. ubuntu-4-VirtualBox中安装ubuntu及其版本介绍
  17. [教程]配置青鸟云Web服务器
  18. 一键获取Windows锁屏壁纸
  19. 视频教程-征服Node.js 7.x视频课程(6):文件系统与Stream视频课程-Node.js
  20. 崩坏3区号+86_86的区号是代表中国!那82是代表哪个国家?

热门文章

  1. Python金融数据挖掘
  2. iconv文件格式转换
  3. LeetCode 231: Power of Two
  4. 最新易语言入门到精通全套教程
  5. Android状态栏和导航栏透明和沉浸式模式
  6. VI高级命令集锦及VIM应用实例
  7. 一个功能齐全的 BitTorrent 库,支持 DHT、磁力链接、加密等功能
  8. PowerBI开发者账号申请,不限license
  9. 上位机倒出OASYS数据库方法
  10. php按关键字查询mysql_php 之 数据访问 查询关键字 (0506)