fix: loop pronunciation in wordClip

This commit is contained in:
KaiyiWing
2023-04-10 01:13:08 +08:00
parent 1272d6e16e
commit e46763a658
2 changed files with 7 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import { useAtomValue } from 'jotai'
import { Howl } from 'howler'
import { useEffect, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import useSound from 'use-sound'
import { HookOptions } from 'use-sound/dist/types'
import { addHowlListener } from '@/utils'
@@ -24,23 +24,22 @@ function generateWordSoundSrc(word: string, pronunciation: Exclude<Pronunciation
}
}
export default function usePronunciationSound(word: string) {
export default function usePronunciationSound(word: string, isLoop?: boolean) {
const pronunciationConfig = useAtomValue(pronunciationConfigAtom)
const loop = useMemo(() => (typeof isLoop === 'boolean' ? isLoop : pronunciationConfig.isLoop), [isLoop, pronunciationConfig.isLoop])
const [isPlaying, setIsPlaying] = useState(false)
const [play, { stop, sound }] = useSound(generateWordSoundSrc(word, pronunciationConfig.type as Exclude<PronunciationType, false>), {
html5: true,
format: ['mp3'],
loop: pronunciationConfig.isLoop,
loop,
} as HookOptions)
useEffect(() => {
if (!sound) return
sound.loop(pronunciationConfig.isLoop)
sound.loop(loop)
return noop
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pronunciationConfig.isLoop])
}, [loop, sound])
useEffect(() => {
if (!sound) return

View File

@@ -13,7 +13,7 @@ export default function WordChip({ word }: { word: Word }) {
const hover = useHover(context)
const role = useRole(context, { role: 'tooltip' })
const { getReferenceProps, getFloatingProps } = useInteractions([hover, role])
const { play, stop } = usePronunciationSound(word.name)
const { play, stop } = usePronunciationSound(word.name, false)
const onClickWord = useCallback(() => {
stop()