leoasis / leoasis/react-sound

How to handle multiple plays of the same sound

Open
#88 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
490
Forks
80
PR merge metrics
No merged PRs in 30d

Description

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}
      ...
   }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing rapid repeated actions using the Sound components in onUserAction, then trace the state updates from onFinishedPlaying through render. Done means each user action plays its sound once, and removing finished components does not replay the remaining sounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
audio-video-rtc, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.