9 Replies Latest reply on Jul 22, 2011 11:39 AM by kglad

    HELP with syncing streaming music!

    Gunto

      Hello!

      I am developing some kind of a smart music player.

      The player is based on loading the sound from the same directory, because it should be able to play dozens of MP3s, that all exist in that directory. I couldn't add them all of course, because it made the SWF huge (kb size) and slower.

      so im using this little code

       

      var soundToLoad:Sound = new Sound();
      songURL = "song.mp3";
      soundToLoad.loadSound(songURL, true);

      soundToLoad.onLoad = function () {
          soundToLoad.start(0, 1);

      }

       

      This works fine for me, but let's say I want the sound to be synced precisely with the swf.

      The other elements (animation etc.) continue working, because they are synced using getTimer() , so no delay or lag would affect them. (I check every frame what is the time and sync them accordingly).

       

      Now, I want the music to be synced that way too!

      Currently, the mp3 drops out of sync when I double click the window bar and switch to full screen, or when I open another program

      It pauses for half a second, while the elements and animations CONTINUE to work (according to the time)

       

      How do I sync the mp3 too? Maybe checking every few frames what is the time, and then calculating where it shoulve been and go to that certain point in the mp3's timeline?? (using actionscript)

       

      Is the only solution is adding the mp3 to an exact frame ?

       

      Please help, it's kinda urgent

        • 1. Re: HELP with syncing streaming music!
          kglad Adobe Community Professional & MVP

          you can disable streaming your sound and repeatedly use its position property to control which frame(s) of your timeline(s) should be displayed.

          1 person found this helpful
          • 2. Re: HELP with syncing streaming music!
            Gunto Level 1

            Well this is the problem.

            My root's timeline has only one frame right now. The animations are inside movie clips, and they are synced (with a calculation) every frame, using getTimer();

            I want the mp3 to be synced to the time like them! Should I use loadSound like I do now?

            Maybe check every few frames which point of the sound SHOULD'VE played and move to it?

                 How do I even control the sound's timeline, using code?

             

            Maybe attach the sound to that frame? The problem  is, that in this method, the SWF has to contain each one of my dozens of songs...

                 Can I attach a song from a folder using loadSound? Loading it "into" a frame already?

             

            Anyways, these are just suggestions. I just need a solution, for keeping my music in sync (it SHOULD skip parts if the computer is slow...) with the swf's time..

             

            THANKS!!

            • 3. Re: HELP with syncing streaming music!
              kglad Adobe Community Professional & MVP

              again, you can disable streaming your sound and repeatedly use its position  property to control which frame(s) of your timeline(s) should be  displayed.

               

              if you want to attach your sound to a timeline (it must be in your library), then you can set it to sync with that timeline by setting the sync property to stream.

               

              you can skip frames for slow computers but you won't be able to skip parts of your sound.

              • 4. Re: HELP with syncing streaming music!
                Gunto Level 1

                Okay, well how do I get the sounds possition property??? Precisely?

                 

                 

                and in which situation I can skip parts of my sound? I can use code from flash based music players, just to skip to certain points when the computer is delayed.

                again, I have only one frame on the root, and each movie clip works individualy, synced using getTimer.

                 

                If I attach the sound to a certain frame, I gotta have ALL of my sounds in the library already, and won't that make the swf file too big??

                and when I do attach it, how do I change the sync property to stream usind actionscript?

                • 5. Re: HELP with syncing streaming music!
                  kglad Adobe Community Professional & MVP
                  Okay, well how do I get the sounds possition property??? Precisely?

                  use a loop (enterframe or setinterval) to repeatedly check your sound's position property

                   

                  and in which situation I can skip parts of my sound? I can use code from flash based music players, just to skip to certain points when the computer is delayed.

                  skipping parts of a sound won't help with slow computers.

                  again, I have only one frame on the root, and each movie clip works individualy, synced using getTimer.

                   

                  If I attach the sound to a certain frame, I gotta have ALL of my sounds in the library already, and won't that make the swf file too big??

                  i can't determine that.  that's your call.

                   

                  you can preload your swf and its sounds and that is helpful in some situations.  whether it's helpful to you or not is for you to decide.

                   

                  and when I do attach it, how do I change the sync property to stream usind actionscript?

                  after selecting the keyframe with the sound, check the properites panel:  there's a sync property listed with a few choices in a combobox.

                  1 person found this helpful
                  • 6. Re: HELP with syncing streaming music!
                    Gunto Level 1

                    Thanks!!

                     

                    When I was asking how do I get the property, I actually meant WHAT to do every few frames, meaning what is the actionscript for seeing what is the sounds position.

                     

                    In slow computers I can just check (using a loop, same method) and see if I had a lag, skip to the part the sound SHOULD'VE played at that point... Get me?

                    Is that even possible? skipping?

                     

                    One more thing - how do I change sync to stream using actionscript, not manually.

                    • 7. Re: HELP with syncing streaming music!
                      kglad Adobe Community Professional & MVP

                      you can stream a loaded sound but it would sync with any timeline.  there's no way to attach a sound to a timeline unless it's in the library and then sync'g it to stream doesn't make sense.

                       

                      anyway to sync movieclips to a loaded sound use:

                       

                       

                      var tl:MovieClip = this;

                      var fps:Number = 24;  // assign the fps you're using

                      var mcA:Array = [array of your movieclips that you want to sync with your sound];

                      mcF(1);

                       

                      var s:Sound=new Sound();

                      s.onLoad=function(){

                      s.start();

                      tl.onEnterFrame=positionF;

                      }

                       

                      s.loadSound("whatever.mp3",false);

                       

                      function positionF():Void{

                      var n:Number = Math.round(fps*s.position/1000);

                      mcF(n);

                      }

                       

                      function mcF(n:Number):Void{

                      for(var i:Number=0;i<mcA.length;i++){

                      mcA[i].gotoAndStop(n);

                      }

                      }

                      1 person found this helpful
                      • 8. Re: HELP with syncing streaming music!
                        Gunto Level 1

                        Thank you!!

                         

                        Can you give me a more simple code, that would just give me the sound's position every frame in milliseconds (no calculation)?

                        • 9. Re: HELP with syncing streaming music!
                          kglad Adobe Community Professional & MVP

                          :

                           


                           

                          var tl:MovieClip = this;

                          var fps:Number = 24;  // assign the fps you're using

                          var mcA:Array = [array of your movieclips that you want to sync with your sound];

                          mcF(1);

                           

                          var s:Sound=new Sound();

                          s.onLoad=function(){

                          s.start();

                          tl.onEnterFrame=positionF;

                          }

                           

                          s.loadSound("whatever.mp3",false);

                           

                          function positionF():Void{

                          var pos:Number = s.position;

                          var n:Number = Math.round(fps*s.position/1000);

                          mcF(n);

                          }

                           

                          function mcF(n:Number):Void{

                          for(var i:Number=0;i<mcA.length;i++){

                          mcA[i].gotoAndStop(n);

                          }

                          }

                           

                          p.s.  please mark helpful/correct responses, if there are any.

                          1 person found this helpful