[英]How audio can be added to a drum machine in React?
I am doing FCC drum machine project in React and trying to add audio on the buttons, but could not add the sound.我正在 React 中做 FCC 鼓机项目并尝试在按钮上添加音频,但无法添加声音。 Can someone check that where I am making a mistake.有人可以检查我在哪里犯了错误。 Help is extremely appreciated.非常感谢您的帮助。
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
drumPads : [
{
id: "Q",
src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3",
beat: "Heater-1",
},
{
id: "W",
src: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3",
beat: "Heater-2"
},
}
handleClick(id, beat) {
return () => {
document.getElementById(id).play();
this.setState({
beatName: beat,
});
};
}
render() {
return (
this.state.drumPads.map((button, i) =>
<button key={i} id={beat} onClick={this.handleClick(id, beat)}>
<h1>{ button.id }</h1>
<audio src={button.src} />
</button>
)
)
}
}
ReactDOM.render( <App />,
document.getElementById("root")
)
You need to change this part of the code: id={beat}
because you're just creating two buttons with the same id.您需要更改这部分代码: id={beat}
因为您只是创建了两个具有相同 id 的按钮。 What you mean to do is id={button.beat}
.你的意思是id={button.beat}
。 Check if that works.检查这是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.