2 Replies Latest reply on Sep 5, 2006 6:52 PM by TripleBrownCow

    Start / Stop Music

    TripleBrownCow
      I'm a relative newbie to Flash so please bear with me if I'm not making sense.

      Anyway, I'm trying to insert background music into the main timeline of my Flash movie. I want that music to start automatically on about the 16th frame of the movie, and loop when the track reaches it's end. I also, want to give the user the ability to turn off the music, if they so choose, and be able to turn it back on again later.

      Here is what I have so far:

      The mp3 in my library has the identifier: firstSound01

      On frame 16 of my main timeline I have a sound object with the following script:
      firstSound = new Sound();
      firstSound.attachSound("firstSound01");
      firstSound.start (0,1000);
      gotoAndStop("stop");

      On a different layer of the maintimeline, starting in frame 16. I have a play button and a stop button (they are two separate buttons).

      The play button has the following script attached to it:
      on (press) {
      if (playing!=true) {
      _root.firstSound.start();
      playing=true;
      _root.myTextBox="Playing"
      }
      _root.firstSound.onSoundComplete =function() (
      _root.myTextBox="Complete"
      playing=false
      }
      }

      The stop button has the following script attached to it:
      on (press) {
      if (playing==true){
      _root.firstSound.stop("firstSound01");
      playing=false;
      _root.myTextBox="Stopped"
      }
      }

      O.k., with all of the above set up, when I play my movie, the music starts when and where it's supposed to, but I cannot stop the music until I first press play, and then, press stop.

      Any suggestions on how to fix this?

      Thanks!
        • 1. Re: Start / Stop Music
          Level 7
          > The play button has the following script attached to it:
          > on (press) {
          > if (playing!=true) {
          > _root.firstSound.start();
          > playing=true;
          > _root.myTextBox="Playing"
          > }
          > _root.firstSound.onSoundComplete =function() (
          > _root.myTextBox="Complete"
          > playing=false
          > }
          > }
          >
          > The stop button has the following script attached to it:
          > on (press) {
          > if (playing==true){
          > _root.firstSound.stop("firstSound01");
          > playing=false;
          > _root.myTextBox="Stopped"
          > }
          > }

          problem with the variables, basically is not set on timeline till you press
          the button, therefor you need to press few times to get it all working.

          David Stiller has nice article about it, read away:
          http://www.quip.net/blog/2006/flash/how-to-toggle-sound-globally


          --
          Best Regards

          Urami


          --


          <urami>
          If you want to mail me - DO NOT LAUGH AT MY ADDRESS
          </urami>
          • 2. Re: Start / Stop Music
            TripleBrownCow Level 1
            Thanks for the reply, but I'm not sure I understand what you're getting at. I read the article you attached, but, again, I was a little confused. Sorry, I'm new at this.

            Let me see... so, you're saying I've got a problem with the script in my buttons?

            So basically, if I drop this script into the frame with my sound object:

            var globalVolume:Sound = new Sound();

            And these scripts into the the actions panel on my buttons:
            Stop Button
            onRelease = function() {
            globalVolume.setVolume(0);
            }

            Start Button
            onRelease = function() {
            globalVolume.setVolume(100);
            }

            Then everything should work?

            Do I still need the following script in my sound object frame?

            firstSound = new Sound();
            firstSound.attachSound("firstSound01");
            firstSound.start (0,1000);
            gotoAndStop("stop");

            Thanks again!