fix for looping sound pause issue in AS3


/ Published in: ActionScript 3
Save to your folder(s)

There is a bug in AS3 where when you pause a looping mp3 the saved position gets messed up because the length extends with every looping instance.


Copy this code and paste it in your HTML
  1. private var st:SoundTransform = new SoundTransform(0);
  2. private const max:int = int.MAX_VALUE;
  3. private var context:SoundLoaderContext = new SoundLoaderContext(2000, true);
  4. private var bkdSound:URLRequest = new URLRequest("background.mp3");
  5. private var bkdS:Sound = new Sound();
  6. private var pausePoint:Number = 0.00;
  7. private var paused:Boolean = false;
  8. function init():void{
  9. bkdS.load(bkdSound, context);
  10.  
  11. //START PLAYING THE SOUND
  12. bkdChannel = bkdS.play(0, max, st);
  13. }
  14.  
  15. function pauseSound():void{
  16. if(!paused){
  17.  
  18. //THIS PART IS THE KEY YOU NEED TO % INTO THE LENGTH OF THE SOUND
  19. pausePoint = bkdChannel.position % bkdS.length;
  20. bkdChannel.stop();
  21. paused = true;
  22.  
  23. }else{
  24.  
  25. //USE THIS TO RESUME//
  26. bkdChannel = bkdS.play(pausePoint, max, st );
  27. paused = false;
  28.  
  29. }
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.