|
| 1 | +<script setup lang="ts"> |
| 2 | + import MediaProgress from './MediaProgress.vue' |
| 3 | + import { useTimer } from '@vuetify/v0' |
| 4 | + import { shallowRef, toRef } from 'vue' |
| 5 | +
|
| 6 | + const elapsed = shallowRef(0) |
| 7 | + const buffered = shallowRef(0) |
| 8 | + const playing = shallowRef(false) |
| 9 | + const duration = 180 |
| 10 | + const step = 100 / duration |
| 11 | +
|
| 12 | + function format (seconds: number) { |
| 13 | + const m = Math.floor(seconds / 60) |
| 14 | + const s = Math.floor(seconds % 60) |
| 15 | + return `${m}:${s.toString().padStart(2, '0')}` |
| 16 | + } |
| 17 | +
|
| 18 | + const time = toRef(() => format((elapsed.value / 100) * duration)) |
| 19 | + const total = toRef(() => format(duration)) |
| 20 | +
|
| 21 | + const playback = useTimer(() => { |
| 22 | + elapsed.value = Math.min(elapsed.value + step, 100) |
| 23 | +
|
| 24 | + if (elapsed.value >= 100) { |
| 25 | + playing.value = false |
| 26 | + playback.stop() |
| 27 | + buffer.stop() |
| 28 | + } |
| 29 | + }, { duration: 1000, repeat: true }) |
| 30 | +
|
| 31 | + const buffer = useTimer(() => { |
| 32 | + buffered.value = Math.min(buffered.value + step * 2, 100) |
| 33 | +
|
| 34 | + if (buffered.value >= 100) { |
| 35 | + buffer.stop() |
| 36 | + } |
| 37 | + }, { duration: 350, repeat: true }) |
| 38 | +
|
| 39 | + function toggle () { |
| 40 | + playing.value = !playing.value |
| 41 | +
|
| 42 | + if (playing.value) { |
| 43 | + playback.start() |
| 44 | + if (!buffer.isActive.value) buffer.start() |
| 45 | + } else { |
| 46 | + playback.pause() |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + function reset () { |
| 51 | + elapsed.value = 0 |
| 52 | + buffered.value = 0 |
| 53 | + playing.value = false |
| 54 | + playback.stop() |
| 55 | + buffer.stop() |
| 56 | + } |
| 57 | +</script> |
| 58 | + |
| 59 | +<template> |
| 60 | + <div class="flex flex-col gap-2 w-full rounded-lg bg-surface p-4"> |
| 61 | + <div class="flex items-center gap-3 mb-1"> |
| 62 | + <div class="size-10 rounded bg-surface-variant flex items-center justify-center"> |
| 63 | + <svg class="size-5 text-on-surface-variant" viewBox="0 0 24 24"><path d="M21 3v12.5a3.5 3.5 0 0 1-3.5 3.5a3.5 3.5 0 0 1-3.5-3.5a3.5 3.5 0 0 1 3.5-3.5c.54 0 1.05.12 1.5.34V6.47L9 8.6v8.9A3.5 3.5 0 0 1 5.5 21A3.5 3.5 0 0 1 2 17.5A3.5 3.5 0 0 1 5.5 14c.54 0 1.05.12 1.5.34V4l14-3z" fill="currentColor" /></svg> |
| 64 | + </div> |
| 65 | + |
| 66 | + <div class="flex flex-col"> |
| 67 | + <span class="text-sm font-medium text-on-surface">Midnight Drive</span> |
| 68 | + <span class="text-xs text-on-surface-variant">Synthwave Collection</span> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + |
| 72 | + <MediaProgress v-model="elapsed" :buffer="buffered" /> |
| 73 | + |
| 74 | + <div class="flex items-center justify-between"> |
| 75 | + <span class="text-xs text-on-surface-variant tabular-nums">{{ time }}</span> |
| 76 | + <span class="text-xs text-on-surface-variant tabular-nums">{{ total }}</span> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div class="flex items-center justify-center gap-4"> |
| 80 | + <button |
| 81 | + class="text-on-surface-variant hover:text-on-surface" |
| 82 | + @click="reset" |
| 83 | + > |
| 84 | + <svg class="size-5" viewBox="0 0 24 24"><path d="M6 6h2v12H6zm3.5 6l8.5 6V6z" fill="currentColor" /></svg> |
| 85 | + </button> |
| 86 | + |
| 87 | + <button |
| 88 | + class="size-9 rounded-full bg-on-surface text-surface flex items-center justify-center" |
| 89 | + @click="toggle" |
| 90 | + > |
| 91 | + <svg v-if="playing" class="size-5" viewBox="0 0 24 24"><path d="M14 19h4V5h-4M6 19h4V5H6v14z" fill="currentColor" /></svg> |
| 92 | + <svg v-else class="size-5 ml-0.5" viewBox="0 0 24 24"><path d="M8 5v14l11-7z" fill="currentColor" /></svg> |
| 93 | + </button> |
| 94 | + |
| 95 | + <button |
| 96 | + class="text-on-surface-variant hover:text-on-surface" |
| 97 | + @click="elapsed = 100; buffered = 100" |
| 98 | + > |
| 99 | + <svg class="size-5" viewBox="0 0 24 24"><path d="M16 18h2V6h-2M6 18l8.5-6L6 6v12z" fill="currentColor" /></svg> |
| 100 | + </button> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | +</template> |
0 commit comments