index.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <div class="flex items-center justify-between px-2 h-72px bg-[var(--el-bg-color-overlay)] b-solid b-1 b-[var(--el-border-color)] b-l-none">
  3. <!-- 歌曲信息 -->
  4. <div class="flex gap-[10px]">
  5. <el-image src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" class="w-[45px]"/>
  6. <div>
  7. <div>我很好</div>
  8. <div class="text-[12px] text-gray-400">刘大壮</div>
  9. </div>
  10. </div>
  11. <!-- 音频controls -->
  12. <div class="flex gap-[12px] items-center">
  13. <Icon icon="majesticons:back-circle" :size="20" class="text-gray-300 cursor-pointer"/>
  14. <Icon :icon="audioProps.paused ? 'mdi:arrow-right-drop-circle' : 'solar:pause-circle-bold'" :size="30" class=" cursor-pointer" @click="toggleStatus('paused')"/>
  15. <Icon icon="majesticons:next-circle" :size="20" class="text-gray-300 cursor-pointer"/>
  16. <div class="flex gap-[16px] items-center">
  17. <span>{{audioProps.currentTime}}</span>
  18. <el-slider v-model="audioProps.duration" color="#409eff" class="w-[160px!important] "/>
  19. <span>{{ audioProps.duration }}</span>
  20. </div>
  21. <!-- 音频 -->
  22. <audio v-bind="audioProps" ref="audioRef" controls v-show="!audioProps" @timeupdate="audioTimeUpdate">
  23. <source :src="response"/>
  24. </audio>
  25. </div>
  26. <!-- 音量控制器 -->
  27. <div class="flex gap-[16px] items-center">
  28. <Icon :icon="audioProps.muted ? 'tabler:volume-off' : 'tabler:volume'" :size="20" class="cursor-pointer" @click="toggleStatus('muted')"/>
  29. <el-slider v-model="audioProps.volume" color="#409eff" class="w-[160px!important] "/>
  30. </div>
  31. </div>
  32. </template>
  33. <script lang="ts" setup>
  34. import { formatPast } from '@/utils/formatTime'
  35. import response from '@/assets/audio/response.mp3'
  36. defineOptions({ name: 'Index' })
  37. const audioRef = ref<Nullable<HTMLElement>>(null)
  38. // 音频相关属性https://www.runoob.com/tags/ref-av-dom.html
  39. const audioProps = reactive({
  40. autoplay: true,
  41. paused: false,
  42. currentTime: '00:00',
  43. duration: '00:00',
  44. muted: false,
  45. volume: 50,
  46. })
  47. function toggleStatus (type: string) {
  48. audioProps[type] = !audioProps[type]
  49. if (type === 'paused' && audioRef.value) {
  50. if (audioProps[type]) {
  51. audioRef.value.pause()
  52. } else {
  53. audioRef.value.play()
  54. }
  55. }
  56. }
  57. // 更新播放位置
  58. function audioTimeUpdate (args) {
  59. audioProps.currentTime = formatPast(new Date(args.timeStamp), 'mm:ss')
  60. }
  61. </script>