audio.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view v-if="controls" 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. }
  134. }
  135. </script>
  136. <style>
  137. /* 顶层容器 */
  138. ._contain {
  139. position: relative;
  140. display: inline-flex;
  141. width: 290px;
  142. background-color: #fcfcfc;
  143. border: 1px solid #e0e0e0;
  144. border-radius: 2px;
  145. }
  146. /* 播放、暂停按钮 */
  147. ._button {
  148. display: flex;
  149. align-items: center;
  150. justify-content: center;
  151. width: 20px;
  152. height: 20px;
  153. overflow: hidden;
  154. background-color: rgb(0, 0, 0, 0.2);
  155. border: 1px solid white;
  156. border-radius: 50%;
  157. opacity: 0.9;
  158. }
  159. ._play {
  160. margin-left: 2px;
  161. border-top: 4px solid transparent;
  162. border-bottom: 4px solid transparent;
  163. border-left: 8px solid white;
  164. }
  165. ._pause {
  166. width: 8px;
  167. height: 8px;
  168. background-color: white;
  169. }
  170. /* 海报 */
  171. ._poster {
  172. display: flex;
  173. align-items: center;
  174. justify-content: center;
  175. width: 70px;
  176. height: 70px;
  177. background-color: #e6e6e6;
  178. background-size: contain;
  179. }
  180. /* 标题栏 */
  181. ._title {
  182. flex: 1;
  183. margin: 4px 0 0 14px;
  184. text-align: left;
  185. }
  186. ._author {
  187. width: 45px;
  188. font-size: 12px;
  189. color: #888;
  190. }
  191. ._name {
  192. width: 140px;
  193. font-size: 15px;
  194. line-height: 39px;
  195. }
  196. ._author,
  197. ._name {
  198. overflow: hidden;
  199. text-overflow: ellipsis;
  200. white-space: nowrap;
  201. }
  202. /* 进度条 */
  203. ._slider {
  204. position: absolute;
  205. right: 16px;
  206. bottom: 8px;
  207. width: 140px;
  208. margin: 0;
  209. }
  210. /* 播放时间 */
  211. ._time {
  212. margin: 7px 14px 0 0;
  213. font-size: 12px;
  214. color: #888;
  215. }
  216. /* 响应式布局,大屏幕用更大的尺寸 */
  217. @media (min-width: 400px) {
  218. ._contain {
  219. width: 380px;
  220. }
  221. ._button {
  222. width: 26px;
  223. height: 26px;
  224. }
  225. ._poster {
  226. width: 90px;
  227. height: 90px;
  228. }
  229. ._author {
  230. width: 60px;
  231. font-size: 15px;
  232. }
  233. ._name {
  234. width: 180px;
  235. font-size: 19px;
  236. line-height: 55px;
  237. }
  238. ._slider {
  239. right: 20px;
  240. bottom: 10px;
  241. width: 180px;
  242. }
  243. ._time {
  244. font-size: 15px;
  245. }
  246. }
  247. </style>