美文网首页iOS Developer
iOS 语音转文字(系统api)

iOS 语音转文字(系统api)

作者: 抱着盒子的boy | 来源:发表于2017-03-29 16:32 被阅读0次

    由于讯飞科大的功能太多,包太大,所以用了iOS 10.0以后系统的语音转文字api。

    1. 首先需要导入框架Speech.framework

    2. 导入头文件#import <Speech/Speech.h>

    3. 申请用户语音识别权限

        [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:*******板凳宽扁担长扁担想绑在板凳上
    

    这里需要注意,他会循环矫正多次,最后得到结果,我们要用最终的结果。

    相关文章

      网友评论

        本文标题:iOS 语音转文字(系统api)

        本文链接:https://www.haomeiwen.com/subject/ikxyottx.html