简体   繁体   English

Wavesurfer.js工作正常,但是react-wavesurfer有问题

[英]Wavesurfer.js is working fine, but react-wavesurfer has issues

I have run into a roadblock on my web project that uses Wavesurfer. 我在使用Wavesurfer的Web项目中遇到了一个障碍。 I have installed wavesurfer.js and react-wavesurfer as node modules in my project. 我已经在项目中安装了waveurfer.js和react-wavesurfer作为节点模块。 Wavesurfer.js seems to be working fine, but react-wavesurfer seems to be encountering issues that I am finding difficult to debug. Wavesurfer.js似乎运行良好,但是react-wavesurfer似乎遇到了我发现难以调试的问题。 The following code: 如下代码:

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;

Produces only the first waveform from the call to this.makeWave(). 从对this.makeWave()的调用中仅生成第一个波形。 It returns an error when trying to create the React waveform: Uncaught TypeError: this._wavesurfer.init is not a function . 尝试创建反应波形时返回错误: Uncaught TypeError: this._wavesurfer.init is not a function I am using browserify to bundle my javascript dependencies. 我正在使用browserify捆绑我的javascript依赖项。

Any help would be much appreciated! 任何帮助将非常感激!

If you are still having trouble with this, you can create your own Waveform component that essentially handles the same load. 如果仍然遇到问题,则可以创建自己的Waveform组件,该组件本质上可以处理相同的负载。 Here is a simple example that worked for me 这是一个对我有用的简单示例

1. install wavesurfer.js manually: 1.手动安装wavesurfer.js:

# taken from here: https://wavesurfer-js.org/
npm install --save wavesurfer.js@2.0.0-beta01

2. build a custom Waveform component: 2.构建一个自定义的Waveform组件:

// 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. and then, in the parent component: 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>
}

React-wavesurfer handles the creation of the wavesurfer instance itself. React-wavesurfer处理waveurfer实例本身的创建。 So you can leave out the makeWave part. 因此,您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" />
    );
  }
}

This code works for me. 该代码对我有用。 If this doesn't work please post the exact versions of wavesurfer.js and react-wavesurfer you are using. 如果这不起作用,请发布您正在使用的waveurfer.js和react-wavesurfer的确切版本。

Also please bear in mind that you need to expose wavesurfer.js as a global variable if you are using a module bundler. 另外请记住,如果使用模块捆绑程序,则需要将Wavesurfer.js作为全局变量公开。 (This will hopefully be no longer necessary in the near future) – for more exact instructions please see https://github.com/mspae/react-wavesurfer#prerequisites-and-common-pitfalls ) (希望在不久的将来不再需要)–有关更确切的说明,请参阅https://github.com/mspae/react-wavesurfer#prerequisites-and-common-pitfalls

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

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