前奏:

  iOS 10 出来之后,我们开发者也可以使用类似Siri的功能。它是使用Siri里面的一个语音识别框架Speech framework来处理siri的。现在, 让我们来看看 一些 主要的代码吧。 我们需要一个 UITextView 和 UIButton 就 能体现了。

实现:

  • 第一步:定义属性
@interface ViewController ()  <SFSpeechRecognizerDelegate>
@property (strong, nonatomic) UIButton *siriBtu;//siri按钮
@property (strong, nonatomic) UITextView *siriTextView; //显示语音转化成的文本
@property (strong, nonatomic) SFSpeechRecognitionTask *recognitionTask; //语音识别任务
@property (strong, nonatomic)SFSpeechRecognizer *speechRecognizer; //语音识别器
@property (strong, nonatomic) SFSpeechAudioBufferRecognitionRequest *recognitionRequest; //识别请求
@property (strong, nonatomic)AVAudioEngine *audioEngine; //录音引擎
@end
  • 第二步:进行语音识别检测
- (void)viewDidLoad {
[super viewDidLoad]; //设备识别语言为中文
NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];
self.speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:cale];
self.siriBtu.enabled = false;   //设置代理
_speechRecognizer.delegate = self;   //发送语音认证请求(首先要判断设备是否支持语音识别功能)
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
bool isButtonEnabled = false;
switch (status) {
case SFSpeechRecognizerAuthorizationStatusAuthorized:
isButtonEnabled = true;
NSLog(@"可以语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusDenied:
isButtonEnabled = false;
NSLog(@"用户被拒绝访问语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusRestricted:
isButtonEnabled = false;
NSLog(@"不能在该设备上进行语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusNotDetermined:
isButtonEnabled = false;
NSLog(@"没有授权语音识别");
break;
default:
break;
}
self.siriBtu.enabled = isButtonEnabled;
}];   //创建录音引擎
self.audioEngine = [[AVAudioEngine alloc]init];
}
  • 第三步:按钮的点击事件
- (void)microphoneTap:(UIButton *)sender {
if ([self.audioEngine isRunning]) {
[self.audioEngine stop];
[self.recognitionRequest endAudio];
self.siriBtu.enabled = YES;
[self.siriBtu setTitle:@"开始录制" forState:UIControlStateNormal];
}else{
[self startRecording];
[self.siriBtu setTitle:@"停止录制" forState:UIControlStateNormal];
}
}
  • 第四步:开始录制语音,并将语音转为文本
-(void)startRecording{
if (self.recognitionTask) {
[self.recognitionTask cancel];
self.recognitionTask = nil;
}
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
bool audioBool = [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
bool audioBool1= [audioSession setMode:AVAudioSessionModeMeasurement error:nil];
bool audioBool2= [audioSession setActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
if (audioBool || audioBool1|| audioBool2) {
NSLog(@"可以使用");
}else{
NSLog(@"这里说明有的功能不支持");
}
self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init];
AVAudioInputNode *inputNode = self.audioEngine.inputNode;
SFSpeechAudioBufferRecognitionRequest *recognitionRequest;
self.recognitionRequest.shouldReportPartialResults = true;   //开始识别任务
self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
bool isFinal = false;
if (result) {
self.siriTextView.text = [[result bestTranscription] formattedString]; //语音转文本
isFinal = [result isFinal];
}
if (error || isFinal) {
[self.audioEngine stop];
[inputNode removeTapOnBus:];
self.recognitionRequest = nil;
self.recognitionTask = nil;
self.siriBtu.enabled = true;
}
}];
AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:];
[inputNode installTapOnBus: bufferSize: format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
[self.recognitionRequest appendAudioPCMBuffer:buffer];
}];
[self.audioEngine prepare];
bool audioEngineBool = [self.audioEngine startAndReturnError:nil];
NSLog(@"%d",audioEngineBool);
self.siriTextView.text = @"我是小冰!Siri 冰,你说我听";
}
  • 第五步:实现这个代理方法,就实现siri语音功能了
-(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{
if(available){
self.siriBtu.enabled = true;
}else{
self.siriBtu.enabled = false;
}
}

为原博主点赞:http://www.jianshu.com/p/b29069529bc2

iOS:创建Siri 功能的更多相关文章

  1. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  2. ROS_Kinetic_24 使用catkin_create_qt_pkg快速创建qt-ros功能包

    使用catkin_create_qt_pkg快速创建qt-ros功能包 参考网址: qt_create:http://wiki.ros.org/qt_create qt_ros:https://git ...

  3. 提高 iOS App 通知功能启用率的三个策略

    我们都知道推送通知在 App 运营中的作用巨大.但是,很多用户却并不买帐,App 第一次启动提示是否「启用推送通知」时,他们直接选择了「否」. 是的,最近我本人就转变成了这样的人 - 认真地评估每个应 ...

  4. File的功能--> 获取功能-->所有的根目录 | 创建文件功能,但是如果文件已经存在-->不再创建(新手)

    //导入的包.import java.io.File;import java.io.FileFilter;import java.io.IOException; // 获取功能-->所有的根目录 ...

  5. 下载器Folx的创建种子功能怎么使用

    当我们想要分享一些自己制作的资源时,可以使用Folx的创建种子功能,在网络上分享种子,供他人下载,这个过程也被称为做种.作为种子创建者,需要在一定时间内保持做种进程,以便维持种子的生命期限,方便他人下 ...

  6. ROS2学习之旅(13)——创建ROS2 功能包

    一个功能包可以被认为是ROS2代码的容器.如果希望能够管理代码或与他人共享代码,那么需要将其组织在一个包中.通过包,可以发布ROS2工作,并允许其他人轻松地构建和使用它. 在ROS2中,创建功能包使用 ...

  7. File类创建删除功能的方法和File类遍历(文件夹)目录功能

    File类创建删除功能的方法 -public boolean createNewFile():当且仅当具有该名称的文件尚不存在时,创建一个新的空文件 -public boolean delete(): ...

  8. File类创建删除功能的方法和File类遍历目录功能

    File类创建删除功能的方法 public boolean createNewFile();当且仅当具有该名称的文件尚不存在的时候,创建一个新的空文件 public boolean delete(); ...

  9. iOS 创建本地私有库 保存功能代码

    创建本地私有库 >>> cd /Users/cxx/Desktop/Mange_JJH/Lib >>> pod lib create TZTools >> ...

随机推荐

  1. Microsoft.AlphaImageLoader滤镜讲解

    Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就IE5-IE6而言还是 ...

  2. unauthenticated user reading from net

    今天有台数据库异常,登录服务器后执行show processlist,发现大量的 unauthenticated user 状态.如下: 于是第一时间想到DNS反向解析的问题,于是看看是否关闭DNS解 ...

  3. http页面转发和重定向的区别

    一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下:request.getRequestDispatcher("new.jsp").forward(request ...

  4. 【iHMI43 4.3寸液晶模块】demo例程(版本1.02)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  5. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  6. CSS系列:长度单位&字体大小的关系em rem px

    em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸. 任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=1 ...

  7. GTX 770 (GK 104)

    上周的这个时候,NVIDIA GeForce 700系列的旗舰产品GTX 780正式发布,传闻已久的GTX 700家族终于来了!虽然没有任何新架构.新特性的旗舰卡发布总让人觉得少点什么.但从性能上来说 ...

  8. Nginx 笔记与总结(5)访问日志管理:计划任务 + 日志切割

    要在第二天的凌晨把前一天的访问日志切割备份,并以时间作为文件名,例如:access.20150728.log,这就需要在 Linux 中格式化时间,例如: [root@localhost ~]# da ...

  9. ecshop lang用法

    ecshop lang用法 分类: ECSHOP2013-08-15 16:17 2184人阅读 评论(0) 收藏 举报 ecshop目录下的languages目录.这个是ecshop语言包所在.ec ...

  10. XML解析器(转)

    常见C/C++ XML解析器有tinyxml.XERCES.squashxml.xmlite.pugxml.libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的.如果你是第一次接触 ...