VoiceRecognizer.stop

解释: 停止

方法参数

示例

在开发者工具中预览效果

VoiceRecognizer.stop - 图1请使用百度APP扫码

图片示例

VoiceRecognizer.stop - 图2

VoiceRecognizer.stop - 图3

VoiceRecognizer.stop - 图4

代码示例

  • 在 swan 文件中
  1. <view class="result">{{result}}</view>
  2. <button type="primary" bindtap="voiceRecognizerStart">点击开始识别语音</button>
  3. <button bindtap="voiceRecognizerStop">点击停止识别</button>
  • 在 js 文件中
  1. Page({
  2. data: {
  3. result: ''
  4. },
  5. voiceRecognizerStart() {
  6. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
  7. let host = swan.getSystemInfoSync().host;
  8. if (host === 'baiduboxapp') {
  9. swan.showToast({
  10. title: '开始识别',
  11. icon: 'none'
  12. });
  13. const voiceRecognizer = swan.ai.getVoiceRecognizer();
  14. voiceRecognizer.onRecognize(res => {
  15. console.log('voice recognize', res.result);
  16. this.setData('result', res.result);
  17. });
  18. const options = {
  19. mode: 'dnn',
  20. // mode: 'touch',
  21. longSpeech: true
  22. };
  23. voiceRecognizer.start(options);
  24. this.voiceRecognizer = voiceRecognize;
  25. }
  26. else {
  27. swan.showToast({
  28. title: '此api目前仅可在百度App上使用',
  29. icon: 'none'
  30. });
  31. }
  32. },
  33. voiceRecognizerStop() {
  34. let host = swan.getSystemInfoSync().host;
  35. if (host === 'baiduboxapp') {
  36. this.voiceRecognizer.stop();
  37. swan.showToast({
  38. title: '已停止识别'
  39. });
  40. }
  41. else {
  42. swan.showToast({
  43. title: '此api目前仅可在百度App上使用',
  44. icon: 'none'
  45. });
  46. }
  47. }
  48. })