当前位置:   article > 正文

uniapp 小程序音乐播放器_uni 音乐播放器

uni 音乐播放器
  1. <template>
  2. <view class="wrapper">
  3. <view class="audio-content">
  4. <view class="audio-control">
  5. <view class="audio-control-disk">
  6. <image
  7. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-23/d1251e95528a476f89b1e24cd64d076d.png"
  8. mode="scaleToFill"></image>
  9. </view>
  10. <view class="audio-control-image">
  11. <image
  12. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-16/f1021faf16fa4c969ad88db3c114576a.jpg"
  13. mode="scaleToFill"></image>
  14. </view>
  15. </view>
  16. <view class="audio-progress-bar">
  17. <u-line-progress :percentage="percentage" :showText="false" activeColor="#ffffff"
  18. inactiveColor="rgba(255,255,255,0.25)"></u-line-progress>
  19. <view class="progress-time">
  20. {{ parse(currentTime) }}/{{ parse(duration) }}
  21. </view>
  22. </view>
  23. <view class="pay-control">
  24. <view class="pay-control-left">
  25. <image
  26. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-23/7019e4ef4ba244ffada9c5d38b2e3b45.png"
  27. mode="scaleToFill"
  28. @click="onSeek(1)"
  29. />
  30. </view>
  31. <view class="pay-control-middle">
  32. <image
  33. v-show="paused"
  34. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-23/cd5b7c1b1c6247a389bcdd0cab9cec49.png"
  35. mode="scaleToFill"
  36. @click="onPause"
  37. />
  38. <image
  39. v-show="!paused"
  40. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-23/19f4c4c65ecd43b8a5f403f348e15ae7.png"
  41. mode="scaleToFill"
  42. @click="onPause"
  43. />
  44. </view>
  45. <view class="pay-control-right">
  46. <image
  47. src="https://yizhen-mamapai-test.oss-cn-zhangjiakou.aliyuncs.com/certification/2023-08-23/c3191ca8e12f4ea6ba3d5fbfad066734.png"
  48. mode="scaleToFill"
  49. @click="onSeek(2)"
  50. />
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. export default {
  58. data() {
  59. return {
  60. audioSrc: require('../../static/LoveOnTheBrain.mp3'),
  61. duration: 0,
  62. currentTime: 0,
  63. audio: null,
  64. percentage: 0,
  65. paused: true
  66. };
  67. },
  68. mounted() {
  69. // 初始化audio
  70. this.initAudio()
  71. },
  72. methods: {
  73. // 初始化audio
  74. initAudio() {
  75. this.audio = uni.createInnerAudioContext()
  76. this.audio.src = this.audioSrc
  77. // 监听audio进度更新
  78. this.audio.onTimeUpdate(() => {
  79. const currentTime = Math.floor(this.audio.currentTime)
  80. this.setTimePercent(currentTime)
  81. this.currentTime = this.audio.currentTime
  82. })
  83. // 监听audio快进或快退
  84. this.audio.onSeeking(() => {
  85. const currentTime = Math.floor(this.audio.currentTime)
  86. this.setTimePercent(currentTime)
  87. this.currentTime = this.audio.currentTime
  88. })
  89. // 监听audio初始化完成后, 获取音频长度
  90. this.audio.onCanplay(() => {
  91. const interval = setInterval(() => {
  92. if (this.audio.duration !== 0) {
  93. clearInterval(interval)
  94. this.duration = Math.floor(this.audio.duration)
  95. }
  96. }, 100)
  97. })
  98. this.audio.onPlay(() => {
  99. this.paused = false
  100. })
  101. this.audio.onPause(() => {
  102. this.paused = true
  103. })
  104. },
  105. // 销毁audio实例
  106. destroyAudio() {
  107. this.duration = 0
  108. this.currentTime = 0
  109. this.percentage = 0
  110. this.audio.onPlay()
  111. this.audio.onPause()
  112. this.audio.offTimeUpdate()
  113. this.audio.offCanplay()
  114. this.audio.offSeeking()
  115. this.audio.destroy()
  116. },
  117. /**
  118. * description 切换音乐的时候调用
  119. * @param {String} src 图片地址
  120. */
  121. checkAudio(src) {
  122. this.audioSrc = src
  123. this.destroyAudio()
  124. this.initAudio()
  125. },
  126. // 暂停/播放
  127. onPause() {
  128. this.audio.paused ? this.audio.play() : this.audio.pause()
  129. },
  130. /**
  131. * @description 快进/快退
  132. * @param {Enum} type
  133. * 1: 快退
  134. * 2: 快进
  135. */
  136. onSeek(type) {
  137. if (this.currentTime <= 0) {
  138. this.currentTime === 0
  139. this.audio.seek(0)
  140. return
  141. } else if (this.currentTime >= this.duration) {
  142. this.currentTime = this.duration
  143. this.audio.seek(this.currentTime)
  144. return
  145. }
  146. type === 1 && this.audio.seek(this.currentTime - 2)
  147. type === 2 && this.audio.seek(this.currentTime + 2)
  148. },
  149. /**
  150. * description 设置时间格式
  151. * @param {number} number
  152. */
  153. parse(number) {
  154. const m = parseInt(number / 60)
  155. const s = parseInt(number % 60)
  156. const _m = m > 10 ? m : '0' + m
  157. const _s = s > 10 ? s : '0' + s
  158. return _m + ':' + _s
  159. },
  160. /**
  161. * @description 计算进度条显示比例
  162. * @param {number} currentTime
  163. */
  164. setTimePercent(currentTime) {
  165. this.percentage = parseFloat((currentTime / this.duration * 100).toFixed(2))
  166. }
  167. }
  168. }
  169. </script>
  170. <style lang="scss" scoped>
  171. .wrapper {
  172. width: 100%;
  173. position: relative;
  174. }
  175. .audio-content {
  176. width: 100%;
  177. height: 712rpx;
  178. background: linear-gradient(325deg, #3CB9D5 0%, #19C5BE 100%);
  179. position: relative;
  180. box-sizing: border-box;
  181. .audio-control {
  182. position: absolute;
  183. top: 124rpx;
  184. left: 265rpx;
  185. right: 265rpx;
  186. margin: 0 auto;
  187. z-index: 1;
  188. margin-bottom: 91rpx;
  189. .audio-control-disk {
  190. width: 206rpx;
  191. height: 206rpx;
  192. position: absolute;
  193. top: 7rpx;
  194. left: 72rpx;
  195. z-index: 2;
  196. image {
  197. width: 100%;
  198. height: 100%;
  199. }
  200. }
  201. .audio-control-image {
  202. width: 220rpx;
  203. height: 220rpx;
  204. border-radius: 15rpx;
  205. background-color: #008494;
  206. position: absolute;
  207. top: 0;
  208. left: 0;
  209. right: 0;
  210. margin: 0 auto;
  211. overflow: hidden;
  212. z-index: 3;
  213. image {
  214. width: 100%;
  215. height: 100%;
  216. }
  217. }
  218. }
  219. .audio-progress-bar {
  220. width: 100%;
  221. padding: 0 28rpx;
  222. box-sizing: border-box;
  223. display: flex;
  224. align-items: center;
  225. position: absolute;
  226. bottom: 249rpx;
  227. .progress-time {
  228. font-size: 20rpx;
  229. font-family: PingFangSC-Medium, PingFang SC;
  230. font-weight: 500;
  231. color: #FFFFFF;
  232. margin-left: 18rpx;
  233. }
  234. }
  235. }
  236. .pay-control {
  237. width: 300rpx;
  238. height: 100rpx;
  239. display: flex;
  240. align-items: center;
  241. justify-content: space-between;
  242. position: absolute;
  243. bottom: 30rpx;
  244. left: 50%;
  245. transform: translate(-50%, -50%);
  246. .pay-control-left {
  247. width: 30rpx;
  248. height: 37rpx;
  249. image {
  250. width: 100%;
  251. height: 100%;
  252. }
  253. }
  254. .pay-control-middle {
  255. width: 100rpx;
  256. height: 100rpx;
  257. image {
  258. width: 100%;
  259. height: 100%;
  260. }
  261. }
  262. .pay-control-right {
  263. width: 30rpx;
  264. height: 37rpx;
  265. image {
  266. width: 100%;
  267. height: 100%;
  268. }
  269. }
  270. }
  271. </style>

使用的时候需要销毁最后的音频实例

  1. beforeDestroy() {
  2. this.audio.pause()
  3. },

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号