katspaugh / wavesurfer.js

Audio waveform player
https://wavesurfer.xyz
BSD 3-Clause "New" or "Revised" License
8.74k stars 1.62k forks source link

Timeline fails to draw when loading new audio. Added handling for duration parameter #1460

Closed mgoldsteinla closed 6 years ago

mgoldsteinla commented 6 years ago

Upgrading to 2.0.6 I ran into a problem when loading new audio files into the player, the timeline would fail to render.

Inside the timeline code, the call for this.wavesurfer.backend.getDuration() returned NaN. Perhaps it's a race condition where the timeline request comes before the audio file is fully downloaded?

I have the song duration available from the models, so my solution was to add a duration parameter when I initialized the timeline plugin:

    this.wavesurfer.addPlugin(Timeline.create({
      container: $('.waveform-timeline'),
      duration: (this.wavesurfer.getDuration()) ? this.wavesurfer.getDuration() : this.model.track.duration
    })).initPlugin('timeline');

I then had to hack the timeline code to add handling for this parameter, updating the this.wavesurfer.backend.getDuration() request:

var duration = (this.wavesurfer.backend.getDuration()) ? this.wavesurfer.backend.getDuration() : this.wavesurfer.timeline.params.duration;

I think it makes sense to be able to pass a duration parameter to timeline on initialization. It would be a nice enhancement.

It is a workaround for the deeper issue of why the this.wavesurfer.backend.getDuration() request sometimes returns NaN. Is anybody else experiencing this issue?

thijstriemstra commented 6 years ago

@mgoldsteinla can you try 2.1.0?

mgoldsteinla commented 6 years ago

Same issue. I end up with blank timelines.

thijstriemstra commented 6 years ago

Can you create a pull request with these changes?

mgoldsteinla commented 6 years ago

https://github.com/katspaugh/wavesurfer.js/pull/1475