index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // pages/main/component/Player/index.js
  2. var AudioContext = wx.createInnerAudioContext()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. src: {
  9. type: String,
  10. observer: function (src) {
  11. console.log(this.data.auto)
  12. console.log("audio src changed", src)
  13. this.stop()
  14. if (this.data.auto) {
  15. this.resetAudioContext(src)
  16. }
  17. }
  18. },
  19. duration: {
  20. type: Number,
  21. observer: function(d) {
  22. this.setData({
  23. formatDuration: this.formatTimer(d)
  24. })
  25. }
  26. },
  27. auto: {
  28. type: Boolean
  29. }
  30. },
  31. /**
  32. * 组件的初始数据
  33. */
  34. data: {
  35. isPlay: false,
  36. progress: 0,
  37. formatCurrent: '00:00',
  38. formatDuration: '00:00'
  39. },
  40. audioCallback: null,
  41. canPlay: false,
  42. fetchDurationHandler: 0,
  43. /**
  44. * 组件的方法列表
  45. */
  46. methods: {
  47. stop: function () {
  48. AudioContext.stop()
  49. this.setData({
  50. isPlay: false,
  51. progress: 0,
  52. formatCurrent: '00:00'
  53. })
  54. },
  55. playAction: function (e) {
  56. if (this.fetchDurationHandler) {
  57. clearInterval(this.fetchDurationHandler)
  58. }
  59. if (!this.data.src) {
  60. return
  61. }
  62. /*if (AudioContext.src != this.data.src) {
  63. this.resetAudioContext(this.data.src)
  64. }*/
  65. console.log('playAction',this.data.isPlay)
  66. if (this.data.isPlay) {
  67. this.stop()
  68. } else {
  69. AudioContext.play()
  70. }
  71. },
  72. formatTimer: function (currentTime) {
  73. const minutes = parseInt(currentTime / 60)
  74. const seconds = parseInt(currentTime % 60)
  75. var time = ''
  76. if (minutes > 9) {
  77. time += minutes
  78. } else {
  79. time += '0' + minutes
  80. }
  81. time += ':'
  82. if (seconds > 9) {
  83. time += seconds
  84. } else {
  85. time += '0' + seconds
  86. }
  87. return time
  88. },
  89. resetAudioContext(src) {
  90. AudioContext.stop()
  91. AudioContext.destroy()
  92. AudioContext = wx.createInnerAudioContext()
  93. wx.setInnerAudioOption({
  94. obeyMuteSwitch: false
  95. })
  96. AudioContext.offCanplay()
  97. AudioContext.offPlay()
  98. AudioContext.offEnded()
  99. AudioContext.offStop()
  100. AudioContext.offTimeUpdate()
  101. AudioContext.offError()
  102. this.canPlay = false
  103. this.setData({
  104. isPlay: false,
  105. progress: 0,
  106. formatCurrent: '00:00',
  107. formatDuration: '00:00'
  108. })
  109. const that = this
  110. AudioContext.onCanplay(() => {
  111. console.log("onCanPlay", AudioContext, AudioContext.duration)
  112. that.canPlay = true
  113. setTimeout(function () {
  114. const formatDuration = that.formatTimer(AudioContext.duration)
  115. that.setData({
  116. formatCurrent: '00:00',
  117. formatDuration: formatDuration
  118. })
  119. }, 500)
  120. })
  121. AudioContext.onPlay(() => {
  122. console.log("onPlay")
  123. that.setData({
  124. isPlay: true,
  125. })
  126. })
  127. AudioContext.onEnded(() => {
  128. console.log("onEnded")
  129. const formatCurrent = that.formatTimer(AudioContext.duration)
  130. that.setData({
  131. isPlay: false,
  132. formatCurrent: formatCurrent,
  133. progress: 100
  134. })
  135. })
  136. AudioContext.onStop(() => {
  137. console.log("onStop")
  138. that.setData({
  139. isPlay: false,
  140. formatCurrent: '00:00',
  141. progress: 0
  142. })
  143. })
  144. AudioContext.onTimeUpdate(() => {
  145. console.log("onTimeUpdate")
  146. const formatCurrent = that.formatTimer(AudioContext.currentTime)
  147. const formatDuration = that.formatTimer(AudioContext.duration)
  148. that.setData({
  149. progress: parseInt(100 * AudioContext.currentTime / AudioContext.duration),
  150. formatCurrent: formatCurrent,
  151. formatDuration: formatDuration
  152. })
  153. })
  154. AudioContext.onError(error => {
  155. if (this.fetchDurationHandler) {
  156. clearInterval(this.fetchDurationHandler)
  157. }
  158. console.log("onError", error)
  159. var errMessage = ''
  160. switch (error.errCode) {
  161. case 10001:
  162. errMessage = '系统错误'
  163. break;
  164. case 10002:
  165. errMessage = '网络错误'
  166. break;
  167. case 10003:
  168. errMessage = '文件错误'
  169. break;
  170. case 10004:
  171. errMessage = '格式错误'
  172. break;
  173. default:
  174. errMessage = '未知错误'
  175. break;
  176. }
  177. console.log(errMessage)
  178. wx.showToast({
  179. title: errMessage,
  180. icon: 'warn',
  181. duration: 2500
  182. })
  183. that.setData({
  184. isPlay: false,
  185. formatCurrent: '00:00',
  186. formatDuration: '00:00',
  187. progress: 0
  188. })
  189. if (that.audioCallback) {
  190. that.audioCallback(AudioContext.src)
  191. }
  192. })
  193. if (this.data.src) {
  194. AudioContext.src = this.data.src
  195. }
  196. AudioContext.autoplay = this.data.auto
  197. },
  198. audioCallback: function (callback) {
  199. this.audioCallback = callback
  200. }
  201. },
  202. attached() {
  203. this.resetAudioContext()
  204. const that = this
  205. wx.onAudioInterruptionBegin(function () {
  206. console.log('player onAudioInterruptionBegin')
  207. if (that.data.isPlay) {
  208. that.stop()
  209. }
  210. })
  211. wx.onAppHide(function () {
  212. console.log('player onAppHide')
  213. console.log(that.data.isPlay)
  214. if (that.data.isPlay) {
  215. that.stop()
  216. }
  217. })
  218. wx.onAppShow(function () {
  219. console.log("player onAppShow")
  220. if (that.data.src && !that.canPlay) {
  221. that.resetAudioContext(that.data.src)
  222. }
  223. })
  224. },
  225. detached() {
  226. AudioContext.stop()
  227. AudioContext.destroy()
  228. wx.offAudioInterruptionBegin(() => {
  229. console.log('player offAudioInterruptionBegin')
  230. })
  231. wx.offAppHide(() => {
  232. console.log('player offAppHide')
  233. })
  234. }
  235. })