audio.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view v-if="controls" @click="onClick" class="_contain">
  3. <!-- 海报和按钮 -->
  4. <view class="_poster" :style="'background-image:url('+poster+')'">
  5. <view class="_button" @tap="_buttonTap">
  6. <view :class="playing?'_pause':'_play'" />
  7. </view>
  8. </view>
  9. <!-- 曲名和作者 -->
  10. <view class="_title">
  11. <view class="_name">{{name||'未知音频'}}</view>
  12. <view class="_author">{{author||'未知作者'}}</view>
  13. </view>
  14. <!-- 进度条 -->
  15. <slider class="_slider" activeColor="#585959" block-size="12" handle-size="12" :disabled="error" :value="value" @changing="_seeking" @change="_seeked" />
  16. <!--播放时间-->
  17. <view class="_time">{{time||'00:00'}}</view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * @fileoverview audio 组件
  23. */
  24. import context from './context'
  25. export default {
  26. data () {
  27. return {
  28. error: false,
  29. playing: false,
  30. time: '00:00',
  31. value: 0
  32. }
  33. },
  34. props: {
  35. aid: String,
  36. name: String, // 音乐名
  37. author: String, // 作者
  38. poster: String, // 海报图片地址
  39. autoplay: [Boolean, String], // 是否自动播放
  40. controls: [Boolean, String], // 是否显示控件
  41. loop: [Boolean, String], // 是否循环播放
  42. src: String // 源地址
  43. },
  44. watch: {
  45. src (src) {
  46. this.setSrc(src)
  47. }
  48. },
  49. mounted () {
  50. this._ctx = uni.createInnerAudioContext()
  51. this._ctx.onError((err) => {
  52. this.error = true
  53. this.$emit('error', err)
  54. })
  55. this._ctx.onTimeUpdate(() => {
  56. const time = this._ctx.currentTime
  57. const min = parseInt(time / 60)
  58. const sec = Math.ceil(time % 60)
  59. this.time = (min > 9 ? min : '0' + min) + ':' + (sec > 9 ? sec : '0' + sec)
  60. if (!this.lastTime) {
  61. this.value = time / this._ctx.duration * 100 // 不在拖动状态下
  62. }
  63. })
  64. this._ctx.onEnded(() => {
  65. if (!this.loop) {
  66. this.playing = false
  67. }
  68. })
  69. context.set(this.aid, this)
  70. this.setSrc(this.src)
  71. },
  72. // #ifdef VUE2
  73. beforeDestroy () {
  74. this._ctx.destroy()
  75. context.remove(this.aid)
  76. },
  77. // #endif
  78. // #ifdef VUE3
  79. beforeUnmount() {
  80. this._ctx.destroy()
  81. context.remove(this.aid)
  82. },
  83. // #endif
  84. onPageShow () {
  85. if (this.playing && this._ctx.paused) {
  86. this._ctx.play()
  87. }
  88. },
  89. methods: {
  90. // 设置源
  91. setSrc (src) {
  92. this._ctx.autoplay = this.autoplay
  93. this._ctx.loop = this.loop
  94. this._ctx.src = src
  95. if (this.autoplay && !this.playing) {
  96. this.playing = true
  97. }
  98. },
  99. // 播放
  100. play () {
  101. this._ctx.play()
  102. this.playing = true
  103. this.$emit('play', {
  104. target: {
  105. id: this.aid
  106. }
  107. })
  108. },
  109. // 暂停
  110. pause () {
  111. this._ctx.pause()
  112. this.playing = false
  113. this.$emit('pause')
  114. },
  115. // 设置播放速率
  116. playbackRate (rate) {
  117. this._ctx.playbackRate = rate
  118. },
  119. // 移动进度条
  120. seek (sec) {
  121. this._ctx.seek(sec)
  122. },
  123. // 内部方法
  124. _buttonTap () {
  125. if (this.playing) this.pause()
  126. else this.play()
  127. },
  128. _seeking (e) {
  129. // 避免过于频繁 setData
  130. if (e.timeStamp - this.lastTime < 200) return
  131. const time = Math.round(e.detail.value / 100 * this._ctx.duration)
  132. const min = parseInt(time / 60)
  133. const sec = time % 60
  134. this.time = (min > 9 ? min : '0' + min) + ':' + (sec > 9 ? sec : '0' + sec)
  135. this.lastTime = e.timeStamp
  136. },
  137. _seeked (e) {
  138. this.seek(e.detail.value / 100 * this._ctx.duration)
  139. this.lastTime = undefined
  140. },
  141. onClick(e) {
  142. this.$emit('onClick', e)
  143. }
  144. }
  145. }
  146. </script>
  147. <style>
  148. /* 顶层容器 */
  149. ._contain {
  150. position: relative;
  151. display: inline-flex;
  152. width: 290px;
  153. background-color: #fcfcfc;
  154. border: 1px solid #e0e0e0;
  155. border-radius: 2px;
  156. }
  157. /* 播放、暂停按钮 */
  158. ._button {
  159. display: flex;
  160. align-items: center;
  161. justify-content: center;
  162. width: 20px;
  163. height: 20px;
  164. overflow: hidden;
  165. background-color: rgb(0, 0, 0, 0.2);
  166. border: 1px solid white;
  167. border-radius: 50%;
  168. opacity: 0.9;
  169. }
  170. ._play {
  171. margin-left: 2px;
  172. border-top: 4px solid transparent;
  173. border-bottom: 4px solid transparent;
  174. border-left: 8px solid white;
  175. }
  176. ._pause {
  177. width: 8px;
  178. height: 8px;
  179. background-color: white;
  180. }
  181. /* 海报 */
  182. ._poster {
  183. display: flex;
  184. align-items: center;
  185. justify-content: center;
  186. width: 70px;
  187. height: 70px;
  188. background-color: #e6e6e6;
  189. background-size: contain;
  190. }
  191. /* 标题栏 */
  192. ._title {
  193. flex: 1;
  194. margin: 4px 0 0 14px;
  195. text-align: left;
  196. }
  197. ._author {
  198. width: 45px;
  199. font-size: 12px;
  200. color: #888;
  201. }
  202. ._name {
  203. width: 140px;
  204. font-size: 15px;
  205. line-height: 39px;
  206. }
  207. ._author,
  208. ._name {
  209. overflow: hidden;
  210. text-overflow: ellipsis;
  211. white-space: nowrap;
  212. }
  213. /* 进度条 */
  214. ._slider {
  215. position: absolute;
  216. right: 16px;
  217. bottom: 8px;
  218. width: 140px;
  219. margin: 0;
  220. }
  221. /* 播放时间 */
  222. ._time {
  223. margin: 7px 14px 0 0;
  224. font-size: 12px;
  225. color: #888;
  226. }
  227. /* 响应式布局,大屏幕用更大的尺寸 */
  228. @media (min-width: 400px) {
  229. ._contain {
  230. width: 380px;
  231. }
  232. ._button {
  233. width: 26px;
  234. height: 26px;
  235. }
  236. ._poster {
  237. width: 90px;
  238. height: 90px;
  239. }
  240. ._author {
  241. width: 60px;
  242. font-size: 15px;
  243. }
  244. ._name {
  245. width: 180px;
  246. font-size: 19px;
  247. line-height: 55px;
  248. }
  249. ._slider {
  250. right: 20px;
  251. bottom: 10px;
  252. width: 180px;
  253. }
  254. ._time {
  255. font-size: 15px;
  256. }
  257. }
  258. </style>