17 Replies Latest reply on Apr 15, 2014 6:56 AM by resdesign

    One button to play a sequence of different sounds

    philipeagles

      Hi,

       

      I want a create a button which will play different sounds. For example, first click plays sound one, second click plays sound two and so on.

       

      How can I achieve this?

       

      Many thanks

       

      Phil

        • 1. Re: One button to play a sequence of different sounds
          resdesign Adobe Community Professional & MVP

          The same way you would do symbols.

          Here is an idea:

           

          birds= sym.$("birds")[0];

          bounce1= sym.$("bounce1")[0];

          crowdWalk = sym.$("crowdWalk")[0];

          downslide = sym.$("downslide")[0];

          eat = sym.$("eat")[0];

           

           

          var compSounds = [birds,bounce1,crowdWalk,downslide,eat];

           

          var i = 0;

          sym.$('button').click(function(){

          i++;

            compSounds[i].play();

          });

           

          This is assuming all your sounds are on the timeline. If they are in symbols, use the symbol scope like this for example:

          the large2 sound is in the symbol explodefw which itself is in the symbol fireworksAni

           

          large2 = sym.getSymbol("fireworksAni").getSymbol("explodefw").$("large2")[0];

          • 2. Re: One button to play a sequence of different sounds
            philipeagles Level 1

            Hi resdesign,

             

            Thanks for your guidance with this.

             

            I have added the following to my project. It plays the audio but rather than playing the next audio file when it's clicked again, it will repeat the first.

             

            Here is my code:

             

            document.compositionReady

            Audio1=sym.$("AF_Slide_60a1")[0];

            Audio2=sym.$("AF_Slide_60a2")[0];

            Audio3=sym.$("AF_Slide_60b")[0];

            Audio4=sym.$("AF_Slide_60c")[0];

            Audio5=sym.$("AF_Slide_60d")[0];

            Audio6=sym.$("AF_Slide_60e")[0];

            Audio7=sym.$("AF_Slide_60f")[0];

             

            ContinueBtn.click

            var compSounds = [Audio1,Audio2,Audio3,Audio4,Audio5,Audio6,Audio7];

            var i=0;

             

            sym.play();

            i++;

            compSounds[i].play();

             


            Have I added this correctly?

             

            Many thanks,

             

            Phil

            • 3. Re: One button to play a sequence of different sounds
              philipeagles Level 1

              It's okay, I realised what I was doing wrong!.

               

              Thanks for your help

              • 4. Re: One button to play a sequence of different sounds
                resdesign Adobe Community Professional & MVP

                Try this corrected code:

                 

                birds= sym.$("birds")[0];

                bounce1= sym.$("bounce1")[0];

                crowdWalk = sym.$("crowdWalk")[0];

                downslide = sym.$("downslide")[0];

                 

                var compSounds = [birds,bounce1,crowdWalk,downslide];

                 

                i=0;

                 

                birds.play();

                sym.$('soundBtn').click(function(){

                          i++;

                          compSounds[i-1].pause();

                          compSounds[i-1].currentTime = 0;

                          compSounds[i].play();

                });

                \

                 

                sample here:

                https://app.box.com/s/y421c9a4b6n3puv8tbs4

                1 person found this helpful
                • 5. Re: One button to play a sequence of different sounds
                  philipeagles Level 1

                  Hi,

                   

                  Thanks for the updated code, I can confirm it now works correctly. Is it possible to have another button which plays the same set of sounds but in reverse order?

                   

                  Many thanks

                  • 6. Re: One button to play a sequence of different sounds
                    philipeagles Level 1

                    Okay here is what I have so far:

                     

                    Audio1=sym.$("one")[0];

                    Audio2=sym.$("two")[0];

                    Audio3=sym.$("three")[0];

                    Audio4=sym.$("four")[0];

                    Audio5=sym.$("five")[0];

                     

                    var nextSounds = [Audio1,Audio2,Audio3,Audio4,Audio5];

                    var prevSounds = [Audio5,Audio4,Audio3,Audio2,Audio1];

                    var i=0;

                    var j=0;

                     

                    sym.$('Right_btn').click(function(){

                    i++;

                    nextSounds[i-1].pause();

                    nextSounds[i-1].currentTime = 0;

                    nextSounds[i].play();

                    });

                     

                    sym.$('Left_btn').click(function(){

                    j++;

                    prevSounds[j-1].pause();

                    prevSounds[j-1].currentTime = 0;

                    prevSounds[j].play();

                    });

                     

                    This plays fine when you click to reach the last sound and then click to go backwards to the first slide but if you click the right_btn to go forwards again no sound plays. The sound also loses sync with the slides if you click the left and right btns backwards and forwards.

                     

                    I have attached a link: http://www.candidaalbicans.co.uk/Edge/Slider.html

                     

                    And the project files are here: http://www.candidaalbicans.co.uk/Edge/slider.zip

                     

                    I would really appreciate some help with this.

                     

                    Thanks

                    • 7. Re: One button to play a sequence of different sounds
                      resdesign Adobe Community Professional & MVP

                      corrected file: https://app.box.com/s/u2p7jlsbqlycmp0e3p90

                      Try this instead with only one array.

                      Audio1=sym.$("one")[0];

                      Audio2=sym.$("two")[0];

                      Audio3=sym.$("three")[0];

                      Audio4=sym.$("four")[0];

                      Audio5=sym.$("five")[0];

                       

                      var nextSounds = [Audio1,Audio2,Audio3,Audio4,Audio5];

                      //var prevSounds = [Audio5,Audio4,Audio3,Audio2,Audio1];

                      var i=0;

                       

                      sym.$('Right_btn').click(function(){

                      i++;

                       

                      nextSounds[i].pause();

                      nextSounds[i-1].currentTime = 0;

                      nextSounds[i].play();

                      });

                       

                      sym.$('Left_btn').click(function(){

                      i--;

                      nextSounds[i].pause();

                      nextSounds[i-1].currentTime = 0;

                      nextSounds[i].play();

                      });

                      • 8. Re: One button to play a sequence of different sounds
                        philipeagles Level 1

                        Thank you once again!

                         

                        Here is my final code:

                         

                        // insert code to be run when the composition is fully loaded here

                        Audio1=sym.$("one")[0];

                        Audio2=sym.$("two")[0];

                        Audio3=sym.$("three")[0];

                        Audio4=sym.$("four")[0];

                        Audio5=sym.$("five")[0];

                         

                        var nextSounds = [Audio1,Audio2,Audio3,Audio4,Audio5];

                        //var prevSounds = [Audio5,Audio4,Audio3,Audio2,Audio1];

                        var i=0;

                         

                        sym.$('Right_btn').click(function(){

                        i++;

                        //nextSounds[i].pause();

                        //nextSounds[i-0].currentTime = 0;

                        nextSounds[i].play();

                        sym.getComposition().getStage().getSymbol("SceneHolderSymbol").play();

                        });

                         

                        sym.$('Left_btn').click(function(){

                        i--;

                        //nextSounds[i].pause();

                        //nextSounds[i-0].currentTime = 0;

                        nextSounds[i].play();

                        sym.getComposition().getStage().getSymbol("SceneHolderSymbol").playReverse();

                        });

                         

                        I had to comment out these two lines because they caused the sound to not work past the first sound clip on an iPad:

                         

                        nextSounds[i].pause();

                        nextSounds[i-0].currentTime = 0;

                        • 9. Re: One button to play a sequence of different sounds
                          resdesign Adobe Community Professional & MVP

                          Awesome! Glad it worked for you! Good luck on your project.

                          • 10. Re: One button to play a sequence of different sounds
                            philipeagles Level 1

                            Hi, I'm back again with another question I would really appreciate your help with?

                             

                            The code works great apart from when you click the left or right btn before the audio has finished, the next audio will start playing over the top of the previous audio. Is there any way of controlling this so that if the user clicked before the audio had ended, it would pause the currently playing audio and then start to play the next audio?

                             

                            To further complicate things this project has to be iPad friendly...

                             

                            Thanks again.

                            • 11. Re: One button to play a sequence of different sounds
                              resdesign Adobe Community Professional & MVP

                              In my original code I use i-1 because but you used 1-0 which does not makesense to me. i-1 mean if for example you want to play sound i=2, it will stop i=2-1 or i=1 the previous sound.
                              Hope this makes sense.

                              • 12. Re: One button to play a sequence of different sounds
                                philipeagles Level 1

                                Hi, the reason why I changed the code to I-0 is because I have a play button at the very start which plays the first audio file for the first slide and from then on the left&right buttons take over playing the remaining audio. If I left the code at I-1 when the user clicks on the right button it would go to the second slide but play the first audio.

                                 

                                I have changed it back to i-1 but the audio still overlaps.

                                • 13. Re: One button to play a sequence of different sounds
                                  philipeagles Level 1

                                  Ignore what I wrote above, regarding why I changed the code to i-0 because it still works the same as when it is i-1

                                  • 14. Re: One button to play a sequence of different sounds
                                    philipeagles Level 1

                                    Looking back at your original code, it seems I was missing a i-1 on this line: nextSounds[i-1].pause();

                                     

                                    I have added this in and the audio now pauses correctly when clicking the right button but when you select the left button it will overlap again.

                                     

                                    I have added the code below:

                                     

                                    // insert code to be run when the composition is fully loaded here

                                    Audio1=sym.$("VTE_Slide_13b")[0];

                                    Audio2=sym.$("VTE_Slide_14")[0];

                                    Audio3=sym.$("VTE_Slide_14ab")[0];

                                    Audio4=sym.$("VTE_Slide_15ab")[0];

                                    Audio5=sym.$("VTE_Slide_15c")[0];

                                     

                                    var nextSounds = [Audio1,Audio2,Audio3,Audio4,Audio5];

                                    var i=0;

                                     

                                    Scene1=sym.getComposition().getStage().getSymbol("SceneHolderSymbol").getSymbol("Scene1");

                                    Scene2=sym.getComposition().getStage().getSymbol("SceneHolderSymbol").getSymbol("Scene2");

                                    Scene3=sym.getComposition().getStage().getSymbol("SceneHolderSymbol").getSymbol("Scene3");

                                    Scene4=sym.getComposition().getStage().getSymbol("SceneHolderSymbol").getSymbol("Scene4");

                                    Scene5=sym.getComposition().getStage().getSymbol("SceneHolderSymbol").getSymbol("Scene5");

                                     

                                    var nextScene = [Scene1,Scene2,Scene3,Scene4,Scene5];

                                    var s=0;

                                     

                                    sym.$('Right_btn').click(function(){

                                    i++;

                                    nextSounds[i-1].pause();

                                    nextSounds[i-1].currentTime = 0;

                                    nextSounds[i].play();

                                     

                                    sym.getComposition().getStage().getSymbol("SceneHolderSymbol").play();

                                    s++;

                                    nextScene[s].play(0);

                                     

                                    });

                                     

                                    sym.$('Left_btn').click(function(){

                                    i--;

                                    nextSounds[i-1].pause();

                                    nextSounds[i-1].currentTime = 0;

                                    nextSounds[i].play();

                                     

                                     

                                     

                                    sym.getComposition().getStage().getSymbol("SceneHolderSymbol").playReverse();

                                    s--;

                                    nextScene[s].play(0);

                                    });

                                    • 15. Re: One button to play a sequence of different sounds
                                      resdesign Adobe Community Professional & MVP

                                      In the back button do the opposite which is i+1

                                      • 16. Re: One button to play a sequence of different sounds
                                        philipeagles Level 1

                                        That works! and it was something so simple, very frustrating! Thanks again x

                                        • 17. Re: One button to play a sequence of different sounds
                                          resdesign Adobe Community Professional & MVP

                                          No problem - always a pleasure to help others.

                                          Now, go help someone else!