PDA

View Full Version : Stop specific sound (not stopAllSounds)


Asparagus
05-19-2006, 11:22 AM
OK, keep it simple, Asparagus...

I have a band website.
Under Audio/Video, I have a little mp3 player for each song (see code below).
When a user clicks on another menu point (News, Calendar, etc.) I want the player to stop.
But... since I have a background 'ambience' sound I can't just use stopAllSounds.

// mp3Player class
_soundbuftime = 1;
mp3Player = function (songs, interF, reservedDepth) {
if (songs != undefined && interF != undefined && reservedDepth != undefined && typeof songs == "object" && typeof interF == "object" && typeof reservedDepth == "number") {
this.sound = new Object(new Sound(_root));
this.songs = songs;
this.player = interF;
this.positionDump = 0;
this.songs.index = 0;
this.sound.paused = true;
this.sound.stopped = false;
this.curVolume = 100;
this.player.songInfo.html = true;
this.player.songInfo.condenseWhite = true;
this.player.songInfo.autoSize = "left";
this.safeDepth = reservedDepth-1;
this.player.progressIndicator.offset = this.player.progressIndicator._x;
this.loadHandler = _root.createEmptyMovieClip("LH", ++this.safeDepth);
this.progressHandler = _root.createEmptyMovieClip("PH", ++this.safeDepth);
this.volumeHandler = _root.createEmptyMovieClip("VH", this.safeDepth+3);
this.volumeSlideHandler = _root.createEmptyMovieClip("VSH", ++this.safeDepth+4);
// Start functions ----------------------------------------------------------------------------------------------------
this.setRollOver(1);
this.setControls();
this.setInitialVolume(100);
this.sound.loadSound(this.songs[this.songs.index].url, true);
this.showLoaded();
this.showSongInfo();
this.indicateStatus(1);
this.indicateProgress();
// this.enableDrag();
//
} else {
trace("Invalid parameter(s) specified.");
}
};
mp3Player.prototype.stopProgressIndicator = function() {
delete this.progressHandler.onEnterFrame;
};
mp3Player.prototype.resetProgressIndicator = function() {
delete this.progressHandler.onEnterFrame;
this.player.progressIndicator._x = this.player.progressIndicator.offset;
};
mp3Player.prototype.disableDrag = function() {
delete this.player.progressIndicator.onPress;
delete this.player.progressIndicator.onRelease;
delete this.player.progressIndicator.onReleaseOutside;
};
mp3Player.prototype.indicateStatus = function(stat) {
var tar;
stat>0 ? tar=this.player.playButton : stat+1 ? tar=this.player.pauseButton : tar=this.player.stopButton;
this.player.statusIndicator.easeX(tar._x);
};
mp3Player.prototype.showSongInfo = function() {
var artist = this.songs[this.songs.index].artist;
var title = this.songs[this.songs.index].title;
var output = artist+" "+title;
this.player.songInfo.htmlText = output;
};
mp3Player.prototype.next = function() {
this.sound.stop();
this.songs.index++;
if (this.songs.index>this.songs.length-1) {
this.songs.index = 0;
}
this.showSongInfo();
this.sound = new Object(new Sound(_root));
this.sound.loadSound(this.songs[this.songs.index].url, false);
this.sound.setVolume(this.curVolume);
this.showLoaded();
this.indicateStatus(1);
this.indicateProgress();
};

mp3Player.prototype.previous = function() {
this.sound.stop();
this.songs.index--;
if (this.songs.index<0) {
this.songs.index = this.songs.length-1;
}
this.showSongInfo();
this.sound = new Object(new Sound(_root));
this.sound.loadSound(this.songs[this.songs.index].url, true);
this.sound.setVolume(this.curVolume);
this.showLoaded();
this.indicateStatus(1);
this.indicateProgress();
};
mp3Player.prototype.indicateProgress = function() {
var ref = this;
this.progressHandler.onEnterFrame = function() {
var played = ref.sound.position;
var total = ref.songs[ref.songs.index].duration;
ref.player.progressIndicator._x = ref.player.progressIndicator.offset+((played/total)*ref.player.loadWidth);
if (played>=total) {
delete this.onEnterFrame;
ref.resetProgressIndicator();
ref.next();
}
};
};
mp3Player.prototype.showLoaded = function() {
var ref = this;
this.loadHandler.onEnterFrame = function() {
var loaded = ref.sound.getBytesLoaded();
var total = ref.songs[ref.songs.index].totalbytes;
ref.player.loadBar._width = (loaded/total)*ref.player.loadWidth;
if (loaded == total) {
delete this.onEnterFrame;
}
};
};
mp3Player.prototype.setRollOver = function(grow) {
if (grow) {
this.player.previousButton.onRollOver = this.player.stopButton.onRollOver=this.player.paus eButton.onRollOver=this.player.playButton.onRollOv er=this.player.nextButton.onRollOver=function () {
this.grow();
};
this.player.previousButton.onRollOut = this.player.stopButton.onRollOut=this.player.pause Button.onRollOut=this.player.playButton.onRollOut= this.player.nextButton.onRollOut=function () {
this.shrink();
};
} else {
this.player.previousButton._alpha = this.player.stopButton._alpha=this.player.pauseBut ton._alpha=this.player.playButton._alpha=this.play er.nextButton._alpha=30;
this.player.previousButton.onRollOver = this.player.stopButton.onRollOver=this.player.paus eButton.onRollOver=this.player.playButton.onRollOv er=this.player.nextButton.onRollOver=function () {
this.fadeIn();
};
this.player.previousButton.onRollOut = this.player.stopButton.onRollOut=this.player.pause Button.onRollOut=this.player.playButton.onRollOut= this.player.nextButton.onRollOut=function () {
this.fadeOut();
};
}
};
mp3Player.prototype.setControls = function() {
var ref = this;
var playB = this.player.playButton;
var prevB = this.player.previousButton;
var stopB = this.player.stopButton;
var nextB = this.player.nextButton;
var pauseB = this.player.pauseButton;
var volIndic = this.player.volumeIndicator;
volIndic.onPress = function() {
ref.dragVolumeSlider();
};
volIndic.onRelease = volIndic.onReleaseOutside=function () {
delete ref.volumeSlideHandler.onEnterFrame;
};
pauseB.onRelease = function() {
if (!ref.sound.stopped) {
ref.sound.stop();
ref.indicateStatus(0);
ref.sound.paused = true;
ref.sound.stopped = false;
ref.sound.pausedPosition = ref.sound.position;
ref.stopProgressIndicator();
}
};
stopB.onRelease = function() {
ref.sound.stop();
ref.indicateStatus(-1);
ref.sound.stopped = true;
ref.sound.paused = false;
ref.sound.pausedPosition = 0;
ref.resetProgressIndicator();
};
playB.onRelease = function() {
if (ref.sound.stopped) {
ref.indicateProgress();
ref.indicateStatus(1);
ref.sound.start(0, 1);
ref.sound.stopped = false;
} else if (ref.sound.paused) {
ref.indicateProgress();
ref.indicateStatus(1);
ref.sound.start(ref.sound.pausedPosition/1000, 1);
ref.sound.paused = false;
}
};
nextB.onRelease = function() {
ref.resetProgressIndicator();
ref.next();
};
prevB.onRelease = function() {
ref.resetProgressIndicator();
ref.previous();
};
};
mp3Player.prototype.enableDrag = function() {
var ref = this;
this.player.progressIndicator.onPress = function() {
ref.sound.stop();
ref.stopProgressIndicator();
ref.indicateStatus(0);
ref.sound.paused = true;
ref.sound.stopped = false;
total = ref.songs[ref.songs.index].duration;
this.onEnterFrame = function() {
conv = {x:_root._xmouse};
globalToLocal(conv);
this._x = conv.x;
if (conv.x<ref.player.loadBar._x) {
this._x = ref.player.loadBar._x;
}
if (conv.x>ref.player.loadBar._x+ref.player.loadWidth) {
this._x = ref.player.loadBar._x+ref.player.loadWidth;
}
var percent = ((this._x-ref.player.loadBar._x)/ref.player.loadWidth)*100;
this.newPosition = (percent*total)/100;
};
};
this.player.progressIndicator.onRelease = this.player.progressIndicator.onReleaseOutside=fun ction () {
if (this.newPosition>=ref.songs[ref.songs.index].duration) {
this.newPosition = ref.songs[ref.songs.index].duration-1;
}
delete this.onEnterFrame;
ref.sound.start(ref.player.progressIndicator.newPo sition/1000, 1);
ref.sound.paused = false;
ref.sound.stopped = false;
ref.indicateStatus(1);
ref.indicateProgress();
};
};
// creation of mp3Player class instance and junk
songsArray = new Array();
playerInterface = new Object();
playerInterface.playButton = this.playButton;
playerInterface.stopButton = this.stopButton;
playerInterface.pauseButton = this.pauseButton;
playerInterface.previousButton = this.previousButton;
playerInterface.nextButton = this.nextButton;
playerInterface.loadBar = this.loadbar;
playerInterface.loadWidth = 333;
playerInterface.progressIndicator = this.proIndic;
playerInterface.statusIndicator = this.statusIndic;
playerInterface.songInfo = this.songInfo;
playerInterface.volumeBar = this.volumeBar;
playerInterface.volumeNR = this.volumeNR;
playerInterface.volumeIndicator = this.volumeIndic;
playerInterface.volumeHeight = 100;
//---------------------------------------------------
XMLRetrieve = new XML();
XMLRetrieve.ignoreWhite = true;
XMLRetrieve.load("mp3/track01.xml");
XMLRetrieve.onLoad = function() {
for (var j = 0; j<this.firstChild.childNodes.length; j++) {
/* Handle duration and convert to milliseconds */
dur = this.firstChild.childNodes[j].attributes.duration;
dblPnt = dur.indexOf(":");
mins = Number(dur.substr(0, dblPnt));
sec = Number(dur.substr(dblPnt+1, dur.length));
ms = ((mins*60)+sec)*1000;
/* Handle filesize and convert to bytes */
songsArray.push({title:this.firstChild.childNodes[j].attributes.title, artist:this.firstChild.childNodes[j].attributes.artist, url:this.firstChild.childNodes[j].firstChild.nodeValue, duration:ms, totalbytes:Number(this.firstChild.childNodes[j].attributes.filesize)});
}
player = new mp3Player(songsArray, playerInterface, 1);
};

Asparagus
05-21-2006, 02:58 PM
OK, I've kept trying with different things, and have found something that kinda works, but I still need a little help.

In my main menu I've given the buttons the same Instance Name as the stop button in the mp3 player, which is: stopButton
And I've also changed the path, is it looks a little like this...

mp3Player.prototype.setControls = function() {
var ref = this;
var playB = this.player.playButton;
var prevB = this.player.previousButton;
var stopB = this.player.stopButton;
var stopB = _root.menu.menu_homeMC.stopButton;
var stopB = _root.menu.menu_newsMC.stopButton;
var stopB = _root.menu.menu_calendarMC.stopButton;
var stopB = _root.menu.menu_bioMC.stopButton;
var stopB = _root.menu.menu_avMC.stopButton;
var stopB = _root.menu.menu_galleryMC.stopButton;
var stopB = _root.menu.menu_guestbookMC.stopButton;
var stopB = _root.menu.menu_contactMC.stopButton;
var stopB = _root.menu.menu_linksMC.stopButton;
var nextB = this.player.nextButton;
var pauseB = this.player.pauseButton;
};

The problem with this is, that it will stop the music, but only the last one on the list, which in the above example is _root.menu.menu_linksMC.stopButton. The other don't seem to work. Can anyone see what's going wrong?

EdKav1
05-22-2006, 12:41 PM
Have you tried player.sound.stop();?

In your constructor for mp3Player you could set a root or global variable called something like _root.currentMP3Player. In your menu code you could then check if that variable had been set and if so, you could tell it to stop.

I couldn't figure out what you were trying to do with the menu code (your second post).


Ed

Asparagus
05-24-2006, 10:57 AM
i've tried Sound.stop(); with all sorts of different variables, none of which work??

and the strange thing is that if you press the button within menu_linksMC, then it stops?? although the coding is exactly the same, 'links' is the only one that works??

Asparagus
05-25-2006, 02:42 PM
and yes, i have also tried firstSound.stop(); from the kennybellew.com tutorial

woe is me...

Explorer_pl
05-25-2006, 02:50 PM
hi

i've created a special class for handeling multiple sounds.
its posted on www.sitepoint.com in forums.

its called exSound... i'll try to look it up for you.

Explorer_pl
05-25-2006, 03:14 PM
ok. here it is:

http://www.sitepoint.com/forums/showthread.php?t=221864

there is a zip with a class and a sample.fla

hope it helps.

Asparagus
05-26-2006, 01:21 AM
OK, I've kept trying with different things, and have found something that kinda works, but I still need a little help.

In my main menu I've given the buttons the same Instance Name as the stop button in the mp3 player, which is: stopButton
And I've also changed the path, is it looks a little like this...

mp3Player.prototype.setControls = function() {
var ref = this;
var playB = this.player.playButton;
var prevB = this.player.previousButton;
var stopB = this.player.stopButton;
var stopB = _root.menu.menu_homeMC.stopButton;
var stopB = _root.menu.menu_newsMC.stopButton;
var stopB = _root.menu.menu_calendarMC.stopButton;
var stopB = _root.menu.menu_bioMC.stopButton;
var stopB = _root.menu.menu_avMC.stopButton;
var stopB = _root.menu.menu_galleryMC.stopButton;
var stopB = _root.menu.menu_guestbookMC.stopButton;
var stopB = _root.menu.menu_contactMC.stopButton;
var stopB = _root.menu.menu_linksMC.stopButton;
var nextB = this.player.nextButton;
var pauseB = this.player.pauseButton;
};

The problem with this is, that it will stop the music, but only the last one on the list, which in the above example is _root.menu.menu_linksMC.stopButton. The other don't seem to work. Can anyone see what's going wrong?

I just realised that if I remove the line var stopB = _root.menu.menu_linksMC.stopButton; then the one prior to that (menu_contactMC) becomes the active one??
How do I make them all active?

darkzak
05-26-2006, 01:53 AM
mp3Player.prototype.setControls = function() {
var ref = this;
var playB = this.player.playButton;
var prevB = this.player.previousButton;
var stopB = this.player.stopButton;
var stopB = _root.menu.menu_homeMC.stopButton;
var stopB = _root.menu.menu_newsMC.stopButton;
var stopB = _root.menu.menu_calendarMC.stopButton;
var stopB = _root.menu.menu_bioMC.stopButton;
var stopB = _root.menu.menu_avMC.stopButton;
var stopB = _root.menu.menu_galleryMC.stopButton;
var stopB = _root.menu.menu_guestbookMC.stopButton;
var stopB = _root.menu.menu_contactMC.stopButton;
var stopB = _root.menu.menu_linksMC.stopButton;
var nextB = this.player.nextButton;
var pauseB = this.player.pauseButton;
};

The problem with this code is that you are creating a variable stopB and then assigning it a value this.player.stopButton;. But then you are assigning another value _root.menu.menu_homeMC.stopButton; which writes over the previous value. It is like this code:

var x = 0;
var x = 1;
var x = 2;


You cannot expect that to equal 0 + 1 + 2 or anything else instead it is just the last value you assigned 2.

Asparagus
05-26-2006, 01:58 AM
OK, I Frankenstein'ed it and found that if I gave each one a different var and changed the code a liitle bit then it all worked like a charm.

---------------------------------------------------------------
mp3Player.prototype.setControls = function() {
var ref = this;
var playB = this.player.playButton;
var prevB = this.player.previousButton;
var stopB = this.player.stopButton;
var stopB1 = _root.menu.menu_homeMC.stopButton;
var stopB2 = _root.menu.menu_newsMC.stopButton;
var stopB3 = _root.menu.menu_calendarMC.stopButton;
var stopB4 = _root.menu.menu_bioMC.stopButton;
var stopB5 = _root.menu.menu_avMC.stopButton;
var stopB6 = _root.menu.menu_galleryMC.stopButton;
var stopB7 = _root.menu.menu_guestbookMC.stopButton;
var stopB8 = _root.menu.menu_contactMC.stopButton;
var stopB9 = _root.menu.menu_linksMC.stopButton;
var nextB = this.player.nextButton;
var pauseB = this.player.pauseButton;
};
---------------------------------------------------------------

and then this

---------------------------------------------------------------
stopB1.onRelease = function() {ref.sound.stop(); ref.indicateStatus(-1);
ref.sound.stopped = true; ref.sound.paused = false; ref.sound.pausedPosition = 0;
ref.resetProgressIndicator();};

stopB2.onRelease = function() {ref.sound.stop(); ref.indicateStatus(-1);
ref.sound.stopped = true; ref.sound.paused = false; ref.sound.pausedPosition = 0;
ref.resetProgressIndicator();};

etc. etc. etc.
---------------------------------------------------------------

Sorry to have been such a pain, and hopefully someone (after reading this) won't have to go through the same brain ache I have.

darkzak
05-26-2006, 01:58 AM
This would work:

this.player.stopButton.onRelease =
_root.menu.menu_homeMC.stopButton.onRelease =
_root.menu.menu_newsMC.stopButton.onRelease=
_root.menu.menu_calendarMC.stopButton.onRelease =
_root.menu.menu_bioMC.stopButton.onRelease =
_root.menu.menu_avMC.stopButton.onRelease =
_root.menu.menu_galleryMC.stopButton.onRelease =
_root.menu.menu_guestbookMC.stopButton.onRelease =
_root.menu.menu_contactMC.stopButton.onRelease =
_root.menu.menu_linksMC.stopButton.onRelease = function() {
ref.sound.stop();
ref.indicateStatus(-1);
ref.sound.stopped = true;
ref.sound.paused = false;
ref.sound.pausedPosition = 0;
ref.resetProgressIndicator();
};