audio.vue 5.1 KB

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