Last active
July 30, 2021 13:09
-
-
Save georgeperry1/ab080f95bedf2236ef87e63c7e3e60c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import WaveSurfer from 'wavesurfer.js'; | |
import { WaveformContianer, Wave, PlayButton } from './Waveform.styled'; | |
class Waveform extends Component { | |
state = { | |
playing: false, | |
}; | |
componentDidMount() { | |
const track = document.querySelector('#track'); | |
this.waveform = WaveSurfer.create({ | |
barWidth: 3, | |
cursorWidth: 1, | |
container: '#waveform', | |
backend: 'WebAudio', | |
height: 80, | |
progressColor: '#2D5BFF', | |
responsive: true, | |
waveColor: '#EFEFEF', | |
cursorColor: 'transparent', | |
}); | |
this.waveform.load(track); | |
}; | |
handlePlay = () => { | |
this.setState({ playing: !this.state.playing }); | |
this.waveform.playPause(); | |
}; | |
render() { | |
const url = 'https://www.mfiles.co.uk/mp3-downloads/gs-cd-track2.mp3'; | |
return ( | |
<WaveformContianer> | |
<PlayButton onClick={this.handlePlay} > | |
{!this.state.playing ? 'Play' : 'Pause'} | |
</PlayButton> | |
<Wave id="waveform" /> | |
<audio id="track" src={url} /> | |
</WaveformContianer> | |
); | |
} | |
}; | |
export default Waveform; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
setting property
Backend
should beMediaElement
for this to work.