科大讯飞MSC(Mobile Speech Client,移动语

2017-10-27  本文已影响169人  范范饭特稀

前言

首先,换工作啦~新公司新项目,也用到了新的第三方.所以从本篇开始,总结一下项目里使用到的第三方服务.项目里用到了语音识别,所以在这里简单的介绍一下吧.这里是官方文档

集成

集成步骤官方文档里已经很详细了,此处省略.

语音识别

简单的写了一个工具类来实现语音识别的功能,sdk相关配置和参数,代码如下:

final class VoiceTool: NSObject {
    static let sharedInstance = VoiceTool()
    
    private let IFLY_APP_ID = 你的App id
    private let speechRecognizer = IFlySpeechRecognizer.sharedInstance()
    
    
    private override init() {
        super.init()
        startIflyServer()
        configRecognizer()
    }
    
    private func startIflyServer() {
        #if DEBUG
            // 配置log
            IFlySetting.setLogFile(.LVL_NORMAL)
            IFlySetting.showLogcat(false)
            let logPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0]  + "/IFlyLog"
            IFlySetting.setLogFilePath(logPath)
        #endif
        IFlySpeechUtility.createUtility("appid="+IFLY_APP_ID)
    }
    
    private func configRecognizer() {
        // 扩展参数。
        speechRecognizer?.setParameter("", forKey: IFlySpeechConstant.params())
        // 设置听写模式
        speechRecognizer?.setParameter("iat", forKey: IFlySpeechConstant.ifly_DOMAIN())
        // 回结果的数据格式
        speechRecognizer?.setParameter("plain", forKey: IFlySpeechConstant.result_TYPE())
        // 识别录音保存路径 保存在sdk工作路径中,如未设置工作路径,则默认保存在library/cache下
        speechRecognizer?.setParameter("asr.pcm", forKey: IFlySpeechConstant.asr_AUDIO_PATH())
        // 录音源 默认为麦克风 1
        speechRecognizer?.setParameter(IFLY_AUDIO_SOURCE_MIC, forKey: IFlySpeechConstant.audio_SOURCE())
        // 采样率 16k
        speechRecognizer?.setParameter("16000", forKey: IFlySpeechConstant.sample_RATE())
        // VAD前端点超时
        speechRecognizer?.setParameter("10000", forKey: IFlySpeechConstant.vad_BOS())
        // VAD后端点超时
        speechRecognizer?.setParameter("10000", forKey: IFlySpeechConstant.vad_EOS())
        // 最长录音时长 默认3000ms  30秒
        speechRecognizer?.setParameter("60000", forKey: IFlySpeechConstant.speech_TIMEOUT())
        // 是否返回标点 0: 无 1: 有
        speechRecognizer?.setParameter("1", forKey: IFlySpeechConstant.asr_PTT())
        speechRecognizer?.delegate = self
    }
    
    // MARK: - public function
    public func startRecord() {
        dPrint(message: "开始录音")
        speechRecognizer?.cancel()
        let result = speechRecognizer?.startListening()
        if !result! {
            dPrint(message: "启动服务失败")
        }
    }
    
    public func stopRecord() {
        dPrint(message: "停止录音")
        speechRecognizer?.stopListening()
    }
    
    public func cancelRecord() {
        dPrint(message: "取消录音")
        speechRecognizer?.cancel()
    }
}

extension VoiceTool: IFlySpeechRecognizerDelegate {
    // 识别结果回调
    func onResults(_ results: [Any]!, isLast: Bool) {
      
    }
    // 识别错误回调
    func onError(_ errorCode: IFlySpeechError!) {
     

    }
    /*
    // 音量变化回调
    func onVolumeChanged(_ volume: Int32) {
        
    }
    // 开始录音回调
    func onBeginOfSpeech() {
//        dPrint("start")
    }
    // 结束录音回调
    func onEndOfSpeech() {
//        dPrint("stop")
    }
    // 取消录音回调
    func onCancel() {
//        dPrint("cancel")
    }
 */
}

通过startRecord方法进行语音识别,使用stopRecord方法停止语音识别.识别的结果会通过IFlySpeechRecognizerDelegate的代理方法给出.我们可以通过代理或者闭包将识别结果传出.

最后

语音识别成功后会自动保存到sdk工作目录下,如果涉及到语音的转码或者其他操作,这些部分才是重点~

上一篇 下一篇

猜你喜欢

热点阅读