VoiceRecognizer.cancel

解释: 取消

# 方法参数

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例

  • 在 swan 文件中
<view class="result">{{result}}</view>
<button type="primary" bindtap="voiceRecognizerStart">点击开始识别语音</button>
<button bindtap="voiceRecognizerCancel">点击取消识别</button>
  • 在 js 文件中
Page({
    data: {
        result: ''
    },
    voiceRecognizerStart() {
        // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
        let host = swan.getSystemInfoSync().host;
        if (host === 'baiduboxapp') {
            const voiceRecognizer = swan.ai.getVoiceRecognizer();
            voiceRecognizer.onStart(res => {
                swan.showToast({
                    title: '开始识别',
                    icon: 'none'
                });
            });
            voiceRecognizer.onRecognize(res => {
                console.log('voice recognize', res.result);
                this.setData('result', res.result);
            });
            const options = {
                mode: 'touch',
                longSpeech: true
            };
            voiceRecognizer.start(options);
            this.voiceRecognizer = voiceRecognizer;
        }
        else {
            swan.showToast({
                title: '此api目前仅可在百度App上使用',
                icon: 'none'
            });
        }
    },
    voiceRecognizerCancel() {
         let host = swan.getSystemInfoSync().host;
        if (host === 'baiduboxapp') {
            this.voiceRecognizer.cancel()
            this.setData('result', '');
        }
        else {
            swan.showToast({
                title: '此api目前仅可在百度App上使用',
                icon: 'none'
            });
        }
    }
})