index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. data: {
  20. show: false,
  21. seconds: 60
  22. },
  23. methods: {
  24. /* 点击遮罩层 */
  25. onClose() {
  26. manager.stop();
  27. },
  28. /* 开启语音输入 */
  29. talking(e) {
  30. const that = this;
  31. manager.onError = function (res) {
  32. console.error("error msg", res.msg)
  33. clearInterval(countDown)
  34. that.setData({
  35. show: false,
  36. seconds: 60
  37. })
  38. wx.showToast({
  39. title: '语音识别错误' + res.msg,
  40. icon: "none",
  41. mask: true
  42. })
  43. };
  44. if (this.data.show) return manager.stop();
  45. wx.showToast({
  46. title: '语音识别录入中...',
  47. icon: "none",
  48. duration: 60000
  49. })
  50. manager.start({
  51. duration: 60000,
  52. lang: "zh_CN"
  53. })
  54. this.setData({
  55. show: true
  56. })
  57. countDown = setInterval(() => {
  58. if (this.data.seconds == 0) {
  59. clearInterval(countDown)
  60. this.setData({
  61. show: false,
  62. seconds: 60
  63. })
  64. } else {
  65. this.setData({
  66. seconds: this.data.seconds - 1
  67. })
  68. }
  69. }, 1000);
  70. identifyTheResult = function (value) {
  71. clearInterval(countDown)
  72. that.setData({
  73. show: false,
  74. seconds: 60
  75. });
  76. if (value) that.triggerEvent("callback", value)
  77. }
  78. },
  79. }
  80. })