微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反应按钮,使用tone.Js,在第一次按键后中断,错误:无效参数到setValueAtTime:{},2.2188208616780045

如何解决反应按钮,使用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;

解决方法

错误来自您的语气属性。您应该将 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。