We are creating a library of sounds with a waveform plaver using Wavesurf JS. In order to optimize the loading time we are saving the waveforms generated in the canvas as an image so we don't have to generate it every time the sound is loaded, and so far it works great.
But in order to save the image we need to wait for the canvas to load and the only way we have found is by using setTimeout.
wavediv = WaveSurfer.create({
container: '#canvasRender',
waveColor: '#FFFFFF',
progressColor: '#000000',
cursorColor:'rgba(0,0,0,0)',
splitChannels: true,
height : 75,
plugins: [
]
});
wavediv.load("sounds/test.mp3");
window.setTimeout(function(){
var dataImg = wavediv.exportImage('image/png');
saveWave(dataImg);
},600);
saveWave()
is just a function that calls an AJAX and sends the data to the server and we save it, that works great. The issue is that we don't want to rely on window.setTimeout()
because we a are limited to 0.6s seconds as we have in this example (600) and if the rendering takes more than 0.6s then it saves an empty image.
So I am looking for some kind of callback function or listener that will trigger saveWave()
only when the waveform has finished loading.
example:
wavediv.load("sounds/test.mp3", function(){
var dataImg = wavediv.exportImage('image/png');
saveWave(dataImg);
});
or
wavediv.ready(function () {
var dataImg = wavediv.exportImage('image/png');
saveWave(dataImg);
});
I found something similar in another post but it didn't work :
wavediv.on("ready", function () {
var dataImg = wavediv.exportImage('image/png');
saveWave(dataImg);
});
Same issue here: https://github.com/katspaugh/wavesurfer.js/issues/1727 and was only solved with setTimeout()
:(