您好,我正在尝试在使用reacts useState挂钩的if语句中使用start()和stop()方法切换Tone.js播放器。播放器只是重新启动而不是停止。有什么想法吗?
import React, {useState} from "react"
import Tone from "tone"
function App() {
const [playing, setPlaying] = useState(false)
const track = "https://res.cloudinary.com/makingthings/video/upload/v1568881368/mp3/go_for_landing.mp3"
const player = new Tone.Player(track).toMaster()
Tone.Buffer.on('load', ()=>{
if(playing){
player.start()
}else if(!playing){
player.stop()
}
})
console.log(playing)
return (
<div className="App">
<h1>Hello World</h1>
<button onClick={()=> setPlaying(!playing)}>Toggle</button>
</div>
);
}
export default App;