// Assign var that has to do with how many tracks we want to play from our folder
var tracksInFolder:Number = 6;
// Assign var for default start track number, usually start playing track 1
var trackPlayNum:Number = 1;
// For maintaining volume after setting it using player controls
var volnum:Number = 1;
stop();
// Create the sound object
var snd:Sound = new Sound();
// Assign a var name for the sound channel
var channel:SoundChannel;
// Initialize the pause position
var pausePosition:int = 0;
// Boolean value for button functions, to switch in the conditionals
var isPlaying:Boolean = false;
// Set the play buffer to 5 seconds, you can adjust this
var context:SoundLoaderContext = new SoundLoaderContext(5000, true);
// Load the requested URL sound into the snd var, along with context
snd.load(req, context);
// Create the play channel using snd
channel = snd.play(); // Start playing
// Set "isPalying" to true initially
isPlaying = true;
// Listen for the onPlaybackComplete function directly below
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
// This onPlaybackComplete fires off when tracks finish playing
function onPlaybackComplete(event:Event) {
if (trackPlayNum < tracksInFolder) {
channel.stop();
trackPlayNum += 1;
gotoAndPlay(2);
} else {
channel.stop();
trackPlayNum = 1;
gotoAndPlay(2);
}
}
////////////////////////////////////////////////////////////////////////////////////
// Play Function //////////////////////////////////////////////////////
function playSound(event:MouseEvent):void {
if (isPlaying == false) {
channel = snd.play(pausePosition);
isPlaying = true;
}
}
////////////////////////////////////////////////////////////////////////////////////
/// Get ID3 info for each track playing ////////////////////
snd.addEventListener(Event.ID3, onID3InfoReceived);
function onID3InfoReceived (event:Event):void {
var songName:String;
var artist:String;
if (snd.id3.songName != null){
songName = snd.id3.songName;
} else {
songName = "Title Unknown";
}
if (snd.id3.artist != null){
artist = snd.id3.artist;
} else {
artist = "Unknown Artist";
}
// Now here we add the info to the songInfo text on stage
songInfo_txt.text = songName + " - " + artist;
}
////// END ID3 Info Gathering
// Stop Function //////////////////////////////////////////////////////
function stopSound(event:MouseEvent):void {
channel.stop();
pausePosition = 0;
isPlaying = false;
}
// Pause Function //////////////////////////////////////////////////////
function pauseSound(event:MouseEvent):void {
pausePosition = channel.position;
channel.stop();
isPlaying = false;
}
////////////////////////////////////////////////////////////////////////////////////
// Play Next Song Function ////////////////////////////////////
function nextSound(event:MouseEvent):void {
if (trackPlayNum < tracksInFolder) {
if (snd.bytesLoaded != snd.bytesTotal) {
channel.stop();
snd.close();
trackPlayNum += 1;
gotoAndPlay(2);
} else {
channel.stop();
trackPlayNum += 1;
gotoAndPlay(2);
}
} else {
if (snd.bytesLoaded != snd.bytesTotal) {
channel.stop();
snd.close();
trackPlayNum = 1;
gotoAndPlay(2);
} else {
channel.stop();
trackPlayNum = 1;
gotoAndPlay(2);
}
}
}
////////////////////////////////////////////////////////////////////////////////////
// Play Previous Song Function //////////////////////////////
function previousSound(event:MouseEvent):void {
if (trackPlayNum == 1) {
if (snd.bytesLoaded != snd.bytesTotal) {
channel.stop();
snd.close();
trackPlayNum += 1;
gotoAndPlay(2);
} else {
channel.stop();
trackPlayNum = tracksInFolder;
gotoAndPlay(2);
}
} else {
if (snd.bytesLoaded != snd.bytesTotal) {
channel.stop();
snd.close();
trackPlayNum -= 1;
gotoAndPlay(2);
} else {
channel.stop();
trackPlayNum -= 1;
gotoAndPlay(2);
}
}
}
////////////////////////////////////////////////////////////////////////////////////
// Button Listeners ///////////////////////////////////////////////////////////////////////
playBtn.addEventListener(MouseEvent.CLICK, playSound);
stopBtn.addEventListener(MouseEvent.CLICK, stopSound);
pauseBtn.addEventListener(MouseEvent.CLICK, pauseSound);
previousBtn.addEventListener(MouseEvent.CLICK, previousSound);
nextBtn.addEventListener(MouseEvent.CLICK, nextSound);
volDownBtn.addEventListener(MouseEvent.CLICK, volumeDown);
volUpBtn.addEventListener(MouseEvent.CLICK, volumeUp);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// VOLUME Up and Down CODE //////////////////////////////////////////////////////////////////////////////////////////////
var volumeLevel = channel.soundTransform;
var getVolume:Number = volnum;
volumeLevel.volume = getVolume;
channel.soundTransform = volumeLevel;
volinfo.text = "" + Math.round(volumeLevel.volume * 100) + "%";
function volumeDown(event:MouseEvent):void {
if (volumeLevel.volume > .1) {
volumeLevel.volume = volumeLevel.volume - .1;
channel.soundTransform = volumeLevel;
volinfo.text = "" + Math.round(volumeLevel.volume * 100) + "%";
volnum = volumeLevel.volume;
}
}
function volumeUp(event:MouseEvent):void {
if (volumeLevel.volume < 1.0) {
volumeLevel.volume = volumeLevel.volume + .1;
channel.soundTransform = volumeLevel;
volinfo.text = "" + Math.round(volumeLevel.volume * 100) + "%";
volnum = volumeLevel.volume;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Add listener to trigger [onEnterFrame] function below
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
// ANIMATED EQ BARS MASK CODE
maskEQ1.width = (Math.round(channel.leftPeak * 27) );
maskEQ2.width = (Math.round(channel.rightPeak * 27) );
bars1.mask = maskEQ1;
bars2.mask = maskEQ2;
// Get full time
var tallytime = (snd.length/1000);
var totalmins:Number = Math.floor(tallytime /60);
var totalsecs = Math.floor (tallytime) % 60;
if (totalsecs < 10){
totalsecs = "0" + totalsecs;
}
displayFullTime.text = ( " " + totalmins+ ":" + totalsecs);
// End get Full time
// Get playing time
var totalSeconds:Number = channel.position/1000;
var minutes:Number = Math.floor(totalSeconds /60);
var seconds = Math.floor (totalSeconds) % 60;
if (seconds < 10){
seconds = "0" + seconds;
}
displayTime.text = ( " " + minutes+ ":" + seconds);
// End get playing time
/// progress bar code...
var estimatedLength:int = Math.ceil(snd.length / (snd.bytesLoaded / snd.bytesTotal));
var playbackPercent:uint = 100 * (channel.position / estimatedLength );
// I want my position bar to be 200 pixels wide on completion so I multiply the percentage x 2
positionBar.width = playbackPercent * 2;
}
// Place track amount and current track # in to a text field
songCount_txt.text = "Track " + trackPlayNum + " of " + tracksInFolder;