iOS 10 Siri调用
iOS10 苹果开放SIri API,SiriKit的功能非常强大,支持音频、视频、消息发送接收、搜索照片、预订行程、管理锻炼等等。#import@interface ViewController ()/**
* 1. 先导入Speech.framework的框架
* 2. 导入头文件设置代理
* 3. 声明语音识别器
* 4. 授权
*/
@property (strong, nonatomic) IBOutlet UIButton *siriBtn;//siri按钮
@property (strong, nonatomic) IBOutlet UITextView *siriTextView;//显示语音转化成文字
@property(nonatomic,strong)SFSpeechRecognitionTask *recongnitionTask;//语音识别任务
@property(nonatomic,strong)SFSpeechRecognizer *speechRecognizer;//语音识别器
@property(nonatomic,strong)SFSpeechAudioBufferRecognitionRequest *recongnitionRequest;//语音识别请求
@property(nonatomic,strong)AVAudioEngine *audioEngine;//录音引擎
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设备识别语言为中文
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];
self.speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:locale];
self.siriBtn.enabled = false;
//设置语音识别代理
_speechRecognizer.delegate =self;
//发送语音认证请求(首先要判断设备是否支持语音识别功能)
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
bool isButtonEnaled =false;//初始值不允许
switch (status) {
case SFSpeechRecognizerAuthorizationStatusAuthorized:
isButtonEnaled = true;
NSLog(@"可以语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusDenied:
isButtonEnaled = false;
NSLog(@"用户被拒绝访问语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusRestricted:
isButtonEnaled = false;
NSLog(@"不能在该设备使用语音识别");
break;
case SFSpeechRecognizerAuthorizationStatusNotDetermined:
isButtonEnaled = false;
NSLog(@"没有授权语音识别");
default:
break;
}
self.siriBtn.enabled = isButtonEnaled;
}];
///创建录音引擎
self.audioEngine = [[AVAudioEngine alloc]init];
}
//按钮的点击事件
- (IBAction)microphoneTap:(UIButton *)sender {
if ([self.audioEngine isRunning]) {
[self.audioEngine stop];
[self.recongnitionRequest endAudio];
self.siriBtn.enabled =YES;
[self.siriBtn setTitle:@"开始录制" forState:UIControlStateNormal];
}else{
[self startRecording];
[self.siriBtn setTitle:@"停止录制" forState:UIControlStateNormal];
}
}
//开始录制语音,并将语音转换成文字
-(void)startRecording{
if (self.recongnitionTask) {
[self.recongnitionTask cancel];
self.recongnitionTask = 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.recongnitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init];
AVAudioInputNode *inputmode = self.audioEngine.inputNode;
SFSpeechAudioBufferRecognitionRequest *recongnitionRequest;
self.recongnitionRequest.shouldReportPartialResults =true;
//开始识别任务
self.recongnitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recongnitionRequest 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];
[inputmode removeTapOnBus:0];
self.recongnitionRequest = nil;
self.recongnitionTask = nil;
self.siriBtn.enabled = true;
}
}];
AVAudioFormat *recodingFormat = [inputmode outputFormatForBus:0];
[inputmode installTapOnBus:0 bufferSize:1024 format:recodingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
[self.recongnitionRequest appendAudioPCMBuffer:buffer];
}];
[self.audioEngine prepare];
bool audioEngineBool = [self.audioEngine startAndReturnError:nil];
NSLog(@"%d",audioEngineBool);
self.siriTextView.text = @"我是 siri,你说我听";
}
//实现代理,可以录音
-(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{
if(available){
self.siriBtn.enabled = true;
}else{
self.siriBtn.enabled = false;
}
}