voice.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const util = require('../../../../util/util.js')
  2. let playTimeInterval
  3. let recordTimeInterval
  4. const recorderManager = wx.getRecorderManager()
  5. const innerAudioContext = wx.createInnerAudioContext()
  6. Page({
  7. onShareAppMessage() {
  8. return {
  9. title: '录音',
  10. path: 'packageAPI/pages/media/voice/voice'
  11. }
  12. },
  13. data: {
  14. theme: 'light',
  15. recording: false, // 录音中
  16. playing: false, // 播放中
  17. hasRecord: false, // 已经录音
  18. recordTime: 0, // 录音时长
  19. playTime: 0, // 播放时长
  20. formatedRecordTime: '00:00:00', // 录音时间
  21. formatedPlayTime: '00:00:00' // 播放时间
  22. },
  23. onHide() {
  24. if (this.data.playing) {
  25. this.stopVoice()
  26. } else if (this.data.recording) {
  27. this.stopRecordUnexpectedly()
  28. }
  29. },
  30. onLoad() {
  31. this.setData({
  32. theme: wx.getSystemInfoSync().theme || 'light'
  33. })
  34. if (wx.onThemeChange) {
  35. wx.onThemeChange(({theme}) => {
  36. this.setData({theme})
  37. })
  38. }
  39. const that = this
  40. // 监听录音开始事件
  41. recorderManager.onStart(() => {
  42. console.log('recorderManage: onStart')
  43. // 录音时长记录 每秒刷新
  44. recordTimeInterval = setInterval(() => {
  45. that.data.recordTime += 1
  46. const recordTime = that.data.recordTime
  47. that.setData({
  48. formatedRecordTime: util.formatTime(that.data.recordTime),
  49. recordTime
  50. })
  51. }, 1000)
  52. })
  53. // 监听录音停止事件
  54. recorderManager.onStop((res) => {
  55. console.log('recorderManage: onStop')
  56. that.setData({
  57. hasRecord: true, // 录音完毕
  58. recording: false,
  59. tempFilePath: res.tempFilePath,
  60. formatedPlayTime: util.formatTime(that.data.playTime),
  61. })
  62. // 清除录音计时器
  63. clearInterval(recordTimeInterval)
  64. })
  65. // 监听播放开始事件
  66. innerAudioContext.onPlay(() => {
  67. console.log('innerAudioContext: onPlay')
  68. playTimeInterval = setInterval(() => {
  69. const playTime = that.data.playTime + 1
  70. if (that.data.playTime === that.data.recordTime) {
  71. that.stopVoice()
  72. } else {
  73. console.log('update playTime', playTime)
  74. that.setData({
  75. formatedPlayTime: util.formatTime(playTime),
  76. playTime
  77. })
  78. }
  79. }, 1000)
  80. })
  81. innerAudioContext.onStop(() => {
  82. })
  83. },
  84. startRecord() {
  85. this.setData({
  86. recording: true // 录音开始
  87. })
  88. // 设置 Recorder 参数
  89. const options = {
  90. duration: 10000, // 持续时长
  91. sampleRate: 44100,
  92. numberOfChannels: 1,
  93. encodeBitRate: 192000,
  94. format: 'aac',
  95. frameSize: 50
  96. }
  97. recorderManager.start(options) // 开始录音
  98. },
  99. stopRecord() {
  100. recorderManager.stop() // 停止录音
  101. },
  102. stopRecordUnexpectedly() {
  103. const that = this
  104. wx.stopRecord({
  105. success() {
  106. console.log('stop record success')
  107. clearInterval(recordTimeInterval)
  108. that.setData({
  109. recording: false,
  110. hasRecord: false,
  111. recordTime: 0,
  112. formatedRecordTime: util.formatTime(0)
  113. })
  114. }
  115. })
  116. },
  117. playVoice() {
  118. innerAudioContext.src = this.data.tempFilePath
  119. this.setData({
  120. playing: true,
  121. }, () => {
  122. innerAudioContext.play()
  123. })
  124. },
  125. pauseVoice() {
  126. clearInterval(playTimeInterval)
  127. innerAudioContext.pause()
  128. this.setData({
  129. playing: false
  130. })
  131. },
  132. stopVoice() {
  133. clearInterval(playTimeInterval)
  134. innerAudioContext.stop()
  135. this.setData({
  136. playing: false,
  137. formatedPlayTime: util.formatTime(0),
  138. playTime: 0
  139. })
  140. },
  141. clear() {
  142. clearInterval(playTimeInterval)
  143. innerAudioContext.stop()
  144. this.setData({
  145. playing: false,
  146. hasRecord: false,
  147. tempFilePath: '',
  148. formatedRecordTime: util.formatTime(0),
  149. recordTime: 0,
  150. playTime: 0
  151. })
  152. }
  153. })