APP——語音識別

//HubuilderX 語音識別插件配置: https://ask.dcloud.net.cn/article/35059

封裝的工具類speech.js

let instance = null;

class Speech {
  constructor() {
    if (!instance) {
      instance = this;
    }
    return instance;
  }
  // 初始化 語音識別
  initRecognize() {
    plus.speech.addEventListener('start', () => {
      // console.log('開始語音識別');
      this._start();
    }, false);
    plus.speech.addEventListener('volumeChange', ({
      volume
    }) => {
      // console.log('音量變化', volume);
      this._volumeChange({
        volume
      })
    }, false);
    plus.speech.addEventListener('recognizing', ({
      partialResult
    }) => {
      // console.log('臨時語音識別結果', partialResult);
      this._recognizing({
        partialResult
      })
    }, false);
    plus.speech.addEventListener('recognition', ({
      result
    }) => {
      // console.log('最終語音識別', result);
      this._recognition({
        result
      })
    }, false);
    plus.speech.addEventListener('end', () => {
      // console.log('結束語音識別');
      this._end()
    }, false);
    plus.speech.addEventListener('error', ({
      code,
      message
    }) => {
      console.log('語音識別錯誤', code, message);
      this._error({
        code,
        message
      })
    }, false);
  }
  // 開始語音識別
  start({
    start = () => {},
    volumeChange = () => {},
    recognizing = () => {},
    recognition = () => {},
    end = () => {},
    error = () => {},
  }) {
    this._start = start;
    this._volumeChange = volumeChange;
    this._recognizing = recognizing;
    this._recognition = recognition;
    this._end = end;
    this._error = error;
    const options = {
      engine: 'baidu', // 百度:baidu  訊飛:iFly
      continue: true,
      // userInterface: false
    };
    plus.speech.startRecognize(options);
  }
  stop() {
    plus.speech.stopRecognize();
  }
}

export default new Speech();

import Speech from '@/Speech.js';
Vue.prototype.$speech = Speech;

 

引用:

 <m-button class="demo-btn" type="primary" @click.native="startRecognize">

// 開始識別
    startRecognize() {
      this.$speech.start({
        start: () => {
          this.saveLog('開始語音識別');
        },
        volumeChange: ({ volume }) => {
          this.saveLog('音量變化: ' + volume);
        },
        recognizing: ({ partialResult }) => {
          this.saveLog('臨時語音識別結果: ' + partialResult);
        },
        recognition: ({ result }) => {
          this.saveLog('最終語音識別: ' + result);
        },
        end: () => {
          this.saveLog('結束語音識別');
        },
        error: ({ code, message }) => {
          this.saveLog('語音識別錯誤: ' + code + ',' + message);
        }
      });
    },

saveLog(message) {
      this.logs.unshift(message);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章