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