83 lines
1.6 KiB
TypeScript
83 lines
1.6 KiB
TypeScript
|
|
import { ref, computed } from 'vue'
|
||
|
|
|
||
|
|
const audio = ref<HTMLAudioElement | null>(null)
|
||
|
|
const currentSrc = ref<string | null>(null)
|
||
|
|
const currentName = ref('')
|
||
|
|
const playing = ref(false)
|
||
|
|
const currentTime = ref(0)
|
||
|
|
const duration = ref(0)
|
||
|
|
|
||
|
|
function play(src: string, name: string) {
|
||
|
|
if (!audio.value) {
|
||
|
|
audio.value = new Audio()
|
||
|
|
audio.value.addEventListener('timeupdate', () => {
|
||
|
|
currentTime.value = audio.value?.currentTime ?? 0
|
||
|
|
})
|
||
|
|
audio.value.addEventListener('loadedmetadata', () => {
|
||
|
|
duration.value = audio.value?.duration ?? 0
|
||
|
|
})
|
||
|
|
audio.value.addEventListener('ended', () => {
|
||
|
|
playing.value = false
|
||
|
|
})
|
||
|
|
audio.value.addEventListener('pause', () => {
|
||
|
|
playing.value = false
|
||
|
|
})
|
||
|
|
audio.value.addEventListener('play', () => {
|
||
|
|
playing.value = true
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
if (currentSrc.value === src && playing.value) {
|
||
|
|
audio.value.pause()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if (currentSrc.value !== src) {
|
||
|
|
audio.value.src = src
|
||
|
|
currentSrc.value = src
|
||
|
|
currentName.value = name
|
||
|
|
}
|
||
|
|
|
||
|
|
audio.value.play()
|
||
|
|
}
|
||
|
|
|
||
|
|
function pause() {
|
||
|
|
audio.value?.pause()
|
||
|
|
}
|
||
|
|
|
||
|
|
function seek(time: number) {
|
||
|
|
if (audio.value) {
|
||
|
|
audio.value.currentTime = time
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function stop() {
|
||
|
|
if (audio.value) {
|
||
|
|
audio.value.pause()
|
||
|
|
audio.value.currentTime = 0
|
||
|
|
}
|
||
|
|
playing.value = false
|
||
|
|
currentSrc.value = null
|
||
|
|
currentName.value = ''
|
||
|
|
}
|
||
|
|
|
||
|
|
const progress = computed(() => {
|
||
|
|
if (duration.value === 0) return 0
|
||
|
|
return (currentTime.value / duration.value) * 100
|
||
|
|
})
|
||
|
|
|
||
|
|
export function useAudioPlayer() {
|
||
|
|
return {
|
||
|
|
play,
|
||
|
|
pause,
|
||
|
|
seek,
|
||
|
|
stop,
|
||
|
|
playing,
|
||
|
|
currentName,
|
||
|
|
currentTime,
|
||
|
|
duration,
|
||
|
|
progress,
|
||
|
|
currentSrc,
|
||
|
|
}
|
||
|
|
}
|