Solved

Flash MX issue with multiple versions of music track playing when clicking on site navigation buttons

Posted on 2007-11-20
25
184 Views
Last Modified: 2010-04-21
I have an issue with a Flash MX website that I am creating. The website is a very simple one with navigation buttons at the top to the five sections of the website. It also has a stop/start button for the background music track. To create this I did the following:

- Created a movie clip which I then added to a separate layer in the main timeline called "music controls" which I stretched to span across all the website frames.
- Within the movie clip I created two frames, the first frame contains and "off button" with the action script code:
on (press) {
      stopAllSounds();
}
on (release) {
      gotoAndPlay(2);
}
- I have attached the music clip to the first frame by selecting it in the "Sound" drop down in the properties menu.
- The first frame also contains a stop action.
- The second frame contains an "on button" with the following code:
on (release) {
      gotoAndPlay(1);
}
- The second frame also contains a stop action.

What I have done nearly works correctly however when playing the .swf if I continually click on one of the main top navigation buttons for the site I end up with multiple instances of the music track playing at once. It appears to happen fairly randomly as sometimes it starts multiple versions and sometimes it doesn't. I haven't been able to find a method to it.

I'm hoping this is a fairly simple fix but I'm a bit rusty on Flash and can't work out what is wrong myself! I am happy to provide any further information which is needed.  

Thanks!  
0
Comment
Question by:mike99c
  • 14
  • 11
25 Comments
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Hi,

I've included some sourcecode.
Put everything in the firstframe (both buttons as well)  of your soundplaying movie and have the start and stop button call the functions included. Change the path to the mp3 file and it should work.

Hope this helps
var my_sound:Sound = new Sound();
 

btnStop._visible = false;
 

function start_playing() {

	btnStop._visible = true;

	btnPlay._visible = false;

	play_song(intTeller);

}

function play_song() {

	my_sound.loadSound("mp3/file.mp3" , true);

	// my_sound.onSoundComplete = complete_playing;

	my_sound.start();

}

function stop_playing() {

	my_sound.stop();

	btnStop._visible = false;

	btnPlay._visible = true;

}

Open in new window

0
 

Author Comment

by:mike99c
Comment Utility
Thanks a lot for your help John. I've done as suggested but I'm not too sure how to make the buttons call the functions needed. My attempt is as follows:

on (release) {
call function
start_playing();
}

However this doesn't work and brings up syntax errors. If you could help with this it'd be much appreciated!

Thanks again
0
 

Author Comment

by:mike99c
Comment Utility
Please ignore the post above as I appear to have got that working at least (the buttons appear and disable as they are supposed), however the music isn't playing. I have set it up as follows:

Stop button:

on (press){
  stop_playing();
}

Start button:

on (press){
  start_playing();
}
on (release){
  play_song();
}

In the code you gave me I substitued "mp3/file.mp3" for "Here-Comes-The-Sun.mp3" which is the name of the mp3 file and I have uploaded it in the same folder as the main .swf and html index page.

Please let me know if you have any idea why the music isn't playing.

Thanks for your help!
0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Ok, try this

replace this
my_sound.loadSound("mp3/file.mp3" , true);
// my_sound.onSoundComplete = complete_playing;
my_sound.start();

with

my_sound.onLoad = function(success:Boolean) {
   if (success) {
      my_sound.start();
    trace("Sound loaded");
   } else {
      trace("Sound failed");
   }
};
// load the sound
my_sound.loadSound("Here-Comes-The-Sun.mp3", true);
0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
i see there's a bug in start_playing() as well
play_song(intTeller);
should be
play_song();

and leave out this :
on (release){
  play_song();
}

hope this helps
0
 

Author Comment

by:mike99c
Comment Utility
Hi John,

I have made the updates as suggested but still no luck getting the music track to play. Should this work in the test movie preview or do I have to upload it to get it working? (I've done both just to check and neither work).

The code now reads as follows. I see you have put traces in there but I'm not sure how these work and it isn't bringing up anything different. As before the buttons appear to start/stop but there is no music playing.

Thanks for your help.
var my_sound:Sound = new Sound();

 

btnStop._visible = false;

 

function start_playing() {

	btnStop._visible = true;

	btnPlay._visible = false;

	play_song();

}

function play_song() {

my_sound.onLoad = function(success:Boolean) {

   if (success) {

      my_sound.start();

    trace("Sound loaded");

   } else {

      trace("Sound failed");

   }

};

// load the sound

my_sound.loadSound("Here-Comes-The-Sun.mp3", true);
 

}

function stop_playing() {

	my_sound.stop();

	btnStop._visible = false;

	btnPlay._visible = true;

}

Open in new window

0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Hey Mike

try to put the mp3 file in the same directory as your swf output. Then use test movie, the trace results should appear in your output window. If not check your publish settings to make sure they aren't disabled.
0
 

Author Comment

by:mike99c
Comment Utility
Hi John,

Thanks once again. I've put the mp3 file in teh same directory as the swf output and went to test movie however I'm still not able to see the trace results. I've checked the publish settings and "omit trace actions" is unselected.

Any further ideas?
0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Ok, the function might not be executing then. Can you place another trace on the first line in the function "play_song" ? If you don't see that one, place another one in "start_playing".
Also put one on the first line of code in your first frame, to make sure the trace shows up

Good luck ;-)
0
 

Author Comment

by:mike99c
Comment Utility
Thanks John. I have added traces as shown below in the code. I then tested the movie, clicked the start button, then clicked the stop button and it brought up the following trace:

started properly
start playing has executed
play song has executed
stop playing has executed

So it does appear that the trace you suggested is not returning anything.
trace("started properly");
 

var my_sound:Sound = new Sound();

 

btnStop._visible = false;

 

function start_playing() {

	trace("start playing has executed");

	btnStop._visible = true;

	btnPlay._visible = false;

	play_song();

}

function play_song() {

	trace("play song has executed");

my_sound.onLoad = function(success:Boolean) {

   if (success) {

      my_sound.start();

    trace("Sound loaded");

   } else {

      trace("Sound failed");

   }

};

// load the sound

my_sound.loadSound("Here-Comes-The-Sun.mp3", true);
 

}

function stop_playing() {

	trace("stop playing has executed");

	my_sound.stop();

	btnStop._visible = false;

	btnPlay._visible = true;

}

Open in new window

0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Strange, i've pasted it in a new flash document and it works perfectly
I've placed everything in the first frame, buttons on a different layer.

Are you using flash professional ? which version ?
0
 

Author Comment

by:mike99c
Comment Utility
I'm using flash mx - single user license.

Could there be something else in the file which is stopping it from working?
Is there any way to test further where it's going wrong?
Is there anything else that might be wrong with the music file?

The old solution I had pretty much worked except for the multiple instances issue.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Problem with the first solution is probably that the sound is still playing while the movieclip somehow returns to its first frame and starts playing. It's harder to debug, but you might figure out what happens by adding trace commands to the frames

However, you can adjust the second solution by loading the soundfile the same way described here. This should work imo
http://www.video-animation.com/flash_24.shtml
0
 

Author Comment

by:mike99c
Comment Utility
Hi John,
I have imported the sound file as suggested and called it "musictrack.mp3". I then updated the code as below.

Unfortunately the music is still not playing. Any ideas why? Have I updated the code incorrectly?

Thanks
var my_sound:Sound = new Sound();

 

btnStop._visible = false;

 

function start_playing() {

	btnStop._visible = true;

	btnPlay._visible = false;

	play_song();

}

function play_song() {

	my_sound.attachSound("musictrack.mp3");

	// my_sound.onSoundComplete = complete_playing;

	my_sound.start();

	trace("music started");

	

}

function stop_playing() {

	my_sound.stop();

	btnStop._visible = false;

	btnPlay._visible = true;

}

Open in new window

0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Hey Mike,

Is "musictrack.mp3" the Identifier you used for the mp3 ?
are you seeing the "music started" text ?

cheers
0
 

Author Comment

by:mike99c
Comment Utility
Yes, that is the identifier and yes, I am seeing the "music started" text.
0
 
LVL 3

Expert Comment

by:john_steed
Comment Utility
Hey Mike,

I give up on that one, you do have your speakers on, right ? ;-)

What might happen to the first solution is that your main timeline restarts (does it have a stop action in the end ?) or returns to the first frame of the soundobject (keyframe - maybe a buttonaction returns to frame 0 of the main timeline) which might cause it to relaunch the sound
0
 

Author Comment

by:mike99c
Comment Utility
Hi John,

Yes, it does have a stop action at the end. Somehow the movie (and therefore music) is restarting again when you click on buttons (but only sometimes). Do you perhaps have a slightly different solution which might work?

Thanks!
0
 
LVL 3

Accepted Solution

by:
john_steed earned 500 total points
Comment Utility
How about this

you get the buttons out of the music movieclip, give the music movieclip an instancename "musicmovie"
in this mc : 1 frame blank with a stop action, 2 frame contains the music and frame 3 (different layer) a stop action.
and add the buttons to the main movie

playbutton
musicmovie.gotoAndPlay(2);

stopbutton
musicmovie.gotoAndStop(1);
musicmovie.stopAllSounds();


0
 

Author Comment

by:mike99c
Comment Utility
Hi John,

Thanks, we are getting closer though it is still behaving slightly erratically. Now the stop button doesn't stop the music. The code I've got is as follows:

start button:

on (release) {musicmovie.gotoAndPlay(2);
btnStop._visible = true;
      btnPlay._visible = false;

}

stop button:

on (release) {
musicmovie.gotoAndStop(1);
musicmovie.stopAllSounds();
btnStop._visible = false;
btnPlay._visible = true;
}

I'm now able to start the music playing but the stop button somehow doesn't stop it.

I've set up musicmovie as suggested:

- Layer one: keyframe in frame 3 with a stop action.
- Layer two: keyframe in frame 1 with a stop action, music in frame 2.

Also I need to make it so that the music starts playing by default and only shows the off music button instead of both.

Thanks once again! Appreciate your help!  
0
 

Author Comment

by:mike99c
Comment Utility
Hi John,

I added in a plain stopAllSounds(); into the stop button and that seems to have fixed it. I'm still not entirely sure how come this solution works when my original didn't as they seem essentially the same?

The only thing remaining is making the music start automatically instead of when the button is clicked.

Thanks for your continued help with this issue.
0
 
LVL 3

Assisted Solution

by:john_steed
john_steed earned 500 total points
Comment Utility
hey Mike,

No problem, good to hear that you're getting there.
You can make the music start in your main timeline by adding musicmovie.gotoAndPlay(2);
in one of the first keyframes.
When this causes the old behaviour to return, than that means one of your menu buttons causes the main timeline to restart (which you can then search for and fix)

Good luck!
0
 

Author Comment

by:mike99c
Comment Utility
Thanks John.

Will be testing this next week and then will let you know if it resolves this question.
0
 

Author Comment

by:mike99c
Comment Utility
Thanks John. I've got it all sorted now.
0
 

Author Closing Comment

by:mike99c
Comment Utility
Thanks a lot for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now