SetTimeout for a looped audio - expo

Viewed 1302

i want to run an audio in a loop and before each time the audio is played, i want to delayed a chosen amount of time. For example, if i set the delay is 3 seconds and loop is 3 times, when i press play it will wait 3 seconds, play the audio, wait 3 seconds, play audio, wait 3 seconds, play audio. I tried everything but it only delay for the first time and run in a loop with no delay after that. Moreover, the loop wasn't precise as the number of loop set. The method i used for the loop is in this doc:

https://docs.expo.io/versions/latest/sdk/av/#example-loop-media-exactly-20-times

Is there a way in expo to do this? Down below is my code on the problem:

numLoop=3

//function to loop the audio
_onPlaybackStatusUpdate = playbackStatus => {
if (playbackStatus.didJustFinish) {
if (this.state.numberOfLoops == numLoop - 1) {
this.sound.setIsLoopingAsync(false);
console.log(“it’s looping”);
this.setState({ isPlaying: false });
}
this.setState({
numberOfLoops: this.state.numberOfLoops + 1
});
console.log(this.state.numberOfLoops);
}
};

//function run when user press play button
_onPlayPausePressed = () => {
this.setState({ numberOfLoops: 0 });
if (this.sound != null) {
this.sound.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdate);
this.sound.setIsLoopingAsync(true);
this.setState({ isPlaying: !this.state.isPlaying });

  if (this.state.isPlaying) {
    this.sound.pauseAsync();
  } else {
    setTimeout(() => {
      this.sound.playAsync(); //play the audio
      console.log("play the audio")
    }, 3000); //wait for 3 sec
  }
}
};

In the current code i used console log to check the work flow when the play button is pressed. Here is what the result in the console:

play the audio
1
2
it's looping
3
4
5

My expected console would be:

 play the audio
 1
//delay 3 sec
 play the audio
 2
//delay 3 sec
 play the audio
 3
 it's looping

The problem has been solved, down below is how i changed the code incase somebody need it:

//function to loop the audio
_onPlaybackStatusUpdate = playbackStatus => {
    if (playbackStatus.didJustFinish) {
      if (this.state.numberOfLoops >= numLoop - 1) {
        this.sound.pauseAsync();
        this.sound.setIsLoopingAsync(false);
        console.log("it's looping");
        this.setState({ isPlaying: false });
      } else if (this.state.numberOfLoops < numLoop - 1) {
        this.setState({
          numberOfLoops: this.state.numberOfLoops + 1
        });

        this.sound.pauseAsync();
        setTimeout(() => {
          this.sound.playAsync();
          console.log("play the sound");
        }, 2000);
        console.log(this.state.numberOfLoops);
      }
    }
  };

  _onPlayPausePressed = () => {
    this.setState({ numberOfLoops: 0 });
    if (this.sound != null) {
      this.sound.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdate);
      this.sound.setIsLoopingAsync(true);
      this.setState({ isPlaying: !this.state.isPlaying });

      if (this.state.isPlaying) {
        this.sound.pauseAsync();
      } else {
        setTimeout(() => {
          this.sound.playAsync();
          console.log("play the sound");
        }, 2000);
      }
    }
  };
1 Answers

Solution by OP

//function to loop the audio
_onPlaybackStatusUpdate = playbackStatus => {
    if (playbackStatus.didJustFinish) {
      if (this.state.numberOfLoops >= numLoop - 1) {
        this.sound.pauseAsync();
        this.sound.setIsLoopingAsync(false);
        console.log("it's looping");
        this.setState({ isPlaying: false });
      } else if (this.state.numberOfLoops < numLoop - 1) {
        this.setState({
          numberOfLoops: this.state.numberOfLoops + 1
        });

        this.sound.pauseAsync();
        setTimeout(() => {
          this.sound.playAsync();
          console.log("play the sound");
        }, 2000);
        console.log(this.state.numberOfLoops);
      }
    }
  };

  _onPlayPausePressed = () => {
    this.setState({ numberOfLoops: 0 });
    if (this.sound != null) {
      this.sound.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdate);
      this.sound.setIsLoopingAsync(true);
      this.setState({ isPlaying: !this.state.isPlaying });

      if (this.state.isPlaying) {
        this.sound.pauseAsync();
      } else {
        setTimeout(() => {
          this.sound.playAsync();
          console.log("play the sound");
        }, 2000);
      }
    }
  };
Related