Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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

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
mike99c
Asked:
mike99c
  • 14
  • 11
2 Solutions
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
mike99cAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
john_steedCommented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
Hey Mike,

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

cheers
0
 
mike99cAuthor Commented:
Yes, that is the identifier and yes, I am seeing the "music started" text.
0
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
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
 
mike99cAuthor Commented:
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
 
john_steedCommented:
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
 
mike99cAuthor Commented:
Thanks John.

Will be testing this next week and then will let you know if it resolves this question.
0
 
mike99cAuthor Commented:
Thanks John. I've got it all sorted now.
0
 
mike99cAuthor Commented:
Thanks a lot for your help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 14
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now