7 Replies Latest reply on Jun 30, 2009 12:47 PM by kglad

    How to play next song

    alexManuelSilva

      I am trying to build a simple player inside my swf file. The code is a bit messy but it seems to work.

      I´m using a soundChannel for Playback..

      I´m trying to figure out how to have the player play the next song once the previous one has reached its end.

      Do I add some sort of EventListener to the soundChannel?

      Thanks for helping

       

       

      import fl.transitions.*;
      import fl.transitions.easing.*;

       

      prevBt.buttonMode=true;
      playBt.buttonMode=true;
      stopBt.buttonMode=true;
      nextBt.buttonMode=true;
      pauseBt.buttonMode=true;
      pauseBt.visible=false;

       

      var xmlLoader:URLLoader= new URLLoader;
      var temasURL:URLRequest= new URLRequest ("temas.xml");

      xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded)
      xmlLoader.load(temasURL);

       

      var temasXML:XML = new XML();
      temasXML.ignoreWhitespace=true;

      function xmlLoaded(e:Event):void{
            temasXML =XML(xmlLoader.data);
            var temaNum:Number=0;
            var tema:String = temasXML.tema[temaNum].mp3;
            var musica:Sound = new Sound(new URLRequest("audio/"+ tema))
            var sc:SoundChannel;
            var isPlaying:Boolean = false;
            var pos:Number=0;
            

            stopBt.addEventListener(MouseEvent.CLICK, parar_musica);

            function parar_musica(e:MouseEvent):void{
                  sc.stop();
                  isPlaying=false;
                  playBt.visible=true;
                  pos=0;
            }
        

            playBt.addEventListener(MouseEvent.CLICK, tocar_musica);
            function tocar_musica(e:MouseEvent):void{
                  pauseBt.visible=true;
                  playBt.visible=false;
                  if(!isPlaying){

                       sc= musica.play(pos);
                       isPlaying=true;
                       playerTx.text=temasXML.tema[temaNum].legenda;
                 }

            }
        

           pauseBt.addEventListener(MouseEvent.CLICK, pausa_musica);
           function pausa_musica(e:MouseEvent):void{

                 if (isPlaying){
                     pauseBt.visible=false;
                     playBt.visible=true;
                     pos=sc.position;
                     sc.stop();
                     isPlaying=false;
                  }
           }
           nextBt.addEventListener(MouseEvent.CLICK,seguinte);
           function seguinte(e:MouseEvent):void{
                  sc.stop();
                  temaNum +=1;
                  musica= new Sound(new URLRequest("audio/"+ (temasXML.tema[temaNum].mp3)))
                  sc= musica.play();
                  pauseBt.visible=true;
                  playBt.visible=false;
                  isPlaying=true;
                  playerTx.text=temasXML.tema[temaNum].legenda;
           }
           prevBt.addEventListener(MouseEvent.CLICK,anterior);
           function anterior(e:MouseEvent):void{
                  sc.stop();
                  temaNum -=1;
                  musica= new Sound(new URLRequest("audio/"+ (temasXML.tema[temaNum].mp3)))
                  sc= musica.play();
                  pauseBt.visible=true;
                  playBt.visible=false;
                  isPlaying=true;
                  playerTx.text=temasXML.tema[temaNum].legenda;
           }
      }

        • 1. Re: How to play next song
          kglad Adobe Community Professional & MVP

          the sound class has a complete event you can use:

           

          musica.addEventListener(Event.COMPLETE,f);

           

          function f(e:Event){

          //play next

          }

          • 2. Re: How to play next song
            alexManuelSilva Level 1

            Thanks Kglad.

            That's what I was looking for.

            • 3. Re: How to play next song
              alexManuelSilva Level 1

              It doesn´t seem to be working after all.

              apparently, it is not possible to add an EventListener to a Sound class, or is it?

              I tried

               

              musica.addEventListener(Event.COMPLETE,f);

               

              function f(e:Event){

              //play next

              }

               

              but it does nothing...

               

              thanks, anyway

              • 4. Re: How to play next song
                kglad Adobe Community Professional & MVP

                are defining the listener AFTER musica is defined?

                • 5. Re: How to play next song
                  alexManuelSilva Level 1

                  if I declare:

                   

                       var musica:Sound;

                   

                  then add the EL:

                   

                        musica.addEventListener(Event.COMPLETE, song_ended)

                        function song_ended(e:Event):void{
                              trace("finished");
                              pauseBt.visible=false;
                              playBt.visible=true;
                              isPlaying=false;
                        }

                  and finally load the sound into the variable:

                   

                        musica= new Sound(new URLRequest("audio/"+ (temasXML.tema[temaNum].mp3)))

                  I get this error message:

                       TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
                        at index1_fla::player_33/xmlLoaded()
                        at flash.events::EventDispatcher/dispatchEventFunction()
                        at flash.events::EventDispatcher/dispatchEvent()
                        at flash.net::URLLoader/onComplete()

                       (sorry about the untranslated text, I bought this laptop in Germany)

                  which leads me to think that the COMPLETE event in the Sound Class is related to the loading of the sound rather then the playing of the sound (correct me if I´m wrong).

                   

                  On the other hand,

                   

                         var musica:Sound = new Sound(new URLRequest("audio/"+ (temasXML.tema[temaNum].mp3)))

                   

                        musica.addEventListener(Event.COMPLETE, song_ended)

                        function song_ended(e:Event):void{
                              trace("finished");
                              pauseBt.visible=false;
                              playBt.visible=true;
                              isPlaying=false;
                        }

                  doesn´t work either. As a matter of fact, after I´ve written:

                   

                       var musica:Sound = new Sound(new URLRequest("audio/"+ (temasXML.tema[temaNum].mp3)))

                   

                  it´s as if I can no longer add an Ev Listener (it´s not there on the list of options anymore)

                  • 6. Re: How to play next song
                    alexManuelSilva Level 1

                    I´ve just figured it out

                    the soundChannel has the event I was looking for:

                     

                      sc.addEventListener(Event.SOUND_COMPLETE, song_ended);

                    • 7. Re: How to play next song
                      kglad Adobe Community Professional & MVP

                      oops, you're correct.

                       

                      the sound's complete event fires when the sound completes downloading, not when it completes play.