gpt4 book ai didi

javascript - 音量的 react 输入范围 slider 不起作用

转载 作者:行者123 更新时间:2023-12-02 21:58:32 25 4
gpt4 key购买 nike

各位程序员大家好,我正在制作一个简单的鼓机,并尝试实现音量控制,一切“正常”,我没有收到任何错误, slider 滑动,但由于某种原因音量没有改变,当我 console.log 卷值和显示时,它们都正确更改,除了页面上的音量不受影响,所以也许我在问如何覆盖计算机的音量控制?请帮助stackoverflow的天才,这是我的代码:

class DrumMachine extends React.Component {
constructor(props) {
super(props)
this.state = {
power: false,
volumeVal: 0.5,
display: String.fromCharCode(160)
}
this.onKeyPress = this.onKeyPress.bind(this);
this.powerButton = this.powerButton.bind(this);
this.handleVolume = this.handleVolume.bind(this);
};
componentDidMount() {
document.addEventListener('keydown', this.onKeyPress)
}
componentWillUnmount() {
document.removeEventListener('keydown', this.onKeyPress)
}

onKeyPress(e) {
if (e.keyCode === 86) {
document.getElementById('bassdrum').play();
document.getElementById('bassdrum').currentTime = 0;
};
if (e.keyCode === 71) {
document.getElementById('hihat').play();
document.getElementById('hihat').currentTime = 0;
};
if (e.keyCode === 88) {
document.getElementById('snaredrum').play();
document.getElementById('snaredrum').currentTime = 0;
};
if (e.keyCode === 89) {
document.getElementById('tom1').play();
document.getElementById('tom1').currentTime = 0;
};
if (e.keyCode === 85) {
document.getElementById('tom2').play();
document.getElementById('tom2').currentTime = 0;
};
if (e.keyCode === 56) {
document.getElementById('crashcymbal').play();
document.getElementById('crashcymbal').currentTime = 0;
};
}

powerButton() {
if (this.state.power) {
this.setState({ power: false })
} else {
this.setState({ power: true })
}
};

handleVolume(e) {
if (this.state.power) {
this.setState({
volumeVal: e.target.value,
display: "Volume: " + Math.round(e.target.value * 100)
});
setTimeout(() => this.clearDisplay(), 1000);
}
}

clearDisplay() {
this.setState({
display: String.fromCharCode(160)
});
}

render() {
var buttonClass = ['power'];

if (this.state.power) {
buttonClass.push('On');
} else {
buttonClass.push('Off')
}

return (
<div id='container' className='container'>
<div id='drumkeycontainer'>
<div className='row'>
<h6>ReactJS Drum Machine</h6>
<button className={buttonClass.join(' ')} onClick={this.powerButton}>Power</button>
</div>
<div className='row'>
<div keyCode='86' className='key' onClick={() => {document.getElementById('bassdrum').play(); document.getElementById('bassdrum').currentTime = 0;}}>
<kbd><b>V</b></kbd>
<br />
<span className='sound'>Bass Drum</span>
</div>
<div keyCode='71' className='key' onClick={() => {document.getElementById('hihat').play(); document.getElementById('hihat').currentTime = 0;}}>
<kbd><b>G</b></kbd>
<br />
<span className='sound'>Hi-Hat</span>
</div>
<div keyCode='88' className='key' onClick={() => {document.getElementById('snaredrum').play(); document.getElementById('snaredrum').currentTime = 0;}}>
<kbd><b>X</b></kbd>
<br />
<span className='sound'>Snare</span>
</div>
</div>
<div className='row'>
<div keyCode='89' className='key' onClick={() => {document.getElementById('tom1').play(); document.getElementById('tom1').currentTime = 0;}}>
<kbd><b>Y</b></kbd>
<br />
<span className='sound'>Tom 1</span>
</div>
<div keyCode='85' className='key' onClick={() => {document.getElementById('tom2').play(); document.getElementById('tom2').currentTime = 0;}}>
<kbd><b>U</b></kbd>
<br />
<span className='sound'>Tom 2</span>
</div>
<div keyCode='56' className='key' onClick={() => {document.getElementById('crashcymbal').play(); document.getElementById('crashcymbal').currentTime = 0;}}>
<kbd><b>8</b></kbd>
<br />
<span className='sound'>Crash</span>
</div>
</div>
<div className='row'>
<div className='slider-wrapper'>
<label>Volume
<input type="range" min='0' max='1' value={this.state.volumeVal} onChange={this.handleVolume} step='0.01' />
</label>
</div>
</div>
</div>
{/*links for audio elements obtained from www.findsounds.com */}
{this.state.power && <div id='audiocontainer'>
<audio id='bassdrum' keyCode='86' src='https://www.myinstants.com/media/sounds/bass-drum.mp3'></audio>
<audio id='hihat' keyCode='71' src='http://dight310.byu.edu/media/audio/FreeLoops.com/1/1/Alchemist%20HiHat%203-1788-Free-Loops.com.mp3'></audio>
<audio id='snaredrum' keyCode='88' src='https://www.myinstants.com/media/sounds/snare.mp3'></audio>
<audio id='tom1' keyCode='89' src='http://www.denhaku.com/r_box/sr16/sr16tom/loelectm.wav'></audio>
<audio id='tom2' keyCode='85' src='http://dight310.byu.edu/media/audio/FreeLoops.com/1/1/909%20Tom%20Low%2001-5859-Free-Loops.com.mp3'></audio>
<audio id='crashcymbal' keyCode='56' src='http://dight310.byu.edu/media/audio/FreeLoops.com/1/1/AT%20B%20Crash-1026-Free-Loops.com.mp3'></audio>
</div>}
</div>
);
};
}

ReactDOM.render(<DrumMachine />, document.getElementById('app'))

最佳答案

state对象内,您设置一个名为volumeVal的键,它存储一个范围从0到1的 float ,可以使用 slider 控制。

不幸的是,除了 slider 之外,您没有在代码中的任何地方使用该值,因此您听不到音量的变化。

HTML 的体积 <audio>元素 - 这是您用于个人声音的元素 - 可以通过它的 .volume 属性进行控制。因此,您所要做的就是为该属性分配值 this.state.volumeVal之前的播放。

例如,hihat 的 keyCode 事件需要更改为:

if (e.keyCode === 71) {
document.getElementById('hihat').volume=this.state.volumeVal;
document.getElementById('hihat').currentTime = 0;
document.getElementById('hihat').play();
};

关于javascript - 音量的 react 输入范围 slider 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59940825/

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