4 Replies Latest reply on May 10, 2011 4:33 PM by drkstr_1

    Get length of sound

    K8_1984 Level 1

      Hi there,

       

      I'm trying to get to sound.length property of a sound which is being created:

      {

      s = new Sound();

      s.addEventListener(Event.COMPLETE, addAdditionalInfoToFile); --------------> i need this to get to length property, but it doesn't make the rest of the code wait for the end of the addAdditionalInfoToFile to finish

      if (!s.hasEventListener(Event.ID3))
           s.addEventListener(Event.ID3,onID3InfoReceived);

      s.load(urlReq);

      }

       

      private function addAdditionalInfoToFile(event:Event):void{
                     
                          var mySound:Sound = event.target as Sound;
                          var songLen:Number;
         
                          songLen = mySound.length / mySound.bytesLoaded * mySound.bytesTotal;
                          songLen = Math.round(songLen / 1000);
                          var m:Mp3Player = new Mp3Player();
         
                          trace("event listener"+ String(m.formatTimeValue(s.length/1000))); ---------------------> this is where it gives me a good value of length
      ,
                  }                                                                                                                                         which I need back in another listener
          

                                                                                                                                                             onID3InfoReceived

       

      Problem here:

      • I tried to use global variable like tempLength, and assign s.length to this global value in the above, red place,then pass it in onID3InfoReceived  in certain spot, but it always gives me null. In the addAdditionalInfoToFile it traces properly and gives me , say 4:32 seconds or any other value, but outside of the listener, always null.....is that the case of function not waiting until it goes through all the lines of code in addAdditionalInfoToFile? How can i make the application stop untill it finishes?
      • Any other clues?
        • 1. Re: Get length of sound
          drkstr_1 Level 4

          You would do it the same way you would with any event... write your code so that it doesn't do anything until the event handler is called.

           

          public function doStuff():void
          {
               this.addEventListener("someEvent", someEventHandler);
               this.dispatchEvent( new Event("someEvent") );
          }
          
          private function doSomeOtherStuff():void
          {
               trace("Now I can get back to...");
          }
          
          private function someEventHandler(event:Event):void
          {
               doSomeOtherStuff();
          }
          

           

           

          So in your case, you would probably want to wait and call the rest of your code until you have recieved the Event.COMPLETE from the sound object.

          • 2. Re: Get length of sound
            K8_1984 Level 1

            Ok,

            • write your code so that it doesn't do anything until the event handler  is called - that's more less what I know oin theory, but don't in practice
            • how do I check that I "have recieved the Event.COMPLETE from the sound object." ?
            • why in the world won't the global variable work? I check its value in various places in the code, it was always null....
              • 3. Re: Get length of sound
                K8_1984 Level 1

                Refreshing the topic...I really need a solution on this one...It seems that event complete gives the right length, but when I have many files, and listening for this event in every single one of them, then lengths of those mp3 files weird : first 2 or 3 are always 00:00...looks like there is not enough time to get one, before the request for another...Question - how the hell do I overcome this?

                • 4. Re: Get length of sound
                  drkstr_1 Level 4

                  The standard pratice is to to toggle some kind of Boolean variable, however since it you have multiple sounds objects you probably want to use some kind of counter.

                   

                   

                  Assuming all of your sounds are in an array...

                   

                  private var _numComplete:uint = 0;
                  
                  private var _soundList:Array;
                  
                  protected function startDoingStuff():void
                  {
                       for each(var sound:Sound in _soundList)
                       {
                            sound.addEventListener(Event.COMPLET, sound_completeHandler);
                       }
                       //dont do anything else until all your sounds are ready
                  }
                  
                  protected function finishDoingStuff():void
                  {
                       //by now all of your sounds have finished loading, and you can now reference their length
                  }
                  
                  
                  protected function sound_completeHandler(event:Event):void
                  {
                       _numComplete++;
                  
                       //if we have recieved the same amount of complete events as the number of sounds...
                       if(_numComplete == _soundList.length)
                            finishDoingStuff();
                  }