Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't fire events #52

Closed
Simoneth opened this issue Jun 11, 2022 · 7 comments
Closed

I can't fire events #52

Simoneth opened this issue Jun 11, 2022 · 7 comments

Comments

@Simoneth
Copy link

Hi,
I tried to fire events as:

     useEffect(() => {
        wavesurferRef.current.on("finish", () => {
            console.log("Track Finished");
          });
      }, []); 

It doesn't work, it doesn't fire when a song is finished.
Is it my fault?

@ShiiRochi
Copy link
Owner

@Simoneth, hi, did you try to check whether other events are being fired?

@Simoneth
Copy link
Author

@Simoneth, hi, did you try to check whether other events are being fired?

Hmm...it's strange, I can fire "audioprocess" but not "finish" and I have no idea why.
This is my function to start play, maybe it's related (I use regions):

   wavesurferRef.current.play(
      regionsRef.current[id].start,
      regionsRef.current[id].end
    );

@ShiiRochi
Copy link
Owner

ShiiRochi commented Jun 11, 2022

So, you can subscribe to "audioprocess" event and it's working, but it does not work with "finished", don't you?

If that is so, then event subscription through wavesurfer instance is working, however finished event is not being fired for some reason that is, from my POV, wavesurfer.js related.

Also, it may be, that "finished" is called after record is played up to the end.

@ShiiRochi
Copy link
Owner

ShiiRochi commented Jun 11, 2022

@Simoneth, I've tried to listen for "finish" event in demo fork.

The result is that this event is called when record playing is fully completed, it is not related to region or region play, because all you do to play region is just like make wavesurfer to play record's fragment, when "finish" is related to record.

It should also be taken into account, that the end of an audio can differ from the end of a region, so even if there would be a difference in a pair of milliseconds, then "finish" can be not fired...but this is just my assumption.

@Simoneth
Copy link
Author

Simoneth commented Jun 11, 2022

@Simoneth, I've tried to listen for "finish" event in demo fork.

The result is that this event is called when record playing is fully completed, it is not related to region or region play, because all you do to play region is just like make wavesurfer to play record's fragment, when "finish" is related to record.

It should also be taken into account, that the end of an audio can differ from the end of a region, so even if there would be a difference in a pair of milliseconds, then "finish" can be not fired...but this is just my assumption.

Since I use only regions, from Regions plugin page, I can't find any event that could fire the end of the region play...any workaround? It's just for my toggle button "Play / Stop"

@ShiiRochi
Copy link
Owner

ShiiRochi commented Jun 11, 2022

@Simoneth, I think, that this question (how to listen to region play completed or simulate it) is more related to wavesurfer.js repository. From my point of view, you can add a new listener for...audioprocess or progress event (I don't remember all the list), but with explicit aim to call some callback when progress will reach region's end...I will provide you with some pseudo code, but the rest is fully up to you:

function callbackFn () {
    ...some code...
} // or useCallback...

// It will save you from referencing issues withing listeners
const callbackRef = useRef(callbackFn);

function playRegion ({ start, end, ...restOfRegion }) {
    ws.current.on("progress", (data) => {
        if (data === end) {
            callbackRef.current();
        }
    });

    ws.current.play(
      start,
      rend
    );
}

@Simoneth
Copy link
Author

Here my simple solution:

    useEffect(() => {
        wavesurferRef.current.on("region-out", () => {
            console.log("Track Finished");
        });
    }, [regions]); 

Repository owner locked as resolved and limited conversation to collaborators Jun 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants