// HubuilderX speech recognition plug-in configuration: https://ask.dcloud.net.cn/article/35059
Packaging tools 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;
Quote:
<m-button class="demo-btn" type="primary" @click.native="startRecognize">
// start identification
startRecognize () {
the this $ speech.start ({.
Start: () => {
this.saveLog ( 'start voice recognition');
},
volumeChange: (Volume {}) => {
this.saveLog ( 'volume changes: "volume +);
},
Recognizing: (partialResult {}) => {
this.saveLog (' temporary speech recognition results: '+ partialResult);
},
recognition: (result {}) => {
the this. saveLog ( 'final speech recognition:' Result +);
},
end: () => {
this.saveLog ( 'end of the voice recognition');
},
error: ({code, Message}) => {
this.saveLog ( 'speech recognition error:'+ code + ',' + message);
}
});
},
saveLog(message) {
this.logs.unshift(message);
}