Is it possible to check when sound has finished playing?

Please provide the following:

  1. SDK Version: “34.0.0”
  2. Platforms": Android/iOS

Hi everyone.

I’m just trying to find a way to detect when my first sound has finished playing, so I can start the next one. Is this possible?

It looks like onPlaybackStatusUpdate might give me what I’m after, but I can’t work out how to apply it for my use case.

I’m not sure if this is useful or not, but here is my current code in case:

PS: Thanks for any help!

async componentWillMount() {
    this.heartBeat = new Audio.Sound();
    await this.heartBeat.loadAsync(require("./hearbeat.mp3"));

    await Audio.setAudioModeAsync({
      playsInSilentModeIOS: true,
      allowsRecordingIOS: false,
      interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
      shouldDuckAndroid: false,
      interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
      playThroughEarpieceAndroid: true,
      staysActiveInBackground: true
    });
    await Audio.setIsEnabledAsync(true);
    await this.heartBeat.setPositionAsync(0);
  }

  handlePlaySound = async () => {
    try {
      await this.heartBeat.setPositionAsync(0);
      await this.heartBeat.playAsync();
    } catch (error) {
      console.log("ERROR", error);
    }
  };

  handleStopSound = async val => {
    try {
      await this.heartBeat.setPositionAsync(0);
      await this.heartBeat.stopAsync();
    } catch (error) {
      console.log("ERROR", error);
    }
  };

Still struggling to find an answer to this one! If anyone can help, would really appreciate any leads to how to solve it. Thanks again.

Hi

There’s a didJustFinish option which I think is what you’re looking for.

See the following example for how you might use this:

That looks like exactly what I’m after. Thanks very much @wodin!

1 Like

Hello,

You can set a playback status update function:

const soundObject = new Audio.Sound();
    try {
      await soundObject.loadAsync(audio);
      soundObject.setOnPlaybackStatusUpdate(async (status) => {
        if (status.didJustFinish === true) {
          // audio has finished!
          await soundObject.unloadAsync()
        }
      })
      await soundObject.playAsync();
      
    } catch (error) {
      // An error occurred!
    }
6 Likes

Made my day … thanks :slight_smile:

I’ll choose you for become the president

Thank you so much!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.