添加音效

This commit is contained in:
zyronon
2023-08-04 16:19:35 +08:00
parent 5c334d6d2b
commit 69fbf9e734
11 changed files with 58 additions and 6 deletions

34
src/hooks/useSound.ts Normal file
View File

@@ -0,0 +1,34 @@
import {onMounted} from "vue"
export function useSound(srcList?: string[], num?: number) {
let audioList: HTMLAudioElement[] = $ref([])
let audioLength = $ref(1)
const setAudio = (srcList2: string[], num2?: number) => {
if (num2) audioLength = num2
audioList = []
for (let i = 0; i < audioLength; i++) {
srcList2.map(src => {
audioList.push(new Audio(src))
})
}
index = 0
}
onMounted(() => {
if (srcList) setAudio(srcList, num)
})
let index = $ref(0)
const play = () => {
index++
if (audioList.length > 1 && audioList.length !== audioLength) {
audioList[index % audioList.length].play()
} else {
audioList[index % audioLength].play()
}
}
return {
play,
setAudio
}
}