gpt4 book ai didi

javascript - 正在 react 组件内调用暂停功能,但没有任何反应

转载 作者:行者123 更新时间:2023-12-04 08:21:25 27 4
gpt4 key购买 nike

我有一个用于播放和暂停的切换按钮。
这是代码:

export default (props) => {
let [soundState, setSoundState] = useState({ state: 'off' })
let [soundId, setSoundId] = useState(null)
let [soundSeek, setSoundSeek] = useState(null)
var sound = new Howl({
src: [props.url],
html5: true,
volume: 1,
myMusicID: '',
saveSeek: null,
onpause: () => {
console.log('pause', soundId)
}
})

const onPlayHandle = () => {
setSoundState({ state: 'playing' })
let id = sound.play()
setSoundId(id)
sound.seek(soundSeek, soundId)
}
const onPauseHandle = () => {
console.log('onPauseHandle', soundId)
sound.pause(soundId)
setSoundSeek(sound.seek(soundId))
}

}
onpause着火了,但声音还在响...
请一些帮助...

最佳答案

每次你的组件渲染一个新的 Howl实例已创建。您需要使用 useRef保持实例引用:

const soundRef = useRef(new Howl({
src: ["http://listen.vo.llnwd.net/g3/prvw/6/5/8/3/1/2255913856.mp3"],
html5: true,
volume: 1,
myMusicID: "",
saveSeek: null,
onpause: () => {
console.log("pause", soundId);
}
}));
const sound = soundRef.current;

关于javascript - 正在 react 组件内调用暂停功能,但没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65480141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com