iOS:创建Siri 功能
前奏:
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:0]; self.recognitionRequest = nil; self.recognitionTask = nil; self.siriBtu.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.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
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!