voice.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const util = require('../../../../util/util.js')
  2. let playTimeInterval
  3. let recordTimeInterval
  4. Page({
  5. onShareAppMessage() {
  6. return {
  7. title: '录音',
  8. path: 'page/API/pages/voice/voice'
  9. }
  10. },
  11. data: {
  12. recording: false,
  13. playing: false,
  14. hasRecord: false,
  15. recordTime: 0,
  16. playTime: 0,
  17. formatedRecordTime: '00:00:00',
  18. formatedPlayTime: '00:00:00'
  19. },
  20. onHide() {
  21. if (this.data.playing) {
  22. this.stopVoice()
  23. } else if (this.data.recording) {
  24. this.stopRecordUnexpectedly()
  25. }
  26. },
  27. startRecord() {
  28. this.setData({recording: true})
  29. const that = this
  30. recordTimeInterval = setInterval(function () {
  31. const recordTime = that.data.recordTime += 1
  32. that.setData({
  33. formatedRecordTime: util.formatTime(that.data.recordTime),
  34. recordTime
  35. })
  36. }, 1000)
  37. wx.startRecord({
  38. success(res) {
  39. that.setData({
  40. hasRecord: true,
  41. tempFilePath: res.tempFilePath,
  42. formatedPlayTime: util.formatTime(that.data.playTime)
  43. })
  44. },
  45. complete() {
  46. that.setData({recording: false})
  47. clearInterval(recordTimeInterval)
  48. }
  49. })
  50. },
  51. stopRecord() {
  52. wx.stopRecord()
  53. },
  54. stopRecordUnexpectedly() {
  55. const that = this
  56. wx.stopRecord({
  57. success() {
  58. console.log('stop record success')
  59. clearInterval(recordTimeInterval)
  60. that.setData({
  61. recording: false,
  62. hasRecord: false,
  63. recordTime: 0,
  64. formatedRecordTime: util.formatTime(0)
  65. })
  66. }
  67. })
  68. },
  69. playVoice() {
  70. const that = this
  71. playTimeInterval = setInterval(function () {
  72. const playTime = that.data.playTime + 1
  73. console.log('update playTime', playTime)
  74. that.setData({
  75. playing: true,
  76. formatedPlayTime: util.formatTime(playTime),
  77. playTime
  78. })
  79. }, 1000)
  80. wx.playVoice({
  81. filePath: this.data.tempFilePath,
  82. success() {
  83. clearInterval(playTimeInterval)
  84. const playTime = 0
  85. console.log('play voice finished')
  86. that.setData({
  87. playing: false,
  88. formatedPlayTime: util.formatTime(playTime),
  89. playTime
  90. })
  91. }
  92. })
  93. },
  94. pauseVoice() {
  95. clearInterval(playTimeInterval)
  96. wx.pauseVoice()
  97. this.setData({
  98. playing: false
  99. })
  100. },
  101. stopVoice() {
  102. clearInterval(playTimeInterval)
  103. this.setData({
  104. playing: false,
  105. formatedPlayTime: util.formatTime(0),
  106. playTime: 0
  107. })
  108. wx.stopVoice()
  109. },
  110. clear() {
  111. clearInterval(playTimeInterval)
  112. wx.stopVoice()
  113. this.setData({
  114. playing: false,
  115. hasRecord: false,
  116. tempFilePath: '',
  117. formatedRecordTime: util.formatTime(0),
  118. recordTime: 0,
  119. playTime: 0
  120. })
  121. }
  122. })