10 Replies Latest reply on Apr 5, 2010 7:52 PM by muskin00

    AS 3.0 Sound question?

    Shauri
      How do I pause a sound and resume it using AS 3.0? It's bizarre but I have been unable to locate any resource that explains how to do it.

      You can stop a sound using the SoundChannel object, but there is no resume or play. You can play a sound using the Sound object, but there is no pause or stop. Can someone enlighten me? *I am using Actionscript 3.0*

      Thanks!
        • 1. Re: AS 3.0 Sound question?
          Level 7
          Shauri,

          > How do I pause a sound and resume it using AS 3.0?

          It's a bit different from AS2, but not very hard. :)

          > It's bizarre but I have been unable to locate any resource
          > that explains how to do it.

          Really? It's definitely spelled out in the Sound and SoundChannel class
          entries of the ActionScript 3.0 Language Reference -- but it may not be
          spelled out especially clearly.

          > You can stop a sound using the SoundChannel object, but
          > there is no resume or play.

          The Sound.play() method receives an optional parameter the allows you to
          specify an offset. If you don't use it, the sound starts again from the
          beginning. If you do use it, you can resume from where you left off by
          using the SoundChannel.position property to determine where the sound
          halted.

          > You can play a sound using the Sound object, but there is no
          > pause or stop. Can someone enlighten me?

          I'll do my best. :) The paradigm shift, in this case, is to use these
          two classes in conjunction with each other, much like TextField and
          TextFormat (or, say, TextField and StyleSheet) in AS2.

          Here's a way to get an external MP3 file playing.

          var snd:Sound = new Sound();
          var ch:SoundChannel;

          snd.load(new URLRequest("ABC.mp3"));
          ch = snd.play();

          In these four lines, we declare two variables: snd (of type Sound) and
          ch (of type SoundChannel). The snd variable is not only declared, but also
          set to an instance of the Sound class. Then, the Sound.load() method is
          invoked on the Sound instance (snd) and instructed to request an MP3 file
          named "ABC.mp3" by way of a URLRequest instance. Finally, the previously
          declared SoundChannel variable (ch) is set to the return value of the
          Sound.play() method, which returns a SoundChannel instance.

          At this point, we have a pay to start and stop the sound. To start the
          sound -- as we've already seen -- all you need is the Sound.play() method.
          To stop it, you must invoke SoundChannel.stop() on the SoundChannel
          instance:

          ch.stop();

          To restart from the beginning, re-assign the SoundChannel instance like
          this:

          ch = snd.play();

          ... so that you can stop it again whenever you want.

          To restart from where you left off, use the SoundChannel.position
          property as the optional parameter for Sound.play(). When you're ready to
          stop, not the current position in a variable and use that variable later:

          var pos:Number;

          // perhaps in response to a button ...
          pos = ch.position;
          ch.stop();

          // perhaps in response to another ...
          ch = snd.play(pos);

          Does that give you a good start?


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: AS 3.0 Sound question?
            kglad Adobe Community Professional & MVP
            check the sound class. it has a load() method (to load an external sound file) and a play() method (that will start your sound playing).

            the play() method applied to a sound creates a soundchannel instance that has a stop() method and a position property. you use those to stop your sound and restart your sound from where you last stopped.
            • 3. Re: AS 3.0 Sound question?
              Shauri Level 1
              Thanks a bunch, I will give your advice a go on Monday!
              • 4. Re: AS 3.0 Sound question?
                kglad Adobe Community Professional & MVP
                you're welcome. (i think.)
                • 5. Re: AS 3.0 Sound question?
                  dieslx
                  I have a question on this as well - I am using channel.position to pause, play, seek forward, and seek back on an mp3. My SOUND_COMPLETE function fires correctly UNLESS I pause and resume playing based on channel.position. If I use any seeking or pausing functions, the song ends without firing the event, and the channel.position milliseconds are less than the music.length at the end of playback. Here is the code (sorry for the length)- any ideas would be greatly appreciated. Thanks!

                  var music:Sound = new Sound();
                  var channel:SoundChannel;
                  music.load(new URLRequest(playerSource));

                  ///Register Event Listeners//////////////////////////////////////
                  music.addEventListener(Event.COMPLETE, completeHandler);
                  seekForward_btn.addEventListener(MouseEvent.CLICK, clickForwardHandler);
                  seekBack_btn.addEventListener(MouseEvent.CLICK, clickBackHandler);
                  play_btn.addEventListener(MouseEvent.CLICK, clickPlayHandler);
                  pause_btn.addEventListener(MouseEvent.CLICK, clickPauseHandler);

                  function completeHandler(event:Event):void{
                  channel = music.play(0,0);
                  channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
                  //convert milliseconds to minutes and seconds
                  //var estimatedLength:int = Math.ceil(music.length / (music.bytesLoaded / music.bytesTotal));
                  progBar.value = channel.position;
                  progBar.maximum = music.length;
                  progBar.minimum = 0;
                  var ns_seconds:Number = music.length / 1000;
                  var minutes:Number = Math.floor(ns_seconds/60);
                  var seconds:Number= Math.floor(ns_seconds%60);
                  var totalTime_str:String = ((minutes>=10) ? minutes : "0"+minutes)+":"+((seconds>=10) ? seconds : "0"+seconds);
                  mp3Time_txt.text = totalTime_str;

                  var positionTimer:Timer = new Timer(50);
                  positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
                  positionTimer.start();
                  }


                  function positionTimerHandler(event:TimerEvent):void{
                  //var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength));
                  var playTime:int = channel.position/1000;
                  var minutes2:Number = Math.floor(playTime/60);
                  var seconds2:Number = Math.floor(playTime%60);
                  var elapsedTime_str:String = ((minutes2>=10) ? minutes2 : "0"+minutes2)+":"+((seconds2>=10) ? seconds2 : "0"+seconds2);
                  mp3Progress_txt.text=elapsedTime_str;
                  display2_txt.text = elapsedTime_str;
                  progBar.setProgress(channel.position, music.length);
                  //trace(channel.position + "///" + progBar.value +"///" + music.length);
                  }

                  function onPlaybackComplete(event:Event):void{
                  channel.stop();
                  trace(channel.position + "///" + progBar.value +"///" + music.length);
                  }

                  ///Controller Functions///////////////////////////////////////////////////////
                  function clickForwardHandler (event:MouseEvent):void{
                  var currentPosition:int = channel.position;
                  if(currentPosition <= music.length - 1000){
                  channel.stop();
                  channel = null;
                  channel = music.play(currentPosition + 1000);
                  trace(currentPosition);
                  }
                  }

                  function clickBackHandler (event:MouseEvent):void{
                  var currentPosition:int = channel.position;
                  if(currentPosition >= 1000){
                  channel.stop();
                  channel = null;
                  channel = music.play(currentPosition - 1000);
                  }
                  }

                  function clickPauseHandler (event:MouseEvent):void{
                  channel.stop();
                  }

                  function clickPlayHandler (event:MouseEvent):void{
                  var currentPosition:int = channel.position;
                  if(currentPosition > 0) {
                  channel.stop();
                  channel = null;
                  channel = music.play(currentPosition);
                  }
                  else{
                  channel = music.play(0, 0);
                  }
                  • 6. Re: AS 3.0 Sound question?
                    kglad Adobe Community Professional & MVP
                    you're creating a new soundchannel instance every time you pause, play etc. you must add any listeners you want to the new instances.

                    and you should be removing the listeners from nulled soundchannel instances (or use a weak reference in you addeventlistener) so they can be gc'd.
                    • 7. Re: AS 3.0 Sound question?
                      dieslx Level 1
                      that was the problem-did not register new listeners for the new channel instances-thank you so much!!
                      • 8. Re: AS 3.0 Sound question?
                        kglad Adobe Community Professional & MVP
                        you're welcome.
                        • 9. Re: AS 3.0 Sound question?
                          muskin00

                          .