list-audio.js 3.6 KB

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