Mega Code Archive

 
Categories / Flex / Development
 

Play and Pause an MP3 File

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">     <mx:HBox width="400" height="300">         <mx:Script>                              import mx.collections.ArrayCollection;                      public var sound:Sound;                 public var mySoundChannel:SoundChannel;                 public var pausePos:int = 0;                      private function loadSound():void {                     sound = new Sound();                     sound.addEventListener(Event.SOUND_COMPLETE, soundComplete);                     var req:URLRequest = new URLRequest("http://localhost/a.mp3");                     sound.load(req);                     pausePos = 0;                     mySoundChannel = sound.play();                 }                 private function soundComplete(event:Event):void {                     sound.load(new URLRequest("http://localhost/a.mp3"));                     mySoundChannel = sound.play();                 }                      private function playPauseHandler():void{                     if(pausePlayBtn.selected){                         pausePos = mySoundChannel.position;                         mySoundChannel.stop();                     } else {                         mySoundChannel = sound.play(pausePos);                     }                 }                        </mx:Script>         <mx:Button label="Play" id="cb" click="loadSound()"/>         <mx:Button label="start" id="pausePlayBtn" toggle="true" click="playPauseHandler()"/>         <mx:Button label="stop" click="mySoundChannel.stop()"/>     </mx:HBox> </mx:Application>