3 Replies Latest reply: May 17, 2009 5:58 PM by Craig Grummitt RSS

    How do I loop external audio files?

    EntWebDS Community Member

      I would like to have an external file (actually, one of several randomly selected files, but I can handle that) play and loop but I have yet to understand how to do the latter.  My code works great for playing the file through one time, but when playback completes, it's done.  How do I loop the audio?  Is there perhaps a listener that can wait for the file to finish playing, then call a function to play the sound again?

        • 1. Re: How do I loop external audio files?
          Craig Grummitt Community Member

          You can loop external files by specifying the number of loops when you play the sound file. Look at the Sound.play method. eg.

           

          var audio:Sound = new Sound(new URLRequest("http://www.pacdv.com/sounds/interface_sound_effects/sound3.mp3"));
          audio.play(0, 100);
          
          • 2. Re: How do I loop external audio files?
            EntWebDS Community Member

            I sincerely thank you for the helpful advice.  It answers the question I asked, and I'll mark it as such, but it leaves me hanging (I think) with regard to what I wanted to ask.

             

            I will rephrase the question and put the emphasis where I should have put it to begin with:

             

            Is there an event that fires when an audio file finishes playing so that I can select another audio file to begin the process of loading and streaming again?  The loop solution seems to fall short of accomplishing that goal since it will only loop the same file.

             

            Also, I'm actually using a SoundChannel object, and don't know how to use the number-of-loops argument in this context.  Here is the code I use:

             


            var mysound:URLRequest = new URLRequest('../_RefFiles/bkgdmusic.mp3');
            var sound:Sound = new Sound();
            var controller:SoundChannel;
            var play_mc:StartPlayer = new StartPlayer(); //gif image
            var stop_mc:StopPlayer = new StopPlayer(); //gif image
            var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
            //sound.addEventListener(Event.COMPLETE, soundLoaded);
            sound.load(mysound, context);

             

            //function soundLoaded(evt:Event):void
            //{
                    controller = sound.play(0,200); //Doesn't work...


                    controller.stop();
                    gotoAndPlay("Play");
            //}
            stop();

             

            ****************************

            ["Play" frame]

             

            controller = sound.play();
            addChild(stop_mc);
            stop_mc.y=-7;
            stop_mc.addEventListener(MouseEvent.CLICK, stopSound);

             

            function playSound(evt:MouseEvent):void
            {
                controller = sound.play();
                removeChild(play_mc);
                addChild(stop_mc);
                stop_mc.y = -7;
                stop_mc.addEventListener(MouseEvent.CLICK, stopSound);
                play_mc.removeEventListener(MouseEvent.CLICK, playSound);
            }

             

            function stopSound(evt:MouseEvent):void
            {
                controller.stop();
                removeChild(stop_mc);
                addChild(play_mc);
                play_mc.y = -7;
                play_mc.addEventListener(MouseEvent.CLICK, playSound);
                stop_mc.removeEventListener(MouseEvent.CLICK, stopSound);
            }
            stop();

            • 3. Re: How do I loop external audio files?
              Craig Grummitt Community Member

              In a comment in your code you say:

              controller = sound.play(0,200); //Doesn't work...
              

              however the following line you stop the sound playing:

               controller.stop();

              And then when you next play the sound in the 'Play' frame, you don't specify the number of loops:

              controller = sound.play();

              But to your question, yes looping only refers to looping the file that is playing. Playing another file after the first has stopped playing isn't really referred to as 'looping'. To check whether a sound has finished playing, you need to listen to an event on your soundChannel variable(you've called it controller) called flash.events.Event.SOUND_COMPLETE.(SoundChannel.soundComplete). There's an example in the documentation.