Line data Source code
1 16 : <template>
2 : <div class="relative" @pointerdown.stop>
3 : <div
4 : v-if="!hideWhenEmpty || modelValue"
5 : class="relative flex h-16 w-full items-center gap-4 rounded-lg bg-input px-4 py-0"
6 : >
7 : <!-- Hidden audio element -->
8 : <audio
9 : ref="audioRef"
10 : :src="modelValue"
11 : data-testid="audio-preview"
12 : @loadedmetadata="handleLoadedMetadata"
13 : @timeupdate="handleTimeUpdate"
14 : @ended="handleEnded"
15 : @play="handlePlay"
16 : @pause="handlePause"
17 : @error="handleError"
18 : />
19 :
20 : <!-- Left Actions -->
21 : <div class="relative flex shrink-0 items-center gap-2">
22 : <!-- Play/Pause Button -->
23 : <Button
24 : variant="muted-textonly"
25 : size="unset"
26 : :aria-label="$t('g.playPause')"
27 : :aria-pressed="isPlaying"
28 : class="size-6 rounded-sm"
29 : @click="togglePlayPause"
30 : >
31 : <i v-if="!isPlaying" class="icon-[lucide--play]" />
32 : <i v-else class="icon-[lucide--pause]" />
33 : </Button>
34 :
35 : <!-- Time Display -->
36 : <div class="text-sm font-normal text-nowrap text-foreground">
37 : {{ formatTime(currentTime) }} / {{ formatTime(duration) }}
38 : </div>
39 : </div>
40 :
41 : <!-- Progress Bar -->
42 : <div class="relative h-0.5 flex-1 rounded-full bg-border">
43 : <div
44 : class="absolute top-0 left-0 h-full rounded-full bg-muted-foreground transition-all"
45 : :style="{ width: `${progressPercentage}%` }"
46 : />
47 : <input
48 : type="range"
49 : :value="progressPercentage"
50 : min="0"
51 : max="100"
52 : step="0.1"
53 : :aria-label="$t('g.audioProgress')"
54 : class="absolute inset-0 w-full cursor-pointer opacity-0"
55 : @input="handleSeek"
56 : />
57 : </div>
58 :
59 : <!-- Right Actions -->
60 : <div class="relative flex shrink-0 items-center gap-2">
61 : <!-- Volume Button -->
62 : <Button
63 : variant="muted-textonly"
64 : size="unset"
65 : :aria-label="$t('g.volume')"
66 : class="size-6 rounded-sm"
67 : @click="toggleMute"
68 : >
69 : <i v-if="showVolumeTwo" class="icon-[lucide--volume-2]" />
70 : <i v-else-if="showVolumeOne" class="icon-[lucide--volume-1]" />
71 : <i v-else class="icon-[lucide--volume-x]" />
72 : </Button>
73 :
74 : <!-- Download Button -->
75 : <Button
76 : v-if="modelValue"
77 : size="icon-sm"
78 : variant="muted-textonly"
79 : :aria-label="$t('g.downloadAudio')"
80 : :title="$t('g.downloadAudio')"
81 : class="size-6"
82 : @click="handleDownload"
83 : >
84 : <i class="icon-[lucide--download]" />
85 : </Button>
86 :
87 : <!-- Options Button -->
88 : <Menu v-if="showOptionsButton" :items="menuItems">
89 : <template #trigger>
90 : <Button
91 : variant="muted-textonly"
92 : size="unset"
93 : :aria-label="$t('g.moreOptions')"
94 : class="size-6 rounded-sm"
95 : >
96 : <i class="icon-[lucide--more-vertical]" />
97 : </Button>
98 : </template>
99 : <template #item="{ item, hasSubmenu }">
100 : <div v-if="item.key === 'volume'" class="w-48">
101 : <label class="mb-2 block text-xs text-foreground">{{
102 : item.label
103 : }}</label>
104 : <Slider
105 : :model-value="[volume * 10]"
106 : :min="0"
107 : :max="10"
108 : :step="1"
109 : @update:model-value="handleVolumeChange"
110 : />
111 : </div>
112 : <MenuItemContent v-else :item :has-submenu />
113 : </template>
114 : </Menu>
115 : </div>
116 : </div>
117 : </div>
118 : </template>
119 :
120 : <script setup lang="ts">
121 : import { computed, onUnmounted, ref, useTemplateRef, watch } from 'vue'
122 : import { useI18n } from 'vue-i18n'
123 :
124 : import { reportError } from '@comfyorg/telemetry/reportError'
125 :
126 : import { useToast } from '@/components/ui/toast/toastStore'
127 : import { createToastId } from '@/types/toastId'
128 :
129 : import { downloadFile } from '@/base/common/downloadUtil'
130 : import Button from '@comfyorg/design-system/button/Button'
131 : import Menu from '@comfyorg/design-system/menu/Menu'
132 : import MenuItemContent from '@comfyorg/design-system/menu/MenuItemContent'
133 : import Slider from '@comfyorg/design-system/slider/Slider'
134 :
135 : import { formatTime } from '@/utils/formatUtil'
136 : import { reloadFailedMedia } from '@/utils/mediaReloadUtil'
137 :
138 10 : const { t } = useI18n()
139 10 : const toast = useToast()
140 :
141 : const { hideWhenEmpty = true, showOptionsButton } = defineProps<{
142 : hideWhenEmpty?: boolean
143 : showOptionsButton?: boolean
144 : }>()
145 :
146 : // Refs
147 10 : const audioRef = useTemplateRef('audioRef')
148 10 : const isPlaying = ref(false)
149 10 : const isMuted = ref(false)
150 10 : const volume = ref(1)
151 10 : const currentTime = ref(0)
152 10 : const duration = ref(0)
153 10 : const playbackRate = ref(1)
154 :
155 : // Computed
156 10 : const progressPercentage = computed(() => {
157 15 : if (!duration.value || duration.value === 0) return 0
158 6 : return (currentTime.value / duration.value) * 100
159 : })
160 10 : const modelValue = defineModel<string>()
161 :
162 10 : const showVolumeTwo = computed(() => !isMuted.value && volume.value > 0.5)
163 10 : const showVolumeOne = computed(() => !isMuted.value && volume.value > 0)
164 :
165 : // Playback controls
166 10 : let playAttempt = 0
167 10 : const playbackErrorToastId = createToastId()
168 :
169 10 : onUnmounted(() => {
170 2 : playAttempt++
171 2 : toast.dismiss(playbackErrorToastId)
172 : })
173 :
174 10 : const togglePlayPause = async () => {
175 3 : const audio = audioRef.value
176 3 : if (!audio || !audio.src) {
177 0 : return
178 : }
179 :
180 3 : const attempt = ++playAttempt
181 3 : if (isPlaying.value) {
182 1 : audio.pause()
183 1 : isPlaying.value = false
184 1 : toast.dismiss(playbackErrorToastId)
185 1 : return
186 : }
187 :
188 2 : isPlaying.value = true
189 3 : try {
190 3 : if (audio.error) reloadFailedMedia(audio)
191 2 : await audio.play()
192 2 : if (attempt === playAttempt) toast.dismiss(playbackErrorToastId)
193 : } catch (error) {
194 0 : if (attempt !== playAttempt) return
195 0 : isPlaying.value = false
196 0 : if (error instanceof DOMException && error.name === 'AbortError') {
197 0 : toast.dismiss(playbackErrorToastId)
198 0 : return
199 : }
200 0 : showPlaybackError(error)
201 : }
202 : }
203 :
204 0 : function showPlaybackError(error: unknown) {
205 0 : reportError(error, {
206 : errorType: 'error_playing_audio',
207 : surface: 'graph',
208 : level: 'warning'
209 : })
210 0 : toast.error(t('g.error'), {
211 : id: playbackErrorToastId,
212 : description: t('g.audioPlaybackFailed')
213 : })
214 : }
215 :
216 10 : const handleDownload = () => {
217 1 : if (!modelValue.value) return
218 1 : try {
219 1 : downloadFile(modelValue.value)
220 : } catch {
221 0 : toast.error(t('g.error'), { description: t('g.failedToDownloadFile') })
222 : }
223 : }
224 :
225 10 : const toggleMute = () => {
226 2 : if (audioRef.value) {
227 2 : isMuted.value = !isMuted.value
228 2 : audioRef.value.muted = isMuted.value
229 : }
230 : }
231 :
232 10 : const handleSeek = (event: Event) => {
233 0 : const target = event.target as HTMLInputElement
234 0 : const value = parseFloat(target.value)
235 0 : if (audioRef.value && duration.value > 0) {
236 0 : const newTime = (value / 100) * duration.value
237 0 : audioRef.value.currentTime = newTime
238 0 : currentTime.value = newTime
239 : }
240 : }
241 :
242 : // Audio events
243 10 : const handleLoadedMetadata = () => {
244 5 : if (audioRef.value) {
245 5 : duration.value = audioRef.value.duration
246 : }
247 : }
248 :
249 10 : const handleTimeUpdate = () => {
250 1 : if (audioRef.value) {
251 1 : currentTime.value = audioRef.value.currentTime
252 : }
253 : }
254 :
255 10 : const handlePlay = () => {
256 2 : if (!audioRef.value || audioRef.value.paused) return
257 2 : isPlaying.value = true
258 2 : toast.dismiss(playbackErrorToastId)
259 : }
260 :
261 10 : const handlePause = () => {
262 1 : if (audioRef.value?.paused) isPlaying.value = false
263 : }
264 :
265 10 : const toPlaybackError = (error: MediaError | null | undefined) =>
266 : new Error(
267 : `Audio playback failed (MediaError ${error?.code ?? 'unknown'}): ${
268 : error?.message || 'no message'
269 : }`
270 : )
271 :
272 10 : const handleError = () => {
273 3 : if (!isPlaying.value) return
274 0 : playAttempt++
275 0 : isPlaying.value = false
276 3 : showPlaybackError(toPlaybackError(audioRef.value?.error))
277 : }
278 :
279 10 : const handleEnded = () => {
280 0 : isPlaying.value = false
281 0 : currentTime.value = 0
282 : }
283 :
284 10 : const setPlaybackSpeed = (speed: number) => {
285 0 : playbackRate.value = speed
286 0 : if (audioRef.value) {
287 0 : audioRef.value.playbackRate = speed
288 : }
289 : }
290 :
291 10 : const handleVolumeChange = (value: number[] | undefined) => {
292 0 : const numValue = value?.[0] ?? 0
293 0 : volume.value = numValue / 10
294 0 : if (audioRef.value) {
295 0 : audioRef.value.volume = volume.value
296 0 : if (volume.value > 0 && isMuted.value) {
297 0 : isMuted.value = false
298 0 : audioRef.value.muted = false
299 : }
300 : }
301 : }
302 :
303 10 : const menuItems = computed(() => [
304 : {
305 : label: t('g.playbackSpeed'),
306 : radioGroup: {
307 : value: () => String(playbackRate.value),
308 : options: [
309 : {
310 : value: '0.5',
311 : label: t('g.halfSpeed'),
312 : command: () => setPlaybackSpeed(0.5)
313 : },
314 : {
315 : value: '1',
316 : label: t('g.1x'),
317 : command: () => setPlaybackSpeed(1)
318 : },
319 : {
320 : value: '2',
321 : label: t('g.2x'),
322 : command: () => setPlaybackSpeed(2)
323 : }
324 : ]
325 : }
326 : },
327 : {
328 : label: t('g.volume'),
329 : key: 'volume'
330 : }
331 : ])
332 :
333 10 : watch(modelValue, () => {
334 3 : playAttempt++
335 3 : isPlaying.value = false
336 3 : toast.dismiss(playbackErrorToastId)
337 3 : audioRef.value?.pause()
338 3 : currentTime.value = 0
339 3 : duration.value = 0
340 : })
341 : </script>
|