How to Reset a Song in AS3

One element required to create a full-featured online song or music player is a reset function. Music enthusiasts often need to reset songs not just to the beginning or the end, but also anywhere in between. Resetting a song to the beginning of the song with ActionScript 3 (Flash's programming language) is easy. It happens automatically every time you reactivate the sound object. Resetting the song back to the position where it was previously stopped is a little more challenging.

Things You'll Need

  • Adobe Flash Professional: CS3, CS4 or CS5 versions
Show More

Instructions

    • 1

      Start the Flash program. Click "Flash File(ActionScript 3.0)" from the splash window to create a new file for an AS3 Flash animation project.

    • 2

      Create three rectangles on the Flash stage with which to make clickable buttons. Assign the first button an instance name of "playSong," the next an instance name of "stopSong," and the next an instance name of "resetSong." Convert these rectangles to MovieClips with the "Convert to Symbol" selection in the "Modify" menu.

    • 3

      Select "Actions" from the "Window" menu on the main Flash menu bar to open the ActionScript 3 editor. Position your mouse cursor on the first line of the ActionScript 3 editor. Click your mouse button, and type in the code below to declare the sound position variable to store the time at which the sound was stopped.

      var soundPosition: Number = 0

    • 4

      Type in the code starting at the next line of the ActionScript 3 editor to load the MP3 music file into a Sound object that will be played through a Flash SoundChannel.

      var nobody:Sound = new Sound();

      var myRequest:URLRequest=new URLRequest("nobody_knows.mp3")

      nobody.load(myRequest);

      var sirenchannelone: SoundChannel

    • 5

      Type in the code starting at the next line of the ActionScript 3 editor to attach mouse click event listeners to each of the buttons that were drawn and given instance names.

      playSong.addEventListener(MouseEvent.CLICK, bluesplay)

      resetSong.addEventListener(MouseEvent.CLICK, redplay)

      stopSong.addEventListener(MouseEvent.CLICK, violetplay)

      Labelstop.addEventListener(MouseEvent.CLICK, violetplay)

    • 6

      Type in the code starting at the next line of the ActionScript 3 editor so that Flash will play the MP3 file when the playSong button is clicked.

      function bluesplay (songevent:MouseEvent): void {

      sirenchannelone = nobody.play();

      }

    • 7

      Type in the code starting at the next line of the ActionScript 3 editor so that Flash will continue playing the MP3 file at the position it was stopped at once the resetSong button is clicked.

      function redplay (songevent:MouseEvent): void {

      sirenchannelone = nobody.play(soundPosition);

      }

    • 8

      Type in the code starting at the next line of the ActionScript 3 editor so that Flash will stop the music after the stopSong button is clicked and then display the time position when the music was stopped.

      function violetplay (songevent:MouseEvent): void {

      if (sirenchannelone){

      trace(sirenchannelone.position)

      soundPosition = sirenchannelone.position

      sirenchannelone.stop();

      sirenchannelone = null;

      }

      trace("stop");

      }

    • 9

      Copy and paste the code to ensure that there are no syntax errors in the Reset Song AS3 program and that the program runs correctly.

      var soundPosition: Number = 0

      var nobody:Sound = new Sound();

      var myRequest:URLRequest=new URLRequest("nobody_knows.mp3")

      nobody.load(myRequest);

      var sirenchannelone: SoundChannel

      playSong.addEventListener(MouseEvent.CLICK, bluesplay)

      resetSong.addEventListener(MouseEvent.CLICK, redplay)

      stopSong.addEventListener(MouseEvent.CLICK, violetplay)

      Labelstop.addEventListener(MouseEvent.CLICK, violetplay)

      function bluesplay (songevent:MouseEvent): void {

      sirenchannelone = nobody.play();

      }

      function redplay (songevent:MouseEvent): void {

      sirenchannelone = nobody.play(soundPosition);

      }

      function violetplay (songevent:MouseEvent): void {

      if (sirenchannelone){

      trace(sirenchannelone.position)

      soundPosition = sirenchannelone.position

      sirenchannelone.stop();

      sirenchannelone = null;

      }

      trace("atop");

      }

    • 10

      Click "Test Movie" in the "Control" menu to play the Reset Song program. Click the playSong button to begin playing the song. Press the stopSong button to stop the song. Observe that the time position that the song was stopped at is displayed in the Output panel. Click the resetSong button and observe that the song has been reset to play at the position at which the song was just stopped.

Learnify Hub © www.0685.com All Rights Reserved