6 Replies Latest reply on Oct 20, 2006 7:28 AM by Paradigm

    Controlling background music with button

    Paradigm Level 1
      I have a presenation that introduces a background piece of music at start-up like this:

      _root.music=new Sound();
      _root.music.attachSound("intro_music");
      _root.music.start(0,999);

      (where intro_music is the identifier for the mp3 in the library. There is also a button acting as an "OFF"/"ON" switch and I want to use the following script to control the playback of the music as this button is pressed by the end-user:

      on(release) {
      if (_root.playing!=1) {
      if (musicPosition==null) {
      musicPosition=0;
      }
      _root.music.start(musicPosition,0);
      _root.playing=1;
      } else {
      if (_root.playing==1) {
      musicPosition=_root.music.position/1000;
      _root.music.stop();
      _root.playing=0;
      }}
      }

      When I employ the button script for the FIRST time (the first press) I actually get the music starting to play again under the initial playback. The second button press silences both playbacks. A further press restarts only one music playback (controlled by the above button script). I'm trying to work out which bit of the button script I need to delete or change for FLASH to recognise that the clip is already playing, but each time I "tweak" it - I break it! I'm primarily a designer, not a programmer, so any easy to follow suggestions on script changes would be great.

      (PS the loop number parameter in the fist script (above) - 999, doesn't make the clip loop at all - is this a bug?)
        • 1. Re: Controlling background music with button
          TimSymons Level 1
          It does make it loop but then you tell it not to loop with restart in the on(release) portion of your code:

          _root.music.start(musicPosition,0); // 0 - no looping.

          However, if you change this "0" to "999" then it will loop but it will also start from the musicPosition location and not from the beginning of the sound. If you want to be able to stop and restart the sound from that position then you will have to build a listening object to capture the onSoundComplete event. This way you can just restart the sound everytime it ends.

          I would change the first part of code to:

          _root.music=new Sound();
          _root.music.attachSound("intro_music");
          music.onSoundComplete = function() {
          _root.music.start(0,0);
          };

          And leave the rest the same.

          Tim
          • 2. Re: Controlling background music with button
            Paradigm Level 1
            Yep, thanks Tim, that works fine, the music is now instantly replaying looping.

            However your code change has stopped the initial (automatic) music playback. I can only get playback once the button is pressed. Subsequent presses toggle off/on OK.

            How can I get that initial playback as soon as the presentation opens?
            • 3. Re: Controlling background music with button
              TimSymons Level 1
              Oops. Sorry about that. I just took out your start function when I copied and pasted it. This should do it.

              _root.music=new Sound();
              _root.music.attachSound("intro_music");
              _root.music.start(0,0); // deleted this by mistake last time.
              music.onSoundComplete = function() {
              _root.music.start(0,0);
              };

              Tim
              • 4. Re: Controlling background music with button
                Paradigm Level 1
                Thanks Tim, that's sorted out the looping of the initial playback of the music. Sounds great.

                But I still need help on editing the second lot of script (for the ON/OFF button) to stop the music starting a second time when the button is initially pressed (as though the end-user is wanting to switch off the music after it automatically starts - see above).
                • 5. Re: Controlling background music with button
                  TimSymons Level 1
                  Sorry, missed that part. To do that, just initialize the playing variable you have to 1.

                  _root.music=new Sound();
                  _root.music.attachSound("intro_music");
                  _root.music.start(0,0);
                  _root.playing = 1; // added this line
                  music.onSoundComplete = function() {
                  _root.music.start(0,0);
                  };

                  Then when you click on the button, it will know that the music is already playing and function as you would expect.

                  Tim
                  • 6. Re: Controlling background music with button
                    Paradigm Level 1
                    Excellent work, thanks Tim, you're a star!

                    This works perfectly, the music starts automatically, and then is stopped by the first click of the ON/OFF switch, which then toggles between on and off with each subsequent press.

                    Thanks also to Kenny Bellew at www.cowfly.com for the initial script ideas.