audio.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. beforeDestroy () {
  73. this._ctx.destroy()
  74. context.remove(this.aid)
  75. },
  76. onPageShow () {
  77. if (this.playing && this._ctx.paused) {
  78. this._ctx.play()
  79. }
  80. },
  81. methods: {
  82. // 设置源
  83. setSrc (src) {
  84. this._ctx.autoplay = this.autoplay
  85. this._ctx.loop = this.loop
  86. this._ctx.src = src
  87. if (this.autoplay && !this.playing) {
  88. this.playing = true
  89. }
  90. },
  91. // 播放
  92. play () {
  93. this._ctx.play()
  94. this.playing = true
  95. this.$emit('play', {
  96. target: {
  97. id: this.aid
  98. }
  99. })
  100. },
  101. // 暂停
  102. pause () {
  103. this._ctx.pause()
  104. this.playing = false
  105. this.$emit('pause')
  106. },
  107. // 设置播放速率
  108. playbackRate (rate) {
  109. this._ctx.playbackRate = rate
  110. },
  111. // 移动进度条
  112. seek (sec) {
  113. this._ctx.seek(sec)
  114. },
  115. // 内部方法
  116. _buttonTap () {
  117. if (this.playing) this.pause()
  118. else this.play()
  119. },
  120. _seeking (e) {
  121. // 避免过于频繁 setData
  122. if (e.timeStamp - this.lastTime < 200) return
  123. const time = Math.round(e.detail.value / 100 * this._ctx.duration)
  124. const min = parseInt(time / 60)
  125. const sec = time % 60
  126. this.time = (min > 9 ? min : '0' + min) + ':' + (sec > 9 ? sec : '0' + sec)
  127. this.lastTime = e.timeStamp
  128. },
  129. _seeked (e) {
  130. this.seek(e.detail.value / 100 * this._ctx.duration)
  131. this.lastTime = undefined
  132. },
  133. onClick(e) {
  134. this.$emit('onClick', e)
  135. }
  136. }
  137. }
  138. </script>
  139. <style>
  140. /* 顶层容器 */
  141. ._contain {
  142. position: relative;
  143. display: inline-flex;
  144. width: 290px;
  145. background-color: #fcfcfc;
  146. border: 1px solid #e0e0e0;
  147. border-radius: 2px;
  148. }
  149. /* 播放、暂停按钮 */
  150. ._button {
  151. display: flex;
  152. align-items: center;
  153. justify-content: center;
  154. width: 20px;
  155. height: 20px;
  156. overflow: hidden;
  157. background-color: rgb(0, 0, 0, 0.2);
  158. border: 1px solid white;
  159. border-radius: 50%;
  160. opacity: 0.9;
  161. }
  162. ._play {
  163. margin-left: 2px;
  164. border-top: 4px solid transparent;
  165. border-bottom: 4px solid transparent;
  166. border-left: 8px solid white;
  167. }
  168. ._pause {
  169. width: 8px;
  170. height: 8px;
  171. background-color: white;
  172. }
  173. /* 海报 */
  174. ._poster {
  175. display: flex;
  176. align-items: center;
  177. justify-content: center;
  178. width: 70px;
  179. height: 70px;
  180. background-color: #e6e6e6;
  181. background-size: contain;
  182. }
  183. /* 标题栏 */
  184. ._title {
  185. flex: 1;
  186. margin: 4px 0 0 14px;
  187. text-align: left;
  188. }
  189. ._author {
  190. width: 45px;
  191. font-size: 12px;
  192. color: #888;
  193. }
  194. ._name {
  195. width: 140px;
  196. font-size: 15px;
  197. line-height: 39px;
  198. }
  199. ._author,
  200. ._name {
  201. overflow: hidden;
  202. text-overflow: ellipsis;
  203. white-space: nowrap;
  204. }
  205. /* 进度条 */
  206. ._slider {
  207. position: absolute;
  208. right: 16px;
  209. bottom: 8px;
  210. width: 140px;
  211. margin: 0;
  212. }
  213. /* 播放时间 */
  214. ._time {
  215. margin: 7px 14px 0 0;
  216. font-size: 12px;
  217. color: #888;
  218. }
  219. /* 响应式布局,大屏幕用更大的尺寸 */
  220. @media (min-width: 400px) {
  221. ._contain {
  222. width: 380px;
  223. }
  224. ._button {
  225. width: 26px;
  226. height: 26px;
  227. }
  228. ._poster {
  229. width: 90px;
  230. height: 90px;
  231. }
  232. ._author {
  233. width: 60px;
  234. font-size: 15px;
  235. }
  236. ._name {
  237. width: 180px;
  238. font-size: 19px;
  239. line-height: 55px;
  240. }
  241. ._slider {
  242. right: 20px;
  243. bottom: 10px;
  244. width: 180px;
  245. }
  246. ._time {
  247. font-size: 15px;
  248. }
  249. }
  250. </style>