首页 > 解决方案 > 反应按钮,使用tone.Js,在第一次按键后中断,错误:setValueAtTime 的参数无效:{},2.2188208616780045

问题描述

然后获得第一个按钮按下的音调,我收到此错误...

错误: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;

标签: javascriptreactjstone.js

解决方案


错误来自您的音调属性。您应该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>


推荐阅读