v1 version
This commit is contained in:
@@ -4,115 +4,132 @@ import {PronunciationApi} from "@/types.ts";
|
||||
import beep from "@/assets/sound/beep.wav";
|
||||
import correct from "@/assets/sound/correct.wav";
|
||||
|
||||
export function useSound(urlList?: string[], num?: number) {
|
||||
let audioList: HTMLAudioElement[] = $ref([])
|
||||
let audioLength = $ref(1)
|
||||
let index = $ref(0)
|
||||
export function useSound(audioSrcList?: string[], audioFileLength?: number) {
|
||||
let audioList: HTMLAudioElement[] = $ref([])
|
||||
let audioLength = $ref(1)
|
||||
let index = $ref(0)
|
||||
|
||||
onMounted(() => {
|
||||
if (urlList) setAudio(urlList, num)
|
||||
})
|
||||
onMounted(() => {
|
||||
if (audioSrcList) setAudio(audioSrcList, audioFileLength)
|
||||
})
|
||||
|
||||
function 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
|
||||
function setAudio(audioSrcList2: string[], audioFileLength2?: number) {
|
||||
if (audioFileLength2) audioLength = audioFileLength2
|
||||
audioList = []
|
||||
for (let i = 0; i < audioLength; i++) {
|
||||
audioSrcList2.map(src => audioList.push(new Audio(src)))
|
||||
}
|
||||
index = 0
|
||||
}
|
||||
|
||||
function play() {
|
||||
index++
|
||||
if (audioList.length > 1 && audioList.length !== audioLength) {
|
||||
audioList[index % audioList.length].play()
|
||||
} else {
|
||||
audioList[index % audioLength].play()
|
||||
}
|
||||
function play() {
|
||||
index++
|
||||
if (audioList.length > 1 && audioList.length !== audioLength) {
|
||||
audioList[index % audioList.length].play()
|
||||
} else {
|
||||
audioList[index % audioLength].play()
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
play,
|
||||
setAudio
|
||||
]
|
||||
return {play, setAudio}
|
||||
}
|
||||
|
||||
export function usePlayKeyboardAudio() {
|
||||
const settingStore = useSettingStore()
|
||||
const [playKeyboardAudio, setAudio] = useSound()
|
||||
const settingStore = useSettingStore()
|
||||
const {play, setAudio} = useSound()
|
||||
|
||||
watchEffect(() => {
|
||||
let urlList = getAudioFileUrl(settingStore.keyboardSoundFile)
|
||||
setAudio(urlList, urlList.length === 1 ? 3 : 1)
|
||||
})
|
||||
return playKeyboardAudio
|
||||
watchEffect(() => {
|
||||
let urlList = getAudioFileUrl(settingStore.keyboardSoundFile)
|
||||
setAudio(urlList, urlList.length === 1 ? 3 : 1)
|
||||
})
|
||||
|
||||
function playAudio() {
|
||||
if (settingStore.keyboardSound) {
|
||||
play()
|
||||
}
|
||||
}
|
||||
|
||||
return playAudio
|
||||
}
|
||||
|
||||
export function usePlayBeep() {
|
||||
return useSound([beep], 1)[0]
|
||||
const settingStore = useSettingStore()
|
||||
const {play} = useSound([beep], 1)
|
||||
|
||||
function playAudio() {
|
||||
if (settingStore.effectSound) {
|
||||
play()
|
||||
}
|
||||
}
|
||||
|
||||
return playAudio
|
||||
}
|
||||
|
||||
export function usePlayCorrect() {
|
||||
return useSound([correct], 1)[0]
|
||||
const settingStore = useSettingStore()
|
||||
const {play} = useSound([correct], 1)
|
||||
|
||||
function playAudio() {
|
||||
if (settingStore.effectSound) {
|
||||
play()
|
||||
}
|
||||
}
|
||||
|
||||
return playAudio
|
||||
}
|
||||
|
||||
export function usePlayWordAudio() {
|
||||
const audio = $ref(new Audio())
|
||||
const settingStore = useSettingStore()
|
||||
const audio = $ref(new Audio())
|
||||
|
||||
function generateWordSoundSrc(word: string, pronunciation: string) {
|
||||
switch (pronunciation) {
|
||||
case 'uk':
|
||||
return `${PronunciationApi}${word}&type=1`
|
||||
case 'us':
|
||||
return `${PronunciationApi}${word}&type=2`
|
||||
}
|
||||
function playAudio(word: string) {
|
||||
if (settingStore.wordSoundType === 'uk') {
|
||||
audio.src = `${PronunciationApi}${word}&type=1`
|
||||
} else if (settingStore.wordSoundType === 'us') {
|
||||
audio.src = `${PronunciationApi}${word}&type=2`
|
||||
}
|
||||
audio.play()
|
||||
}
|
||||
|
||||
function playWordAudio(word: string) {
|
||||
audio.src = generateWordSoundSrc(word, 'us')
|
||||
audio.play()
|
||||
}
|
||||
|
||||
return playWordAudio
|
||||
return playAudio
|
||||
}
|
||||
|
||||
export function usePlayAudio(url: string) {
|
||||
new Audio(url).play().then(r => void 0)
|
||||
new Audio(url).play().then(r => void 0)
|
||||
}
|
||||
|
||||
export function getAudioFileUrl(name: string) {
|
||||
if (name === '机械') {
|
||||
return [
|
||||
`/sound/key-sounds/jixie/机械0.mp3`,
|
||||
`/sound/key-sounds/jixie/机械1.mp3`,
|
||||
`/sound/key-sounds/jixie/机械2.mp3`,
|
||||
`/sound/key-sounds/jixie/机械3.mp3`,
|
||||
]
|
||||
} else {
|
||||
return [`/sound/key-sounds/${name}.mp3`]
|
||||
}
|
||||
if (name === '机械') {
|
||||
return [
|
||||
`/sound/key-sounds/jixie/机械0.mp3`,
|
||||
`/sound/key-sounds/jixie/机械1.mp3`,
|
||||
`/sound/key-sounds/jixie/机械2.mp3`,
|
||||
`/sound/key-sounds/jixie/机械3.mp3`,
|
||||
]
|
||||
} else {
|
||||
return [`/sound/key-sounds/${name}.mp3`]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function useWatchAllSound() {
|
||||
const settingStore = useSettingStore()
|
||||
const settingStore = useSettingStore()
|
||||
|
||||
watch([
|
||||
() => settingStore.wordSound,
|
||||
() => settingStore.keyboardSound,
|
||||
() => settingStore.translateSound,
|
||||
() => settingStore.effectSound,
|
||||
], (n) => {
|
||||
settingStore.allSound = n.some(v => v);
|
||||
})
|
||||
watch([
|
||||
() => settingStore.wordSound,
|
||||
() => settingStore.keyboardSound,
|
||||
() => settingStore.translateSound,
|
||||
() => settingStore.effectSound,
|
||||
], (n) => {
|
||||
settingStore.allSound = n.some(v => v);
|
||||
})
|
||||
}
|
||||
|
||||
export function useChangeAllSound(e: boolean) {
|
||||
const settingStore = useSettingStore()
|
||||
const settingStore = useSettingStore()
|
||||
|
||||
settingStore.allSound = e
|
||||
settingStore.wordSound = e
|
||||
settingStore.keyboardSound = e
|
||||
settingStore.translateSound = e
|
||||
settingStore.effectSound = e
|
||||
settingStore.allSound = e
|
||||
settingStore.wordSound = e
|
||||
settingStore.keyboardSound = e
|
||||
settingStore.translateSound = e
|
||||
settingStore.effectSound = e
|
||||
}
|
||||
Reference in New Issue
Block a user