index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const plugin = requirePlugin("WechatSI"),
  2. manager = plugin.getRecordRecognitionManager();
  3. let countDown = null,
  4. identifyTheResult = null;
  5. manager.onStop = function (res) {
  6. console.log("识别结束", res)
  7. wx.showToast({
  8. title: res.result ? '录入成功' : '未接收到录入声音',
  9. icon: "none",
  10. })
  11. identifyTheResult(res.result)
  12. }
  13. Component({
  14. properties: {
  15. callback: {
  16. type: Function
  17. }
  18. },
  19. options: {
  20. addGlobalClass: true
  21. },
  22. data: {
  23. show: false,
  24. seconds: 60
  25. },
  26. methods: {
  27. /* 点击遮罩层 */
  28. onClose() {
  29. manager.stop();
  30. },
  31. /* 开启语音输入 */
  32. talking(e) {
  33. const that = this;
  34. manager.onError = function (res) {
  35. console.error("error msg", res.msg)
  36. clearInterval(countDown)
  37. that.setData({
  38. show: false,
  39. seconds: 60
  40. })
  41. wx.showToast({
  42. title: '语音识别错误' + res.msg,
  43. icon: "none",
  44. mask: true
  45. })
  46. };
  47. if (this.data.show) return manager.stop();
  48. wx.showToast({
  49. title: '语音识别录入中...',
  50. icon: "none",
  51. duration: 60000
  52. })
  53. manager.start({
  54. duration: 60000,
  55. lang: "zh_CN"
  56. })
  57. this.setData({
  58. show: true
  59. })
  60. countDown = setInterval(() => {
  61. if (this.data.seconds == 0) {
  62. clearInterval(countDown)
  63. this.setData({
  64. show: false,
  65. seconds: 60
  66. })
  67. } else {
  68. this.setData({
  69. seconds: this.data.seconds - 1
  70. })
  71. }
  72. }, 1000);
  73. identifyTheResult = function (value) {
  74. clearInterval(countDown)
  75. that.setData({
  76. show: false,
  77. seconds: 60
  78. });
  79. if (value) that.triggerEvent("callback", value)
  80. }
  81. },
  82. }
  83. })