speechRecognition.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // components/speechRecognition/speechRecognition.js
  2. const plugin = requirePlugin("WechatSI")
  3. const manager = plugin.getRecordRecognitionManager()
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. show: {
  10. type: Boolean,
  11. value: false
  12. }
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. manager: null,
  19. start: false,
  20. resText: ""
  21. },
  22. /**
  23. * 组件的方法列表
  24. */
  25. methods: {
  26. start() {
  27. let that = this
  28. setTimeout(() => {
  29. manager.start()
  30. }, 10);
  31. manager.onError = function (res) {
  32. console.log(res)
  33. wx.showToast({
  34. title: '识别失败',
  35. icon: "none",
  36. duration: 3000
  37. })
  38. }
  39. manager.onStart = function (res) {
  40. console.log(1)
  41. }
  42. manager.onStop = function (res) {
  43. that.triggerEvent('result', res.result)
  44. }
  45. that.setData({
  46. start: true
  47. })
  48. },
  49. end() {
  50. let that = this
  51. manager.stop()
  52. that.setData({
  53. start: false
  54. })
  55. },
  56. result(e) {
  57. let that = this
  58. let text = e.currentTarget.dataset.text
  59. that.triggerEvent('result', text)
  60. },
  61. close() {
  62. let that = this
  63. that.triggerEvent('close', true)
  64. }
  65. }
  66. })