Hi everyone!!
I’m working on adding music controls to my site. I have a player that I downloaded and edited to fit my needs, but the original had a bug or something b/c the song will pause when the pause button is pushed, but it won’t resume when you push the play button. The button changes back to the pause button as if it had worked, but it doesn’t actually resume, if you keep clicking it just switches buttons back and forth. Everything else seems to work with it, so I would like to just figured this out. The code is separated onto a few different layers so I’ll just share them all:
On my “Functions” layer:
/*******************************
function formatTime()
feed it time in milliseconds; it returns time formatted
minutes:seconds:remainder
thanks to Philip Kerman for this code
*********************************/
function formatTime (theSource) {
var elapsedTime = theSource;
//Minutes
var elapsedM = Math.floor (elapsedTime / 60000);
var remaining = elapsedTime - (elapsedM * 60000);
//add a leading zero if it's a single digit number
if (elapsedM < 10) {
elapsedM = "0" + elapsedM;
}
//Seconds
var elapsedS = Math.floor (remaining / 1000);
remaining -= (elapsedS * 1000);
////add leading zero
if (elapsedS < 10) {
elapsedS = "0" + elapsedS;
}
//Hundredths
var elapsedFractions = Math.floor (remaining / 10);
if (elapsedFractions < 10) {
elapsedFractions = "0" + elapsedFractions;
}
//display result nicely
outputData = elapsedM + ":" + elapsedS + ":" + elapsedFractions;
return outputData;
}
/********************************
functions displayStreaming() and displayPosition()
called by setIntervals in the doPlay() function, these two functions are
responsible for updating and displaying time data: one for the song's
duration,the other for the song's position
*********************************/
function displayStreaming (theSource) {
if (theSource.duration > 0) {
if (theSource.getBytesLoaded () == theSource.getBytesTotal ()) {
clearInterval (streamingID);
mainTL.displayDuration_txt.text = " song duration: " + formatTime (theSource.duration);
} else {
mainTL.displayDuration_txt.text = "streaming: " + formatTime (theSource.duration);
}
}
}
function displayPosition (theSource) {
if (theSource.position > 0) {
if (theSource.position == theSource.duration) {
clearInterval (playingID);
mainTL.displayPosition_txt.text = "completed: " + formatTime (theSource.position);
} else {
mainTL.displayPosition_txt.text = "current position: " + formatTime (theSource.position);
}
}
}
/**********************************
the core function: doPlay()
doPlay() clears old Sound object, creates a new one and starts it playing
also clears it when it finishes playing
*********************************/
function doPlay (theSong) {
//we always want to start with a fresh Sound object,
//so intialize the environment:
clearInterval (streamingID);
clearInterval (playingID);
if (typeof myTunes == "object") {
myTunes.stop ();
delete myTunes;
}
displayDuration_txt.text = "--";
displayPosition_txt.text = "--";
//reset pause/play button to default in case user had
//paused sound and then selected another track
pausePlay_pb.setClickHandler ("pauseSound");
pausePlay_pb.setLabel ("ll");
//grab data parameter from ListBox
var theSong = songList_lb.getSelectedItem ().data;
myTunes = new Sound ();
myTunes.loadSound (theSong, true);
myTunes.setVolume (Math.floor(volume_mc.slider_mc._x/2));
//the setIntervals that display time data
streamingID = setInterval (displayStreaming, 200, myTunes);
playingID = setInterval (displayPosition, 200, myTunes);
//what to do when tune ends
//clean up stuff from previous song
myTunes.onSoundComplete = function () {
nextTrack ();
};
}
//pull out the meat of the xml file, pass it to a
//parsing function and set the listBox's dataProvider
//Thanks to Sam Wan and Branden Hall for these techniques
function songListLoaded() {
// get the first child: "songs"
var mainNode = this.songList_xml.firstChild;
// listBoxData is a reference to the DataProvider object
// created by the function which follows this one
//feed createResourceList the child nodes ("song")
var listBoxData = createResourceList (mainNode.childNodes);
// tell the ListBox to use this object as its DataProvider
this.songList_lb.setDataProvider (listBoxData);
songList_lb.setSelectedIndex(0);
};
/********************************
stuff extracted data into generic objects and return those objects
formatted to make sense to the listBox component.
********************************/
function createResourceList(resource_array) {
var listData = new DataProviderClass ();
var resourceCount = resource_array.length;
var resource, display, url;
for (var i = 0; i < resourceCount; i++) {
resource = resource_array*;
//add a consecutive number in front of each title
display = i+1+". "+resource.attributes.display;
url = resource.attributes.url;
listData.addItem ({label:display, data:url});
}
return listData;
};
//nextTrack() is called by both the "next" button AND by the
//doPlay() function if mainTL.continuous is true
function nextTrack() {
if (songList_lb.getSelectedIndex () == songList_lb.getLength () - 1) {
songList_lb.setSelectedIndex (0);
} else {
songList_lb.setSelectedIndex (songList_lb.getSelectedIndex () + 1);
}
};
//the prevTrack() function is called by "prev" button
function prevTrack() {
if (songList_lb.getSelectedIndex () == 0) {
songList_lb.setSelectedIndex (songList_lb.getLength () - 1);
} else {
songList_lb.setSelectedIndex (songList_lb.getSelectedIndex () - 1);
}
};
//function playMode() - called by checkbox's change handler
//it modifies the global variable "continuous"
//if continuous is true, doPlay calls the nextTrack() function
function playMode() {
mainTL.continuous = continuous_ch.getValue();
}
On my “Logic” layer: (these names were the original from the download):
//persistent reference to _root
_global.mainTL = this;
//prevent resizing
Stage.scaleMode = "noScale";
/**********************
This portion is concerned with the main navigation,which is the ListBox component
containing the titles. These are brought in from an external xml file and
displayed in a ListBox. Change these values for ones that suit your site's colors
************************/
//first, set styling for the ListBox
lbStyle = new FStyleFormat ();
lbStyle.textAlign = "left";
lbStyle.textFont = "Verdana";
lbStyle.textSize = 10;
lbStyle.textLeftMargin = 5;
lbStyle.textColor = 0xffffff;
lbStyle.textSelected = 0xF4DB84;
lbStyle.embedFonts = false;
lbStyle.background = 0x333333;
lbStyle.arrow = 0xF4DB84;
lbStyle.scrollTrack = 0x333333;
lbStyle.face = 0x444444;
lbStyle.addListener (songList_lb);
//add styling to buttons
var buttonStyle = new FStyleFormat();
buttonStyle.textFont = "Verdana";
buttonStyle.textSize = 10;
buttonStyle.textColor = 0xF4DB84;
buttonStyle.textAlign = center;
buttonStyle.radioDot = 0x333333;
buttonStyle.addlistener(pausePlay_pb, next_pb, prev_pb, continuous_ch);
//second, set basic xml stuff
songList_xml = new XML ();
songList_xml.ignoreWhite = true;
songList_xml.onLoad = function (success) {
if (success) {
songListLoaded ();
}
};
//tell it how many rows to display
songList_lb.setRowCount (5);
//Define change handler for listBox component
songList_lb.setChangeHandler ("doPlay");
//Finally! Load the xml document.
songList_xml.load ("songList.xml");
And on the “buttons” layer:
/**************************
code for volume slider
***************************/
volume_mc.slider_mc.onPress = function () {
this.startDrag (false, 0, this._y, 200, this._y);
this.onEnterFrame = function () {
//volume slider track is 200px long;
//we want volume between 0 and 100, so divide by 2
myTunes.setVolume (Math.floor(this._x/2));
};
};
volume_mc.slider_mc.onRelease = function () {
this.stopDrag ();
delete this.onEnterFrame;
};
/*********************************************
code for pause/resume button
credit to Joey Lott ("ActionScript Cookbook") for the
code related to pausing and resuming sounds
*********************************************/
//start by extending sound class with two new methods
//pause() and resume()
Sound.prototype.pause = function () {
//get the current position and then stop the sound
this.pauseTime = this.position;
this.stop ();
};
Sound.prototype.resume = function () {
//start the sound at the point at which it was previously stopped
this.start (this.pauseTime / 1000);
};
//define two callback functions. One pauses the sound
//the other resumes the sound. Each one toggles the
//push button click handler to the other function
function resumeSound () {
myTunes.resume ();
pausePlay_pb.setClickHandler ("pauseSound");
pausePlay_pb.setLabel ("ll");
}
function pauseSound () {
myTunes.pause ();
pausePlay_pb.setClickHandler ("resumeSound");
pausePlay_pb.setLabel (">");
}
//define initial click handler and label for button
pausePlay_pb.setClickHandler ("pauseSound");
pausePlay_pb.setLabel ("ll");
pausePlay_pb.setSize(55, 12);
//code for next and previous
prev_pb.setClickHandler ("prevTrack");
prev_pb.setLabel ("«");
prev_pb.setSize(70, 12);
next_pb.setClickHandler ("nextTrack");
next_pb.setLabel ("»");
next_pb.setSize(70, 12);
I know that’s a lot of code but if anyone would like to take a look I would REALLY appreciate it. I tried contacting the author of the original code, but it isn’t working.
Thanks for any help!!
-SD