0 Replies Latest reply on Mar 2, 2010 7:20 PM by sunburnol

    MP3 Player Play/Pause?

    sunburnol


      I'm making an MP3 player.  I can't get the play/pause toggle to work.  I'm not sure how to have the event be toggled on and off. Sorry, I'm a newbie to flex.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:net="flash.net.*" xmlns:media="flash.media.*" creationComplete="mySound.load(myURLReq)" themeColor="#000000" borderColor="#000000" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #FFFFFF]">
       
          <mx:Script>
              <![CDATA[
                  private function appInit():void {
                      stage.addEventListener( KeyboardEvent.KEY_DOWN, keyPressHandler );
                  }
                 
                  private function keyPressHandler( e:KeyboardEvent ):void {
                      // if the spaceBar is pressed then toggle play/pause
                      if( e.charCode == 32 ) {
                          if( this.focusManager.getFocus() != toggleBtn ) {
                              this.focusManager.setFocus( toggleBtn );
                              // whatever happens when you click on the button you place here
                              // it's best to have a 'clickHandler' that calls another method
                              toggleBtnHandler();
                          }   
                      }
                  }
                 
                  private function clickHandler( e:Event ):void {
                      toggleBtnHandler();
                  }
                 
                  private function toggleBtnHandler():void {
                      if( toggleBtn.label == "PLAY" ) {
                          toggleBtn.label = "PAUSE";
                      } else {
                          toggleBtn.label = "PLAY";
                      }
                  }
              ]]>
          </mx:Script>

       

      <mx:Button id="toggleBtn"
              x="10" y="70" toolTip="Press the spacebar to toggle label"
              click="clickHandler( event );"
              label="PLAY"/>

       

       
         <mx:Number id="myPos">0</mx:Number>
         <net:URLRequest id="myURLReq" url="01-amy_macdonald-dont_tell_me_that_its_over.mp3" />
         <media:SoundChannel id="mySoundChannel" />
         <media:Sound id="mySound" />
         <mx:Panel title="MP3 Player">
           <mx:Text id="prog" />
           <mx:ControlBar>
             <mx:Button id="play" label="PLAY" click="mySoundChannel = mySound.play(myPos, 0, null);" />
             <mx:Button id="pause" label="PAUSE" click="myPos = mySoundChannel.position; mySoundChannel.stop();" />
             <mx:Button id="stop" label="STOP" click="mySoundChannel.stop();" />
           </mx:ControlBar>
         </mx:Panel>
      </mx:Application>