Sound playing double

  • 3
  • Problem
  • Updated 4 years ago
  • In Progress
I've come across a strange bug when using SM2 (20120318) to play two different sounds after one another, like in a playlist for example. I've put the test page into a gist, but the steps to reproduce basically go like this:

https://gist.github.com/2561726

1. Play first sound.
2. on finish:
2.1 sound1.pause()
2.2 sound1.setPosition(0)
2.3 sound2.setPosition(0)
2.4 sound2.play()
3. Then, after playing sound2 for some amount of time:
3.1 sound2.pause()
3.2 sound1.setPosition(*anywhere*)
3.3 sound1.play()
4. Then, repeat step 2
5. sound2.pause() // boom!

What happens is the sound2.pause() causes the audio to be "doubled" -- two sounds are played very closely on top of each other. Calling sound2.pause() again makes one of the sounds stop but the other continues. soundManager.stopAll() has no effect once it is in this state.

The strange thing is that SM2 seems to have all the correct values for sound2.playState and sound2.paused, however when _flash._pause() is called here https://github.com/scottschiller/Soun... it causes the sound doubling.

The fix I've been able to find is to swap steps 2.3 and 2.4. That is, call sound2.play() before sound2.setPosition(0) -- it's a very strange and annoying bug though!

Cheers,
Nick
Photo of Nick Fisher

Nick Fisher

  • 8 Posts
  • 0 Reply Likes
  • tired of debugging

Posted 4 years ago

  • 3
Photo of Scott

Scott, Official Rep

  • 3854 Posts
  • 251 Reply Likes
Thanks, I'm finally getting through to looking at this one - sounds like a specific case where the internal playback state gets confused, and flash starts playing double where it shouldn't.

Will follow up with updates.
Photo of Scott

Scott, Official Rep

  • 3854 Posts
  • 251 Reply Likes
OK, I think I fixed this, and also found an HTML5 support check I broke earlier on the dev branch. The sound now plays only once, in testing.

Mind grabbing the JS + SWFs from this commit and testing?

The fix involved making the multiShot checks more strict, and forcing Flash to stop and re-start the sound when beginning playback if multiShot is false.

Your JS example was slightly off also as you need to define SM2_DEFER before loading soundmanager2.js in order for deferring to actually work - I recommend a single-line script block above the sm2.js one that defines it. Otherwise, soundManager will be defined right away and I'm not sure if re-instantiating it is safe.

Forked + modified your gist slightly, see yonder.

Good edge-case find; hope this fixes things. Let me know!