iOS开发——Siri语音识别
来源:互联网 发布:沉戟酥油饼txt知乎 编辑:程序博客网 时间:2024/05/17 12:07
原理: 先用系统的录音器录音,让后让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]; // 创建信息label UILabel *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:^{ }]; }
0 0
- iOS开发——Siri语音识别
- Siri 语音识别 Speech
- 【iOS开发】---- 语音识别
- ios系列——语音识别
- iOS——科大讯飞之语音识别
- iOS语音识别——OC
- ios开发科大讯飞的语音识别
- ios开发-语音识别(科大讯飞)
- 百度语音识别服务 —— 语音识别 REST API 开发笔记
- Android开发 语音识别技术之科大讯飞语音识别(一) — 语音听写
- Android开发 语音识别技术之科大讯飞语音识别(二) — 语音合成
- iOS开发之类Siri的语言识别程序的开发与实践(Nuance技术)
- Siri工作方式详解:本地语音识别+云计算服务
- Siri工作方式详解:本地语音识别+云计算服务
- 苹果组建语音识别团队 或抛弃Siri技术提供商
- ios语音识别实现——科大讯飞的使用
- iOS Siri的语言识别程序的开发与实践(Nuance技术)
- Kinect开发笔记之二——语音识别
- CodeForces 779B Weird Rounding
- 算法:DES
- 动态材质实例(Dynamic Material Instance)【UE4】
- mysql里面如何用sql语句让字符串转换为数字
- Ubuntu14.04下安装OpenCV3.0经验
- iOS开发——Siri语音识别
- 通过JS判断浏览器类型,详细区分IE各版本浏览器
- 关于Xcode报错, 但编译,运行正常情况
- PAT 1029. Median (25)
- Ext Grid中的Summary功能
- Android之设置状态栏颜色
- Apache的httpclient版本大于4.3后,通过HttpClient发起http请求
- Git 常用命令大全
- 蓝桥杯ADV-143——算法提高 扶老奶奶过街