Skip to main content
Participating Frequently
August 22, 2009
Question

How make mp3 player stop when moving to different pages

  • August 22, 2009
  • 3 replies
  • 3699 views

Hi, I new to Flash  and Action Script 3.0.  I've just designed my first Flash website (for a band), and one of the pages has an MP3 player on the audio page.  It plays fine, as long as you're on that page.  But the problem is, when you go to a different page in the website, it keeps playing, and then when you return to the audio page, a new instance of the song that's playing starts playing on top of the song that's already playing. It's a jumble of sound that can't be stopped unless you close out of the website.

How can I make the mp3 player stop when someone leaves the audio page?  I'd like the songs to just play on that one page, and then stop if someone clicks a tab for a different page. I hope if I paste the code for the player below, somebody (please!) can help.  Thank you.

var my_songs:XMLList;
var my_total:Number;

var my_sound:Sound;
var my_channel:SoundChannel;

var current_song:Number = 0;

var song_position:Number;
var song_paused:Boolean;

var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("playlist.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);

function processXML (e:Event):void{
var myXML:XML = new XML(e.target.data);

my_songs = myXML.SONG;
my_total = my_songs.length();

myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader = null;

}

function playSong(mySong:Number):void{
var myTitle = my_songs[mySong].@TITLE;
var myArtist = my_songs[mySong].@ARTIST;
var myURL = my_songs[mySong].@URL;

title_txt.text = myTitle;
artist_txt.text = myArtist;

if (my_channel){
my_channel.stop();
my_channel.removeEventListener(Event.SOUND_COMPLETE, onNext);
}

my_sound = new Sound();
my_sound.load(new URLRequest(myURL));
my_channel = my_sound.play();
my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);
}

next_btn.addEventListener(MouseEvent.CLICK, onNext);
function onNext(e:Event):void{ //This used to have MouseEvent, change it to Event
current_song++;
if (current_song>=my_total){
current_song=0;
}
playSong(current_song);
}

prev_btn.addEventListener(MouseEvent.CLICK, onPrev);
function onPrev(e:MouseEvent):void{
current_song--;
if (current_song<0){
current_song = my_total-1;
}
playSong(current_song);
}

pause_btn.addEventListener(MouseEvent.CLICK, onPause);
function onPause(e:MouseEvent):void{
if (my_channel){
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
}

play_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onPlay(e:MouseEvent):void{
if (song_paused){
my_channel = my_sound.play(song_position);
song_paused=false;
} else if (!my_channel){
playSong(current_song);
}
}

This topic has been closed for replies.

3 replies

maryannm4Author
Participating Frequently
August 22, 2009

Thank you for trying, but I'm lost.  No matter where I put the code, there were errors. I've just started learning AS3, so nothing about it is coming very easy.

kglad
Community Expert
August 22, 2009

do you have buttons that you use to navigate your swf?  if so, show the code used for one of your buttons.

Known Participant
February 26, 2010

if your music player is mplayer, you can use:

mplayer.addEventListener(Event.REMOVED_FROM_STAGE,stopsoundF);

function stopsoundF(e:Event){

// stop your sound

}


Hey Kglad,


I read about this but it doesn't seem to work for me.  Car3 is a music page.  It contains the XML mp3 player with the buttons.

I added your code in as you mentioned.  When I play the music, hit say home page.......home slides in, music still on. I can send you the URL through message if you want to take a look.

Code as follows:

//must add for all tween and easing

import fl.transitions.Tween;

import fl.transitions.easing.*;

//buttons association with movie clips

btn1.mc=car1;

btn2.mc=car2;

btn3.mc=car3;

btn4.mc=car4;

btn5.mc=car5;

var curMarkerXPos:Number = btn1.x;

var duration:Number = 1.3;

function markerFollow (event:MouseEvent):void {

var markerTween:Tween = new Tween(marker, "x", Strong.easeOut, curMarkerXPos, event.target.x, duration, true);

curMarkerXPos = event.target.x;

}

btn1.addEventListener(MouseEvent.ROLL_OVER, markerFollow);

btn2.addEventListener(MouseEvent.ROLL_OVER, markerFollow);

btn3.addEventListener(MouseEvent.ROLL_OVER, markerFollow);

btn4.addEventListener(MouseEvent.ROLL_OVER, markerFollow);

btn5.addEventListener(MouseEvent.ROLL_OVER, markerFollow);

//movie clip car1 possition to start the movie

car1.x=0;

car1.y=0;

//button control and event listeners

var currentMC:MovieClip = car1;

btn1.addEventListener(MouseEvent.CLICK,f);

btn2.addEventListener(MouseEvent.CLICK,f);

btn3.addEventListener(MouseEvent.CLICK,f);

btn4.addEventListener(MouseEvent.CLICK,f);

btn5.addEventListener(MouseEvent.CLICK,f);

//you add this code to each button.  make the movie clip act as button on rollover

btn1.mouseChildren=false;

btn1.buttonMode=true;

btn2.mouseChildren=false;

btn2.buttonMode=true;

btn3.mouseChildren=false;

btn3.buttonMode=true;

btn4.mouseChildren=false;

btn4.buttonMode=true;

btn5.mouseChildren=false;

btn5.buttonMode=true;

var tweenOut:Tween;

var tweenIn:Tween;

//your defined function.  tells current MC to slide out as new one slides in.

function f(e:MouseEvent) {

tweenOut=new Tween(currentMC,"x",Strong.easeOut,currentMC.x,stage.stageWidth,2,true);

currentMC=e.currentTarget.mc;

currentMC.x=-currentMC.width;

tweenIn=new Tween(currentMC,"x",Strong.easeOut,currentMC.x,0,2,true);

}

car3.addEventListener(Event.REMOVED_FROM_STAGE,stopsoundF);

function stopsoundF(e:Event){

// stop your sound

}

maryannm4Author
Participating Frequently
August 22, 2009

Thank you. All of the website pages are movie clips, and the mp3 player is inside the audio page movie clip.  Do I put the code you suggested (below) on the main timeline, at the end of the audio movie clip frame, inside the audio page movie clip, or at the end of the existing code for the mp3 player?


Thanks for your patience . . .

execute

my_channel.stop();

just prior to exiting that page.

kglad
Community Expert
August 22, 2009

if my_channel is defined on a movieclip's timeline (say mc) and mc is on the timeline that contains your navigation code, then in your navigation code use:

mc.my_channel.stop();

kglad
Community Expert
August 22, 2009

execute

my_channel.stop();

just prior to exiting that page.