audio-play.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // components/audio-play.js
  2. import { CDN_URL,API_BASE_URL } from '../../config/index';
  3. var AllExample = [];
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. audio: {
  10. type: Object,
  11. value: {},
  12. }
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. cdn_url:CDN_URL,
  19. durationTime: '00:00',
  20. currentTime: '00:00',
  21. isPlay: false,
  22. audioExample: null,
  23. progress: 0,
  24. allAudio: [],
  25. api_base_url:API_BASE_URL,
  26. targetUrl:API_BASE_URL+'/data/'
  27. },
  28. /**
  29. * 组件的方法列表
  30. */
  31. methods: {
  32. startMusic() {
  33. // 全部暂停
  34. AllExample.forEach(simpleAudio => {
  35. // console.log('simpleAudio', simpleAudio)
  36. simpleAudio.pause();
  37. })
  38. // this.data.audioExample.autoplay = true;
  39. // this.data.audioExample.src = this.data.targetUrl+this.properties.audio['audio'];
  40. // this.data.audioExample.play();
  41. // this.setData({
  42. // isPlay: true
  43. // })
  44. // console.log('this.properties.audio', this.properties.audio)
  45. // console.log('this.data', this.data)
  46. // console.log('开始音乐')
  47. this.data.audioExample.autoplay = true;
  48. this.data.audioExample.src = this.data.targetUrl+this.properties.audio['audio'];
  49. this.data.audioExample.play();
  50. this.setData({
  51. isPlay: true
  52. })
  53. this.loadProgress()
  54. },
  55. // 播放中
  56. loadProgress() {
  57. this.data.audioExample.onCanplay((e) => {
  58. this.data.audioExample.duration; //必须写,不然获取不到。。。
  59. var timer = setInterval(() => {
  60. // 只要音频暂停了,定时器(假的)不执行
  61. if (this.data.audioExample.paused) return;
  62. var currentime = this.data.audioExample.currentTime;
  63. var duration = this.data.audioExample.duration;
  64. var currTimeStr = this.formatTime(currentime);
  65. var duraTimeStr = this.formatTime(duration);
  66. var progress = parseInt((this.data.audioExample.currentTime / this.data.audioExample.duration) * 100)
  67. // console.log('progress', progress)
  68. // console.log('this.data.progress', this.data.progress)
  69. if (progress === 100 || isNaN(this.data.progress)) {
  70. clearInterval(timer)
  71. console.log('停止了')
  72. this.setData({
  73. isPlay: false,
  74. progress: 0,
  75. currentTime: "00:00",
  76. })
  77. } else {
  78. // console.log('duraTimeStr', typeof duraTimeStr, duraTimeStr, !!duraTimeStr)
  79. // console.log('currTimeStr', typeof currTimeStr, currTimeStr, !!currTimeStr)
  80. // console.log('this.data.audioExample', this.data.audioExample, this.data.audioExample.paused)
  81. this.setData({
  82. progress,
  83. currentTime: currTimeStr,
  84. durationTime: duraTimeStr
  85. })
  86. }
  87. }, 1000);
  88. });
  89. },
  90. stopMusic() {
  91. this.data.audioExample.pause();
  92. console.log('结束音乐')
  93. this.setData({
  94. isPlay: false
  95. })
  96. },
  97. formatTime: function (num) { //格式化时间格式
  98. num = num.toFixed(0);
  99. let second = (num % 60);
  100. if (second < 10) second = '0' + second;
  101. let min = Math.floor(num / 60);
  102. if (min < 10) min = '0' + min;
  103. return min + ":" + second;
  104. }
  105. },
  106. attached: function () {
  107. //组件显示初始化函数
  108. console.log(3);
  109. var that = this;
  110. this.data.audioExample = wx.createInnerAudioContext();
  111. //所有组件实例都放在一个容器AllExample里
  112. AllExample.push(this.data.audioExample);
  113. this.data.audioExample.onPlay(function () {
  114. console.log('播放中。。。')
  115. })
  116. this.data.audioExample.onTimeUpdate(function () {
  117. console.log('监听进度。。')
  118. })
  119. //控制单个实例的isPlay
  120. this.data.audioExample.onPause(function () {
  121. that.setData({
  122. isPlay: false
  123. })
  124. })
  125. // 每个组件实例播放结束
  126. this.data.audioExample.onEnded(() => {
  127. this.setData({
  128. currentTime: "00:00",
  129. isPlay: false,
  130. progress: 0,
  131. })
  132. console.log('播放结束了')
  133. })
  134. // 在组件实例进入页面节点树时执行
  135. },
  136. detached: function () {
  137. // 在组件实例被从页面节点树移除时执行
  138. // 卸载
  139. this.data.audioExample.destroy();
  140. AllExample = [];
  141. }
  142. })