我在使用Wavesurfer的web项目中遇到了一个障碍。我已经在项目中安装了wavesurfer.js和react-wavesurfer作为节点模块。Wavesurfer.js似乎工作得很好,但react-wavesurfer似乎遇到了我发现难以调试的问题。以下代码:
import React from "react";
import WaveSurfer from "wavesurfer.js"
import ReactWavesurfer from "react-wavesurfer";
class Waveform extends React.Component {
makeWave() {
var wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'red',
progressColor: 'purple'
});
wavesurfer.load('path/to/mp3');
};
render() {
this.makeWave();
return (
<div>
<ReactWavesurfer
audioFile={'path/to/mp3'}
/>
</div>
);
}
}
export default Waveform;仅从对this.makeWave()的调用中生成第一个波形。它在尝试创建React波形时返回错误:Uncaught TypeError: this._wavesurfer.init is not a function。我正在使用browserify捆绑我的javascript依赖项。
任何帮助都将不胜感激!
发布于 2017-08-13 03:04:58
如果你在这方面仍然有问题,你可以创建你自己的Waveform组件,它本质上处理相同的负载。下面是一个适用于我的简单示例
1.手动安装wavesurfer.js:
# taken from here: https://wavesurfer-js.org/
npm install --save wavesurfer.js@2.0.0-beta012.构建自定义波形组件:
// components/waveform.js
import React from 'react'
import ReactDOM from 'react-dom'
import WaveSurfer from 'wavesurfer.js'
export default class Waveform extends React.Component {
constructor(props) {
super(props)
this.state = {
}
}
componentDidMount() {
this.$el = ReactDOM.findDOMNode(this)
this.$waveform = this.$el.querySelector('.wave')
this.wavesurfer = WaveSurfer.create({
container: this.$waveform,
waveColor: 'violet',
progressColor: 'purple'
})
this.wavesurfer.load(this.props.src)
}
componentWillUnmount() {
}
render() {
return (
<div className='waveform'>
<div className='wave'></div>
</div>
)
}
}
Waveform.defaultProps = {
src: ""
}3.然后,在父组件中:
// components/my-parent-component.js
import Waveform from 'path/to/components/Waveform'
...
render() {
return <div clasName='parent-component'><Waveform src={'/path/to/audio/src.mp3'} /></div>
}发布于 2017-08-13 23:51:43
React-wavesurfer处理wavesurfer实例本身的创建。因此,您可以省略makeWave部分。
import React, { Component } from 'react';
import WaveSurfer from 'react-wavesurfer';
export default class WaveComponent extends Component {
render() {
return (
<WaveSurfer audioFile="/path/to/audio.mp3" />
);
}
}这段代码适用于我。如果这不起作用,请发布您正在使用的wavesurfer.js和react-wavesurfer的确切版本。
此外,请记住,如果您正在使用模块捆绑器,则需要将wavesurfer.js公开为全局变量。(希望在不久的将来不再需要)-有关更准确的说明,请参阅https://github.com/mspae/react-wavesurfer#prerequisites-and-common-pitfalls)
https://stackoverflow.com/questions/44813585
复制相似问题