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

How to handle multiple plays of the same sound #88

Open
lucas-rudd opened this issue Mar 24, 2020 · 0 comments
Open

How to handle multiple plays of the same sound #88

lucas-rudd opened this issue Mar 24, 2020 · 0 comments

Comments

@lucas-rudd
Copy link

lucas-rudd commented Mar 24, 2020

I have an application where the user may take an action in which a sound effect plays.

The user may take this action many times, leading to the sound effect being played multiple times before the prior sound effect has finished.

To accomplish this, I'm pushing all the Sound Components into an array, putting them onto the component state, and attempting to remove them from the state in the onFinishedPlaying so that they get removed from the DOM.

However, since removing them from the array updates the state, the sounds play again.

So, if I the user pressed a key 10 times, the sounds play fine the inital ten times, but then, they play 9 times, then 8 times, then 7 times, then 6 times, etc. as they are all updating the state to remove themselves from the DOM.

What's the suggested way to go about doing this?

This is analogous to what I have in my code in an attempt to accomplish this

onUserAction = () => {
   sounds.push(
          <Sound
            url={SFX}
            playStatus={MusicState.PLAYING}
            loop={false}
            onFinishedPlaying={() => {
              const [
                _,
                ...newSoundComponents
              ] = this.state.SoundComponents;
              this.setState({
                soundComponents: newSoundComponents,
                isSFXPlaying: sounds.length !== 0
              });
            }}
   );
}
this.setState({
  soundComponents: newSoundComponents,
   isSFXPlaying: sounds.length !== 0
}

render() {
   return {
      ...
      {this.state.isSFXPlaying && this.state.soundComponents}
      ...
   }
}
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

1 participant