简体   繁体   English

Tone.js无法与React一起使用

[英]Tone.js not working with React

I want to implement a basic Tone Player to load a file from a folder and play it by pressing a button in ReactJS. 我想实现一个基本的Tone Player来从文件夹加载文件并通过在ReactJS中按一个按钮来播放它。

import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import Tone from 'tone';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import Slider from 'material-ui/Slider';
import RaisedButton from 'material-ui/RaisedButton';


let sound = new Tone.Player("kick.wav").toMaster();
export default class App extends Component {

render() {

    return(
        <MuiThemeProvider>
            <div>
                <Slider min={0} max={100} step={10} className = "Slider1Test1" style={{height: 400}} axis="y"/>
                <RaisedButton label="A" className = "RaisedB1Test1" style={{width: 10}}/>

                <Slider min={0} max={100} step={10} className = "Slider2Test1" style={{height: 400}} axis="y"/>
                <RaisedButton label="B" className = "RaisedB2Test1" style={{width: 10}}/>
            </div>
        </MuiThemeProvider>
    );
}
}

But this gives an error: 但这给出了一个错误:

Uncaught (in promise) DOMException: Unable to decode audio data modules.js?hash=625032f…:48100 Uncaught (in promise) Tone.Buffer: could not decode audio data: kick.wav 未捕获(已承诺)DOMException:无法解码音频数据modules.js?hash = 625032f…:48100未捕获(已承诺)Tone.Buffer:无法解码音频数据:kick.wav

I have tried "./kick.wav", "./kick.ogg", "./kick.mp3". 我已经尝试过“ ./kick.wav”、“./kick.ogg”、“./kick.mp3”。 I have tried giving complete paths. 我试图给出完整的路径。 I have tried using only web audio by giving XMLHTTPRequest - it is the same error all the time. 我尝试通过提供XMLHTTPRequest仅使用网络音频-一直都是相同的错误。

I am new to react - please let me know if there is a correct way to do this. 我是新来的回应者-请告知我是否有正确的方法。 Do I need to use componentDidMount? 我需要使用componentDidMount吗? ( I have tried using that as well. Please let me know the correct way to do it). (我也尝试过使用它。请让我知道正确的方法)。 Thanks 谢谢

It looks like you're using meteor and this is somehow blocking your request to the server to load your file. 看来您正在使用流星,这以某种方式阻止了您对服务器加载文件的请求。 The easiest solution is to create a public folder in your root directory and place your samples in there. 最简单的解决方案是创建一个public的根目录文件夹,将您的样品在那里。 Then pass in ./kick.wav to your Tone.Player constructor. 然后将./kick.wav传递给您的Tone.Player构造函数。 The constructor makes a GET request under the hood and will find your file automatically. 构造函数在GET发出GET请求,并会自动找到您的文件。 Along with the changes I had to add this code to play the sample after the buffer had finished loading: 除了更改,我还必须添加以下代码以在缓冲区完成加载后播放示例:

const x = new Tone.Player('./kick.wav').toMaster()
Tone.Buffer.on('load', () => {
    x.start();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM