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

About event handler #357

Open
blueromans opened this issue Mar 16, 2025 · 10 comments
Open

About event handler #357

blueromans opened this issue Mar 16, 2025 · 10 comments

Comments

@blueromans
Copy link

I want to handle event when playback completed. But I don't understand how onended props running

@maciejmakowski2003
Copy link
Collaborator

Currently you have to assign callback to .onended property/event. This callback will be called in c++ when playback stops with one argument - stopTime. This approach allows us to achieve frame accurate play/pause.

bufferSourceRef.current.onended = (stopTime?: number) => {
        setOffset((_prev) => stopTime || 0);
      };

Here you have full example https://github.com/software-mansion-labs/react-native-audio-api/blob/main/apps/common-app/src/examples/AudioFile/AudioFile.tsx

@blueromans
Copy link
Author

blueromans commented Mar 17, 2025

Currently you have to assign callback to .onended property/event. This callback will be called in c++ when playback stops with one argument - stopTime. This approach allows us to achieve frame accurate play/pause.

bufferSourceRef.current.onended = (stopTime?: number) => {
        setOffset((_prev) => stopTime || 0);
      };

Here you have full example https://github.com/software-mansion-labs/react-native-audio-api/blob/main/apps/common-app/src/examples/AudioFile/AudioFile.tsx

Yes i followed this example. Copied as well. But itsnt fire trigger playback completed. Just i have seen console messages onended, when click to stop button, after play started.

@maciejmakowski2003
Copy link
Collaborator

which version are you using?

@blueromans
Copy link
Author

which version are you using?
0.5.0

@maciejmakowski2003
Copy link
Collaborator

maciejmakowski2003 commented Mar 18, 2025

Could you prepare some repro? In my build this example works just how it has to.

@dlebrun
Copy link

dlebrun commented Mar 26, 2025

Same problem here (using 0.5.2). Here is the relevant part of my code:

      const sourceNode = audioContext.createBufferSource();
      sourceNode.buffer = audioBuffersMap.get(index)!;

      const callback = (stopTime?: number) => {
        setOffset((_prev) => stopTime || 0);
      };
      console.log(callback);
      sourceNode.onended = callback;

      if (sourceNode.onended === undefined) {
        console.log("Callback not set");
      } else {
        console.log(sourceNode.onended);
      }

When I run this with npx expo run:android, I get this in the console:

 (NOBRIDGE) LOG  [Function callback]
 (NOBRIDGE) LOG  Callback not set

@maciejmakowski2003
Copy link
Collaborator

Getter for onended callback is not implemented, but setter works fine. I have added some improvements to processing process including stop/playback completed. I will added it in the newest version(0.5.4) with getter for onended callback.

@dlebrun
Copy link

dlebrun commented Mar 27, 2025

Thanks for the feedback.
In the meantime, I have experimented a bit more: the callback seems to be correctly called when the track reaches its end, but not when stop is called on the source node.

@blueromans
Copy link
Author

Is there a method or an event to calculate how many times the audio played?

@maciejmakowski2003
Copy link
Collaborator

Unfortunately not, so you have to calculate it by yourself. If you send me same snippet of code I will help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants