list-audio.js 3.6 KB

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