问题描述
错误:setValueAtTime 的无效参数:{},2.2188208616780045
不确定这个错误来自哪里,我为每个鼠标事件设置了时间,所以这应该可以防止任何错误。
我的代码:
import React,{ Component } from "react";
import ButtonGroup from "react-bootstrap/ButtonGroup";
import Button from "react-bootstrap/Button";
import * as Tone from "tone";
const synth = new Tone.Synth().toDestination();
//
//container component to hold the piano keys
//
class PianoBoard extends Component {
constructor(props) {
super(props);
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
}
handleMouseDown(e){
e.preventDefault(e);
Tone.start();
synth.triggerAttack(e.target.attributes.note,Tone.Now());
}
handleMouseUp(e){
e.preventDefault(e);
synth.triggerRelease(Tone.Now());
}
render() {
return (
<div class="pianoboard">
<Button onMouseDown={this.handleMouseDown} onmouseup={this.handleMouseUp} note="C4">
C4
</Button>
</div>
);
}
}
export default PianoBoard;
解决方法
错误来自您的语气属性。您应该将 note
直接传递给鼠标按下函数,而不是尝试获取事件的属性 note
。
...
...
handleMouseDown(e,note){
e.preventDefault(e);
Tone.start();
synth.triggerAttack(note,Tone.now());
}
...
...
<Button
onMouseDown={e => this.handleMouseDown(e,"C4")}
onMouseUp={this.handleMouseUp}
>
C4
</Button>