Not sure I'm wording this correctly but here is what I'm trying to do.
I am using a function to render multiple instances of wavesurfer on a page.
Out of the box this is straight forward, as you just declare each separate instance of wavesurfer :
var wavesurfer1 = WaveSurfer.create({
container: domEl,
barWidth: 3,
.....
var wavesurfer2 = WaveSurfer.create({
container: domEl,
barWidth: 3,
.....
But I'm preloading JSON stored waveform data and NOT loading any audio file at page load, only when a user clicks the play button should the audio file load. Also the amount of instances and their id's will be created dynamically.
I've got the logic working but I'm having trouble targeting each unique instance of wavesurfer as, well, they are not unique - hence my question, and I can't find any information on the official site (or git) about targeting individual instances. I would assume that it's possible by the container
but can't find any info about how to do this, so I'm trying it another way.
In the function that renders each instance of wavesurfer we have this (domEl
is being passed into the function and creates a unique div
and id
for each waveform container) :
var wavesurfer = WaveSurfer.create({
container: domEl,
barWidth: 3,
waveColor: '#CCC',
progressColor: 'white',
backend: 'MediaElement',
mediaType:'audio',
height: 80,
cursorColor: 'white',
responsive: true,
hideScrollbar: true
});
And the function that loads the actual audio :
$('.m-btn-player').on('click', function() {
id = $(this).data("id");
var url = "/play?id="+id;
wavesurfer.load(url);
wavesurfer.play();
})
This works, but as each instance of wavesurfer is declared as wavesurfer
it loads the audio into every single instance no matter which play button is clicked.
What I thought I could do was declare each instance of wavesurfer in the render function with a dynamically generated name i.e :
// `id` is passed to the function
var wavesurfer+id = WaveSurfer.create({
container: domEl,
barWidth: 3,
....
Then :
wavesurfer+id.load(url);
wavesurfer+id.play();
But this doesn't work (and I don't even think it's possible from what I've read about dynamic variable names).
I've also read that I should possibly be using hashmaps for something like this but have no idea how to use them, so if that is the case, or you can see another way I can make this work I would really appreciate some pointers.
If this approach is entirely wrong how do I target multiple instances of wavesurfer without declaring the object name for each one? I can't find anything that explains how to do this anywhere.