free-audio.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <!-- 音频播放器组件 -->
  3. <view v-if='url' class='flex justify-between align-center audio' >
  4. <view class='mr-3' @click='start(audioId)'>
  5. <image :src='startPic' class='icon' v-show='!status'></image>
  6. <image :src='endPic' class='icon' v-show='status'></image>
  7. </view>
  8. <view class='flex-1'>
  9. <slider
  10. @change='changeAudio'
  11. :activeColor='activeColor'
  12. :min='0'
  13. :max='duration.toFixed(0)'
  14. :value='currentTime.toFixed(0)'
  15. :step='0.1'
  16. backgroundColor="#E9F0F0"
  17. :block-size='14'
  18. block-color='#2BC7B9'>
  19. </slider>
  20. </view>
  21. <view class='ml-3'>{{getTime(Math.round(currentTime))}}</view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. context: null,
  29. currentTime: 0,
  30. duration: 100,
  31. status: false
  32. }
  33. },
  34. props: {
  35. url: String,
  36. activeColor: {
  37. type: String,
  38. default: '#0E7EFC'
  39. },
  40. startPic: String,
  41. endPic: String,
  42. audioId: [String,Number]
  43. },
  44. created() {
  45. this.context = uni.createInnerAudioContext();
  46. this.context.src = this.url;
  47. this.onTimeUpdate();
  48. this.onCanplay();
  49. this.onEnded();
  50. uni.$on('stop',(id)=> {
  51. if(id && id != this.audioId) {
  52. this.context.stop();
  53. this.status = false;
  54. } else if(!id){
  55. this.context.stop();
  56. this.status = false;
  57. }
  58. })
  59. },
  60. methods: {
  61. start(id) { //点击播放
  62. let audioId = id;
  63. if(this.status) {
  64. this.context.pause();
  65. this.status = !this.status;
  66. }else {
  67. uni.$emit('stop',id)
  68. this.context.play()
  69. this.status = !this.status;
  70. }
  71. },
  72. onCanplay() { //进入可播放状态
  73. this.context.onCanplay(() => {
  74. this.context.duration;
  75. setTimeout(()=>{
  76. this.duration = this.context.duration;
  77. },1000)
  78. })
  79. },
  80. onTimeUpdate() { //音频播放进度
  81. this.context.onTimeUpdate(() => {
  82. if (!Number.isFinite( this.context.duration)) {
  83. this.context.currentTime = Number.MAX_SAFE_INTEGER;
  84. this.context.currentTime = 0;
  85. } else {
  86. this.duration = this.context.duration;
  87. this.currentTime = this.context.currentTime;
  88. }
  89. })
  90. },
  91. onEnded() { //播放结束
  92. this.context.onEnded(()=> {
  93. this.status = false;
  94. this.currentTime = 0;
  95. })
  96. },
  97. changeAudio(e) {
  98. let paused = this.context.paused;
  99. this.context.pause();
  100. this.context.seek(e.detail.value)
  101. if(!paused) {
  102. this.context.play();
  103. }
  104. },
  105. getTime(time) {
  106. let m = parseInt(time / 60);
  107. let s = time % 60;
  108. return this.towNum(m) + ':' + this.towNum(s);
  109. },
  110. towNum(num) {
  111. if(num >= 10) {
  112. return num;
  113. }else {
  114. return '0' + num;
  115. }
  116. }
  117. }
  118. }
  119. </script>
  120. <style>
  121. .audio {
  122. background: #F5F7F7;
  123. padding: 30upx 20upx;
  124. }
  125. .icon {
  126. width: 54upx;
  127. height: 54upx;
  128. }
  129. slider{
  130. margin: 0;
  131. }
  132. .flex {
  133. display: flex;
  134. flex-direction: row;
  135. }
  136. .justify-between {
  137. justify-content: between;
  138. }
  139. .align-center {
  140. align-items: center;
  141. }
  142. .flex-1 {
  143. flex: 1;
  144. }
  145. .ml-3 {
  146. margin-left: 20upx;
  147. font-size: 24upx;
  148. font-family: PingFang SC;
  149. font-weight: 500;
  150. color: #666666;
  151. }
  152. .mr-3 {
  153. margin-right: 30upx;
  154. display: flex;
  155. align-items: center;
  156. justify-content: center;
  157. }
  158. </style>