2016年的WWDC大会上,苹果公司公布了一下新的框架 语音转文字。Speech framework,然后就自己感觉挺有意思就研究了一下。
1.首先你要在xcode8里去创建一个工程。然后添加speech头文件。
#import <Speech/Speech.h>
2.创建一个按钮用来启动,关闭录音。创建一个textfield来显示转换好的文字。创建一个语音控制器。创建一个语音识别请求。创建一个管理语音任务管理器。还有一个录音设备。并且给管理器添加代理方法
@interface ViewController ()<SFSpeechRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *recordingBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UITextField *inPutTextField;
@property(nonatomic,strong)SFSpeechRecognizer * recognizer ;
//语音识别功能
@property(nonatomic,strong)SFSpeechAudioBufferRecognitionRequest * recognitionRequest ;
@property(nonatomic,strong)SFSpeechRecognitionTask * recognitionTask ;
@property(nonatomic,strong)AVAudioEngine * audioEngine ;
3.将设备识别语音为中文
NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];
self.recognizer = [[SFSpeechRecognizer alloc]initWithLocale:cale];
self.recordingBtn.enabled = false;
//设置代理
self.recognizer.delegate = self;
4.发送语音认证请求(首先要判断设备是否支持语音识别功能)
[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.recordingBtn.enabled = isButtonEnabled;
}];
5.创建录音引擎。
self.audioEngine = [[AVAudioEngine alloc]init];
6.创建按钮点击事件
- (void)BtnClick:(UIButton *)sender {
if ([self.audioEngine isRunning]) {
[self.audioEngine stop];
[self.recognitionRequest endAudio];
self.recordingBtn.enabled = YES;
[self.recordingBtn setTitle:@"开始录制" forState:UIControlStateNormal];
}else{
[self startRecording];
[self.recordingBtn setTitle:@"停止录制" forState:UIControlStateNormal];
}
}
7.开始录音
- (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;
self.recognitionRequest.shouldReportPartialResults = true;
//开始识别任务
self.recognitionTask = [self.recognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
bool isFinal = false;
if (result) {
self.inPutTextField.text = [[result bestTranscription] formattedString]; //语音转文本
isFinal = [result isFinal];
}
if (error || isFinal) {
[self.audioEngine stop];
[inputNode removeTapOnBus:0];
self.recognitionRequest = nil;
self.recognitionTask = nil;
self.recordingBtn.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.inPutTextField.text = @"大妹砸。聊十块钱的";
}
8.设置代理方法。
- (void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available{
if (available) {
self.recordingBtn.enabled = YES;
}else{
self.recordingBtn.enabled = NO;
}
}