Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong in my AS3 code for a Play Pause button in a flash music player?
    text
    copied!<p>I'm trying to make a simple music player with only one button for Play &amp; Pause the music. What I have now is a MovieClip with:</p> <ul> <li>1st Keyframe: active state for play Button</li> <li>2nd Keyframe: active state for pause button</li> <li>3rd Keyframe: hover state for play button</li> <li>4th Keyframe: hover state for pause button</li> </ul> <p>The script works OK, and play and pause the music without problems as expected.</p> <p>The problem comes after the second click in the play/pause button when the alternance between two states becomes chaotic. The button never returns to the play state although the music was paused.</p> <p>¿Any idea? I'm really getting mad with this. Thanks in advance!</p> <p>This is the code:</p> <pre><code> /**** Objects and Vars ****/ var AutoPlay:Boolean = false; var isPlaying:Boolean = false; var pausePosition:Number = 0; var snd:Sound = new Sound(); //Instantiation var req:URLRequest = new URLRequest ("files/celebrate.mp3"); var status_text:String = ''; /**** Load Files ****/ snd.load(req); var channel:SoundChannel = new SoundChannel(); if(AutoPlay == true) { channel = snd.play(pausePosition); isPlaying = true; play_btn.gotoAndStop(2); status_text = "The song is now playing..."; channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); } function onPlaybackComplete(event:Event):void { isPlaying = false; status_text = "The song is over"; pausePosition = 0; } /******* Listeners ******/ play_btn.addEventListener(MouseEvent.CLICK, playSound); checkPlaying(); function playSound(evt:MouseEvent):void { if(isPlaying == false) { channel = snd.play(pausePosition); isPlaying = true; play_btn.gotoAndStop(2); status_text = "The song is now playing..."; channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); checkPlaying(); } else { pausePosition = channel.position; channel.stop(); isPlaying = false; play_btn.gotoAndStop(1); status_text = "The song is now paused"; } } function checkPlaying(){ if(isPlaying == false){ removeEventListener(MouseEvent.MOUSE_OVER, mouseOverImagePause); removeEventListener(MouseEvent.MOUSE_OUT, mouseOutImagePause); play_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverImagePlay); play_btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutImagePlay); } if (isPlaying == true){ removeEventListener(MouseEvent.MOUSE_OVER, mouseOverImagePlay); removeEventListener(MouseEvent.MOUSE_OUT, mouseOutImagePlay); play_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverImagePause); play_btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutImagePause); } } function mouseOverImagePlay(evt:MouseEvent):void { play_btn.gotoAndStop(3); } function mouseOverImagePause(evt:MouseEvent):void { play_btn.gotoAndStop(4); } function mouseOutImagePlay(evt:MouseEvent):void { play_btn.gotoAndStop(1); } function mouseOutImagePause(evt:MouseEvent):void { play_btn.gotoAndStop(2); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload