22 Replies Latest reply on May 18, 2009 7:28 AM by Muzak

    Sync two sound object

    LucaL8

      Hi to all!

      I have a little question about play sound in Flex (with AIR).

      I create two Sound Object and load two mp3 file. Now i want that this two file start playing at the same time, when user click a button.

      The problem is that two file, when playing are a little lag (delay of about 1/2 second or less)...

      Where is the problem? Is an actionscript bug or is my interpretation of the sound class incorrect?

      This is my simplified code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
      <mx:Script>
           <![CDATA[
      
                public var sound1:Sound;
                public var sound2:Sound;
                
                public function  init():void
                {
                     var path:String = "C:\\Documents and Settings\\myuser\\Documenti\\song\\";
                     var request1:URLRequest = new URLRequest(path + "file1.mp3");
                     var request2:URLRequest = new URLRequest(path + "file2.mp3");
                     sound1 = new Sound(request1);
                     sound2 = new Sound(request2);
                }
                
                public function StartSong():void
                {
                     sound1.play(0.0, 0.0, null);
                     sound2.play(0.0, 0.0, null);
                }
           ]]>
      </mx:Script>     
           <mx:Button id="Start" x="113" y="101" label="Button" click="StartSong()"/> 

      </mx:WindowedApplication>

       

      Thanks to all in advance.

      Regards.

      Luca

        • 1. Re: Sync two sound object
          Gregory Lafrance Level 6

          I think you're going to have a hard time getting these two sounds to play at exactly the same time. Flex needs to work at least a bit to get the first sound started, so there will be a delay.

          • 2. Re: Sync two sound object
            ATIF FAROOQ Level 3

            Hi,
               If you are targeting your application for fp10 then u can also try
            checking FP10 sound processing.Means play one sound on one hand and get the
            bit stream of other sound and then superimpose them so both sounds play
            together in harmony.If nothing works for you then its no harm in trying this thing too

            1 person found this helpful
            • 4. Re: Sync two sound object
              LucaL8 Level 1

              Thanks for the answer.

              The time that Flex use to play a mp3 file is insignificant (in order of milli seconds), so in fact is trascurable.

              In fact sometimes, when i start my test app (the code that i attach) the sound are started at the same time.

               

              Is it possible that the problem is the file format?

              • 5. Re: Sync two sound object
                LucaL8 Level 1

                I read the FP 10 functionality, and the extract method of Sound Class, but i think is not the correct way.

                In fact i want create a simple "mixer" apps, where user can load 8 mp3, and than when they click on start button all 8 mp3 start at the same time.

                I think is not impossible...

                • 6. Re: Sync two sound object
                  ATIF FAROOQ Level 3

                  yes its definately Possible.I also did something similar recently

                  • 7. Re: Sync two sound object
                    LucaL8 Level 1

                    Sorry, can you give me a hint on this?

                    When yuu say FP10 you mean new event SampleData e new function Extract? I don't know how to implement this.

                    Perhaps you mean that i must create an object Sound with my mp3, then "extract" the byte Array of the Sound and then with this create another object Sound? In this?

                     

                    Thanks a lot.

                    Regards.

                    • 8. Re: Sync two sound object
                      LucaL8 Level 1

                      I can try to do this:

                       

                      1) Create a sound object sound1 and load my mp3.

                      2) Create a sound object sound2 and performe the play (without any loaded mp3)

                      3) On sound2 add handler of event "sampleData" and then in the handler function "extract" the sound data of "sound1" and put on sound2.

                       

                      In this way, if i implement this solution for every Sound object i think is possible, because the event are called async and they should have the same "start delay".

                       

                      Is this the correct way?

                       

                      Now i try to implement.

                      • 9. Re: Sync two sound object
                        LucaL8 Level 1

                        Nothing, the two mp3 has the same lag (delay)...... Uffff....

                        • 10. Re: Sync two sound object
                          Michael Borbor Level 4

                          Have you try playing both files in Audition or any other app, maybe there's a gap at the beginning of one of the audio files that is causing the lag.

                          • 11. Re: Sync two sound object
                            LucaL8 Level 1

                            Hi michael,

                             

                            the two mp3 are the same song, i copied one file to another, in order to avoid this problem...

                            i'm going crazy...

                             

                            Pheraphs is an irresolvable issue?

                            • 12. Re: Sync two sound object
                              Michael Borbor Level 4

                              A threading issue I'll say.

                               

                              Sincerely,

                               

                              Michael

                               

                              El 18/05/2009, a las 4:03, LucaL8 <forums@adobe.com> escribió:

                               

                              >

                              Nothing, the two mp3 has the same lag (delay)...... Uffff....

                              >

                              • 13. Re: Sync two sound object
                                Muzak Level 3

                                Have a look at this:

                                http://www.flexiblefactory.co.uk/flexible/?p=46

                                 

                                The problem with mp3's is the fact that when encoding an mp3, its length changes, making it hard (impossible) to loop them propertly (if that's what you're trying to do).

                                I took your code and loaded 2 sounds (drums + bass) and they synched up perfect. Looping them however results in a hickup every time they "restart".

                                 

                                See this article:

                                http://www.8bitrocket.com/newsdisplay.aspx?newspage=20224

                                1 person found this helpful
                                • 14. Re: Sync two sound object
                                  LucaL8 Level 1

                                  Thanks Muzak,

                                   

                                  so in your opinion the problem is the MP3's encoding.

                                  I don't want looping MP3, i want only that they start at the same time, so the "first" time that they play are sync.

                                   

                                  For your test you use Mp3 file with particular encoding? I try with many mp3 and all havea sync problem.

                                   

                                  One possible solution is to load wave file and then convert it in mp3 in Flex?

                                  • 15. Re: Sync two sound object
                                    Muzak Level 3

                                    I used thse mp3 files:

                                    http://muzakdeezign.com/air/funk_drums.mp3

                                    http://muzakdeezign.com/air/funk_bass.mp3

                                     

                                    For your test you use Mp3 file with particular encoding? I try with many mp3 and all havea sync problem.

                                     

                                    I used MediaCoder to encode from wav to mp3

                                    http://mediacoder.sourceforge.net/

                                     

                                    But from what I can tell (and have read), it doesn't matter which encoder you use.

                                     

                                    here's the code I used:

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                     

                                    <?xml version="1.0" encoding="utf-8"?>
                                    <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="appInit()">
                                     
                                     <mx:Script>
                                      <![CDATA[
                                       
                                       private var sound1:Sound;
                                       private var sound2:Sound;
                                       //-------------------------------------------------------------------------
                                       // appInit()
                                       //-------------------------------------------------------------------------
                                       private function  appInit():void {
                                        trace("Application ::: appInit");
                                        var request1:URLRequest = new URLRequest("loops/funk_drums.mp3");
                                        var request2:URLRequest = new URLRequest("loops/funk_bass.mp3");
                                        //var request3:URLRequest = new URLRequest(path + "G.mp3");
                                        sound1 = new Sound(request1);
                                        sound2 = new Sound(request2);
                                       }
                                       
                                       //-------------------------------------------------------------------------
                                       // startSong()
                                       //-------------------------------------------------------------------------
                                       private function startSong():void {
                                        trace("Application ::: soundCompleteHandler");
                                        sound1.play(0, 0, null);
                                        sound2.play(0, 0, null);
                                       }
                                       
                                       ]]>
                                     </mx:Script>
                                     
                                     <mx:Button id="Start" x="113" y="101" label="Button" click="startSong()"/>
                                     
                                    </mx:WindowedApplication>
                                    

                                     

                                    • 16. Re: Sync two sound object
                                      LucaL8 Level 1

                                      There something wrong.

                                      In my app (i use AIR for standalone apps) if i load your two mp3's, the first that i load start 3/4 dec of second before the other, so don't start correctly.

                                      In fact they are not sync.

                                       

                                      In your case are perfectly synched?

                                       

                                      Another issue could be the sound driver?

                                      • 17. Re: Sync two sound object
                                        Muzak Level 3
                                        In your case are perfectly synched?


                                        Yup. 100% perfect.

                                         

                                         

                                        Another issue could be the sound driver?

                                         

                                        Nope, there's no driver involved. Everything is handled by the Flash Player.

                                        My guess is your PC/Mac is too slow or not enough RAM to handle them both at the same time.

                                        • 18. Re: Sync two sound object
                                          LucaL8 Level 1

                                          It's incredible.

                                          My PC have 3 giga of RAM and Core 2 Duo T5800 processors... It's impossible.

                                          What i can do is try my test app on another pc, but it's very strange.

                                          • 19. Re: Sync two sound object
                                            LucaL8 Level 1

                                            Do you use MAC or PC?

                                            • 20. Re: Sync two sound object
                                              Muzak Level 3
                                              My PC have 3 giga of RAM and Core 2 Duo T5800 processors... It's impossible.

                                              That should be more than enough.

                                              I have a similar setup, 4Gb RAM, Core 2 Duo 3.0 Ghz (running Vista 64bit).

                                               

                                              Which AIR version are you using ?

                                              I'm using 1.5 with Flex 3.2.0 SDK.

                                              • 21. Re: Sync two sound object
                                                LucaL8 Level 1

                                                I'm using Adobe Air 1.5.1.8210 and Flex 3.0.

                                                I've installed Windows XP 32 bit.

                                                 

                                                I search on internet and i found player multitrack and mixer, so is possible to do what i want.
                                                Now i'm searching one "open source" so i can see how they 're implement  this.....
                                                Is very strange that on your system, the same code and the same MP3's works different.

                                                • 22. Re: Sync two sound object
                                                  LucaL8 Level 1

                                                  Last issue,

                                                  i try this:

                                                   

                                                  var a:date = new Date();

                                                  trace("Time: " + a.getMilliseconds.toString());

                                                  sound1.ply(0, 0, null);

                                                  a = new Date();

                                                  trace("Time: " + a.getMilliseconds.toString());

                                                  sound2.play(0, 0, null);

                                                  a = new Date();

                                                  trace("Time: " + a.getMilliseconds.toString());

                                                   

                                                  And the 3 times are equal!!!!!!!!