由于讯飞科大的功能太多,包太大,所以用了iOS 10.0以后系统的语音转文字api。
-
首先需要导入框架Speech.framework
-
导入头文件#import <Speech/Speech.h>
-
申请用户语音识别权限
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
NSLog(@"------%ld",(long)status);
}];
typedef NS_ENUM(NSInteger, SFSpeechRecognizerAuthorizationStatus) {
//结果未知 用户尚未进行选择
SFSpeechRecognizerAuthorizationStatusNotDetermined,
//用户拒绝授权语音识别
SFSpeechRecognizerAuthorizationStatusDenied,
//设备不支持语音识别功能
SFSpeechRecognizerAuthorizationStatusRestricted,
//用户授权语音识别
SFSpeechRecognizerAuthorizationStatusAuthorized,
} API_AVAILABLE(ios(10.0));
//初始化
SFSpeechRecognizer * rec = [[SFSpeechRecognizer alloc]init];
//文件路径(这里有个坑,url初始化要用 fileURLWithPath,别用URLWithString,UIBoundle也行)
NSURL * url = [NSURL fileURLWithPath:path];
SFSpeechRecognitionRequest * request = [[SFSpeechURLRecognitionRequest alloc]initWithURL:url];//进行请求
[rec recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
//打印语音识别的结果字符串
NSLog(@"*******%@",result.bestTranscription.formattedString);
}];
//你会发现打印了好多次不完整的字符串,只有后两次能看,像这样:
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******反正
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******反正宽
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳宽
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担�想
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担�想�绑
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担�想�绑�在
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担�想�绑�在�板凳
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳�宽�扁担�长�扁担�想�绑�在�板凳�上
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳宽扁担长扁担想绑在板凳上
function:__52-[LGAVAudiorecorder prepareAudioPlayerWithFileName:]_block_invoke_2 line:82 content:*******板凳宽扁担长扁担想绑在板凳上
这里需要注意,他会循环矫正多次,最后得到结果,我们要用最终的结果。
网友评论