原理: 先用系统的录音器录音,让后让siri识别语音转文字

第一步 :在项目plist文件添加授权,如下图

第二步:导入头文件,添加协议,

#import <Speech/Speech.h>

#import <AVFoundation/AVFoundation.h>

@interface SiriViewController () <SFSpeechRecognizerDelegate>

第三步:UI控件,实现siri识别方法,代码如下:

.h

#import <UIKit/UIKit.h>@interface SiriViewController : UIViewController@end

.m

//
//  SiriViewController.m
//  ASR
//
//  Created by 刘成利 on 2017/2/28.
//  Copyright © 2017年 刘成利. All rights reserved.
//
#define sWith     [UIScreen mainScreen].bounds.size.width
#define sHeight   [UIScreen mainScreen].bounds.size.height#import "SiriViewController.h"#import <Speech/Speech.h>
#import <AVFoundation/AVFoundation.h>#import "LongPressButton.h"@interface SiriViewController () <SFSpeechRecognizerDelegate>@property (nonatomic, strong) LongPressButton *longButton;    // 长按按钮
@property (nonatomic, strong) UITextField     *inputText;     // 语音转化成的文本// 录音引擎
@property (strong, nonatomic) AVAudioEngine            *audioEngine;
// 语音识别任务
@property (strong, nonatomic) SFSpeechRecognitionTask  *recognitionTask;
// 语音识别器
@property (strong, nonatomic) SFSpeechRecognizer       *speechRecognizer;
// 识别请求
@property (strong, nonatomic) SFSpeechAudioBufferRecognitionRequest *recognitionRequest;@end@implementation SiriViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];// 创建信息labelUILabel *textLabel      = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, sWith-100, 30)];textLabel.text          = @"【Siri】语音识别转文字";textLabel.font          = [UIFont fontWithName:@"HelveticaNeue" size:20.f];textLabel.textColor     = [UIColor blackColor];textLabel.textAlignment = NSTextAlignmentCenter;[self.view addSubview:textLabel];// 退出控制器{UIButton *dismissButton        = [[UIButton alloc] initWithFrame:CGRectMake(sWith/2-50, sHeight-100, 100, 50)];dismissButton.tag              = 10;dismissButton.backgroundColor  = [UIColor lightGrayColor];dismissButton.titleLabel.font  = [UIFont fontWithName:@"HelveticaNeue" size:16.f];[dismissButton setTitle:@"返回上一页" forState:UIControlStateNormal];[dismissButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];[dismissButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];[dismissButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:dismissButton];}// 语音识别self.inputText = [[UITextField alloc]initWithFrame:CGRectMake(50, 200, sWith-100, 100)];self.inputText.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.2];[self.view addSubview:self.inputText];// 长按按钮self.longButton = [[LongPressButton alloc]initWithFrame:CGRectMake(sWith/2-50, sHeight-300, sWith-300, sWith-300)];self.longButton.removeLoading = YES;// 保持在视图最上面[[UIApplication sharedApplication].keyWindow addSubview:self.longButton];[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.longButton];__weak SiriViewController *weakSelf = self;self.longButton.eventBlock = ^(TouchType TouchType,NSTimeInterval time){switch (TouchType) {case LongTouchBegin:[weakSelf startEvent];break;case LongTouchEnd:[weakSelf endEvent];break;default:break;}};// 基本设置[self basicSetup];}- (void)basicSetup{// 设备识别语言为中文NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];self.speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:cale];// 申请权限认证[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {dispatch_async(dispatch_get_main_queue(), ^{switch (status) {case SFSpeechRecognizerAuthorizationStatusNotDetermined:self.longButton.userInteractionEnabled = NO;self.inputText.text  = @"没有授权语音识别";break;case SFSpeechRecognizerAuthorizationStatusDenied:self.longButton.userInteractionEnabled = NO;self.inputText.text   = @"用户拒绝使用语音识别权限";break;case SFSpeechRecognizerAuthorizationStatusRestricted:self.longButton.userInteractionEnabled = NO;self.inputText.text   = @"不能在该设备上进行语音识别";break;case SFSpeechRecognizerAuthorizationStatusAuthorized:self.longButton.userInteractionEnabled = YES;self.inputText.text   = @"设备录音可用";break;default:break;}});}];// 创建录音引擎self.audioEngine = [[AVAudioEngine alloc]init];}// 语音按钮识别事件
- (void)endEvent{if ([self.audioEngine isRunning]) {[self.audioEngine stop];[self.recognitionRequest endAudio];}[self.longButton finishAndRest];}// 识别语音
-(void)startEvent{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;self.recognitionRequest.shouldReportPartialResults = true;//开始识别任务self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {bool isFinal = false;if (result) {self.inputText.text = [[result bestTranscription] formattedString]; //语音转文本isFinal = [result isFinal];}if (error || isFinal) {[self.audioEngine stop];[inputNode removeTapOnBus:0];self.recognitionRequest = nil;self.recognitionTask = nil;
//            self.siriButton.enabled = true;}}];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];bool audioEngineBool = [self.audioEngine startAndReturnError:nil];NSLog(@"%d",audioEngineBool);
//    self.inputText.text = @"正在录音。。。";
}#pragma mark - SFSpeechRecognizerDelegate
- (void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{if (available) {self.inputText.text = @"语音识别可用";}else{self.inputText.text = @"语音识别不可用";}
}// 退出控制器
- (void)dismiss{[self dismissViewControllerAnimated:YES completion:^{}];}

iOS开发——Siri语音识别相关推荐

  1. 【幻灯片分享】Siri:I,robot! Siri语音识别系统详解 | 新浪 张俊林 | iOS DevCamp

    iOS平台应用详解:<Siri:I,robot! Siri语音识别系统详解> | iOS DevCamp 话题简介:Siri是苹果公司发布的广受关注的iOS平台应用,它不仅仅是一套语音识别 ...

  2. CMUSphinx免费离线语音识别开源库教程iOS开发

    CMUSphinx开源语音识别工具包,可以基于CMUSphinx开发iOS版本的语音识别和语音合成库. 本文介绍的语音识别库使您可以轻松快速地将多种语言的离线语音识别和合成语音添加到iOS应用程序中. ...

  3. WebServer应用示例2:Siri语音识别读取传感器数据 | ESP32轻松学(Arduino版)

    本系列历史文章目录: ESP32 概述与 Arduino 软件准备 ESP32与掌控板IO接口编程入门 蓝牙翻页笔(PPT 控制器) B 站粉丝计数器 Siri 语音识别控制 LED 灯 Siri 语 ...

  4. 对于iOS开发人工智能意味着什么

    对于iOS开发人工智能意味着什么? 前言 近几年来人工智能的话题那是炙手可热.在国内很多大佬言必谈机器学习和大数据:在美国刚毕业的人工智能 PHD 也是众人追捧,工资直逼 NFL 四分卫.人工智能甚至 ...

  5. 【硅谷问道】对于 iOS 开发,人工智能意味着什么?

    前言 近几年来人工智能的话题那是炙手可热.在国内很多大佬言必谈机器学习和大数据:在美国刚毕业的人工智能 PHD 也是众人追捧,工资直逼 NFL 四分卫.人工智能甚至成为了互联网领域茶余饭后的话题 -- ...

  6. 对于 iOS 开发,人工智能意味着什么?

    前言 近几年来人工智能的话题那是炙手可热.在国内很多大佬言必谈机器学习和大数据:在美国刚毕业的人工智能 PHD 也是众人追捧,工资直逼 NFL 四分卫.人工智能甚至成为了互联网领域茶余饭后的话题 -- ...

  7. 用Arduino玩转掌控板(ESP32):Siri语音识别读取传感器数据→WebServer应用示例2

    众所周知,掌控板在创客教育中用的非常广泛,它是一块基于 ESP32 的学习开发板.大家对掌控板编程,用的比较多的都是图形化编程的方式,比如 mPython.Mind+ 等.但是,既然掌控板是基于 ES ...

  8. iOS开发基础知识--碎片44

    iOS开发基础知识--碎片44  iOS开发基础知识--碎片44 1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = ...

  9. iOS开发之如何跳到系统设置里的各种设置界面

    一·iOS开发之如何跳到系统设置里的WiFi界面 之前以为,苹果不支持直接从应用跳到系统设置里的WiFi界面.后来发现,这个小功能是可以实现的,而且实现起来并不麻烦.让我们一起来看看吧! 需求 从应用 ...

最新文章

  1. SecureCRT脚本之WaitForString函数
  2. SpringBoot各种Controller写法
  3. Python实现 灰色关联分析 与结果可视化
  4. js解析json字符串数组
  5. cobalt strick 4.0系列教程(3)---数据管理
  6. android 代理 wifi热点,android wifi热点默认网关
  7. 『线性同余方程和中国剩余定理』
  8. SparkSQL源代码:总体概述
  9. 等差数列java_Java实现 LeetCode 413 等差数列划分
  10. 本地连接远程代码库——生成公钥SSH Key(Linux版)
  11. c语言程序设计 网上资源,超星尔雅C语言程序设计-资源包完整答案
  12. 我的个人成长(1-3年)
  13. windows系统桌面壁纸软件推荐名称
  14. 复习3个月,雅思首考7.0
  15. Android View的事件分发机制和滑动冲突解决方案
  16. XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
  17. 镜头分辨率的计算和理解
  18. 2012系统架构设计师考试大纲
  19. Cox Container 部署Sawyer智能协作机器人
  20. python 获取国内期货_获取和讯期货数据(Python版本).md

热门文章

  1. 美国要求签证申请人提供社交媒体账号
  2. Android Froyo基于32 bit ubuntu 10.10编译问题
  3. Vue将图片转化为base64
  4. 应用程序无法正常启动(0xC0000142)
  5. 产品读书《设计中的设计》-设计
  6. 软件测试职业生涯规划
  7. 做电商网站的主键策略
  8. python 调整灰度图像对比度_Python实现PS图像调整之对比度调整功能示例
  9. 环评师c语言题目,C语言考试——编程题_文库吧
  10. CentOS中ip addr命令不显示ip地址问题的解决方法