im tweeking a mp3 player that loads mp3 from a directory and streams them.
the swf
it seems to be working fine, but i want it to play another song randomly after the previous one is done, and continue that indefinitely. i can do this with a button.. the forward button randomly loads another song and plays it... but i want to do it automatically. i know the solution is simple i am just not seeing it. not concerned with the file browsing code as i will be removing it.
thanks
Joe
ps- i use flash everyday but dont consider myself a pro at all... still very much the newb and learning everyday thanks.
THE CODE:
//
stop();
// Declare user defined variable to be called from the javascript function
user_path = "";
// Set Style Format for listbox and scrollbar
with (globalStyleFormat) {
face = 0x8098B0;
highlight = 0xC0CCD8;
highlight3D = 0xC0CCD8;
darkshadow = 0x000000;
shadow = 0x4E657C;
arrow = 0x333333;
foregroundDisabled = 0xC0CCD8;
scrollTrack = 0xC0CCD8;
focusRectInner = 0xDBDCE5;
focusRectOuter = 0x9495A2;
background = 0xEAEEF2;
globalStyleFormat.border = 0xA35281;
globalStyleFormat.applyChanges();
}
// ****************Begin player code*****************
// Set target path to MP3's.... can be an http internet path or a local path
targetPath = "// Play a random song to start
_root.onLoad = function() {
randomPlay();
};
// function to handle the variable sent to flash from the windows file browser
localPath = function () { this.onEnterFrame = function() {if (_root.user_path != "") {_root.custom.userFile.text = _root.user_path;}};};
localPath();
// random play function
randomPlay = function () { list = _root.lBox.labels.length;_root.lBox.setSelectedIndex(random(list));myRandomSong = _root.lBox.getSelectedItem().label;myRandomArtist = _root.lBox.getSelectedItem().data;newSound(targetPath+myRandomSong+".mp3");_root.status.text = "PLAYING";_root.artist.text = myRandomArtist;_root.song.text = myRandomSong;};
// Set visibility of pop up clips
_root.visualHolder._visible = false;
_root.custom._visible = false;
// Visual pop up function controls visuals option
_root.showVisuals.onRelease = function() {
with (_root.visualHolder) {
if (_visible == true) {
_visible = false;
visuals.gotoAndStop("off");
} else {
_visible = true;
// turn on the first visual
visuals.gotoAndStop("visual1");
visual1_b.onRelease = function() {
visuals.gotoAndStop("visual1");
};
visual2_b.onRelease = function() {
visuals.gotoAndStop("visual2");
};
visual3_b.onRelease = function() {
visuals.gotoAndStop("visual3");
};
}
}
};
// Stop button function
stop_b.onRelease = function() {
currentSound.stop();
_root.equ.gotoAndStop("stop");
_root.status.text = "STOPPED";
_root.percentLoaded.text = "";
_root.percentPlayed.text = "";
_root.artist.text = "";
_root.song.text = "";
_root.preloader.removeMovieClip();
};
// Play button function
play_b.onRelease = function() {
_root.custom._visible = false;
if (_root.selectedSong.text != "User Defined") {
newSound(targetPath+chosenSong+".mp3");
_root.artist.text = _root.lBox.getSelectedItem().data;
_root.song.text = _root.lBox.getSelectedItem().label;
}
if (_root.selectedSong.text == "User Defined") {
_root.custom._visible = true;
_root.lBox._visible = false;
_root.custom.customClose.onRelease = function() {
_root.custom._visible = false;
_root.lBox._visible = true;
};
}
// start of pathchecking to make sure the file is .mp3
if (_root.custom.userPath.text.indexOf(".") == false) {
_root.custom._visible = true;
_root.custom.userPath.text = "Please enter a correct file path.";
}
};
// user selected browse function uses javascript with the fscommand
// to open the windows file browser.(Only works with Internet Explorer)
_root.custom.userLoad.onRelease = function() {
fscommand("init");
// User selected play button function
_root.custom.userPlay.onRelease = function() {
if (_root.custom.userFile.text != "") {
newSound(_root.custom.userFile.text);
_root.user_path = "";
_root.custom.userFile.text = "";
_root.status.text = "PLAYING";
_root.artist.text = "User Defined";
_root.song.text = "";
}
};
};
// };
// Preload work function and sound manipulation
function soundLoadControl() {
if (!this.fullyLoaded) {
// while the sound is streaming do the preloader stuff
var t = currentSound.getBytesTotal();
var l = currentSound.getBytesLoaded();
var pc = l/t;
this.load_mc._width = Math.floor(this._width*pc);
if (t && l == t) {
this.fullyLoaded = true;
}
}
var d = currentSound.duration;
var p = currentSound.position;
var ratio = p/d;
// set the playhead position during play
this.pos_mc._x = Math.floor(this._width*ratio);
// Attempt to allow drag of playhead to set currentsound position
// oldX = this.pos_mc._x;
// np = currentSound.position;
// this.pos_mc.onPress = function() {
// startDrag(this);
// currentSound.stop();
// };
// this.pos_mc.onRelease = function() {
// stopDrag();
// newPosX = this._x;
// this.pos_mc._x = newPosX;
// currentSound.position += Math.floor((newPosX - oldX));
// currentSound.start();
// }
//
// show selection in preview textfield
_root.selectedSong.text = _root.lBox.getSelectedItem().label;
// Percent textfields
_root.percentLoaded.text = parseInt((p/d)*100)+"%"+"Loaded";
if (isNaN(d/p)) {
_root.percentPlayed.text = "0%";
_root.percentLoaded.text = "";
} else {
_root.percentPlayed.text = parseInt((p/d)*100)+"%";
_root.percentLoaded.text = parseInt(l/t*100)+"%";
if (_root.percentLoaded.text>="99") {
_root.percentLoaded.text = "";
}
}
}
// Sound function
function newSound(theURL, song, artist) {
// load the sound
CurrentSound.stop();
delete currentSound;
currentSound = new Sound();
currentSound.loadSound(theURL, true);
currentSound.start();
currentSound.onSoundComplete = function() {
_root.equ.gotoAndStop("stop");
_root.status.text = "STOPPED";
_root.artist.text = "";
_root.song.text = "";
_root.percentLoaded.text = "";
_root.percentPlayed.text = "";
preloader.removeMovieClip();
};
// Attach the preloader
var temp = this.attachMovie("preloader", "preloader", 10);
_root.status.text = "PLAYING";
// Preload position needs to correspond with
// author time position of the player
temp._x = 216;
temp._y = 129;
temp.theURL = theURL;
temp.fullyLoaded = false;
temp.onEnterFrame = soundLoadControl;
}
the swf
it seems to be working fine, but i want it to play another song randomly after the previous one is done, and continue that indefinitely. i can do this with a button.. the forward button randomly loads another song and plays it... but i want to do it automatically. i know the solution is simple i am just not seeing it. not concerned with the file browsing code as i will be removing it.
thanks
Joe
ps- i use flash everyday but dont consider myself a pro at all... still very much the newb and learning everyday thanks.
THE CODE:
//
stop();
// Declare user defined variable to be called from the javascript function
user_path = "";
// Set Style Format for listbox and scrollbar
with (globalStyleFormat) {
face = 0x8098B0;
highlight = 0xC0CCD8;
highlight3D = 0xC0CCD8;
darkshadow = 0x000000;
shadow = 0x4E657C;
arrow = 0x333333;
foregroundDisabled = 0xC0CCD8;
scrollTrack = 0xC0CCD8;
focusRectInner = 0xDBDCE5;
focusRectOuter = 0x9495A2;
background = 0xEAEEF2;
globalStyleFormat.border = 0xA35281;
globalStyleFormat.applyChanges();
}
// ****************Begin player code*****************
// Set target path to MP3's.... can be an http internet path or a local path
targetPath = "// Play a random song to start
_root.onLoad = function() {
randomPlay();
};
// function to handle the variable sent to flash from the windows file browser
localPath = function () { this.onEnterFrame = function() {if (_root.user_path != "") {_root.custom.userFile.text = _root.user_path;}};};
localPath();
// random play function
randomPlay = function () { list = _root.lBox.labels.length;_root.lBox.setSelectedIndex(random(list));myRandomSong = _root.lBox.getSelectedItem().label;myRandomArtist = _root.lBox.getSelectedItem().data;newSound(targetPath+myRandomSong+".mp3");_root.status.text = "PLAYING";_root.artist.text = myRandomArtist;_root.song.text = myRandomSong;};
// Set visibility of pop up clips
_root.visualHolder._visible = false;
_root.custom._visible = false;
// Visual pop up function controls visuals option
_root.showVisuals.onRelease = function() {
with (_root.visualHolder) {
if (_visible == true) {
_visible = false;
visuals.gotoAndStop("off");
} else {
_visible = true;
// turn on the first visual
visuals.gotoAndStop("visual1");
visual1_b.onRelease = function() {
visuals.gotoAndStop("visual1");
};
visual2_b.onRelease = function() {
visuals.gotoAndStop("visual2");
};
visual3_b.onRelease = function() {
visuals.gotoAndStop("visual3");
};
}
}
};
// Stop button function
stop_b.onRelease = function() {
currentSound.stop();
_root.equ.gotoAndStop("stop");
_root.status.text = "STOPPED";
_root.percentLoaded.text = "";
_root.percentPlayed.text = "";
_root.artist.text = "";
_root.song.text = "";
_root.preloader.removeMovieClip();
};
// Play button function
play_b.onRelease = function() {
_root.custom._visible = false;
if (_root.selectedSong.text != "User Defined") {
newSound(targetPath+chosenSong+".mp3");
_root.artist.text = _root.lBox.getSelectedItem().data;
_root.song.text = _root.lBox.getSelectedItem().label;
}
if (_root.selectedSong.text == "User Defined") {
_root.custom._visible = true;
_root.lBox._visible = false;
_root.custom.customClose.onRelease = function() {
_root.custom._visible = false;
_root.lBox._visible = true;
};
}
// start of pathchecking to make sure the file is .mp3
if (_root.custom.userPath.text.indexOf(".") == false) {
_root.custom._visible = true;
_root.custom.userPath.text = "Please enter a correct file path.";
}
};
// user selected browse function uses javascript with the fscommand
// to open the windows file browser.(Only works with Internet Explorer)
_root.custom.userLoad.onRelease = function() {
fscommand("init");
// User selected play button function
_root.custom.userPlay.onRelease = function() {
if (_root.custom.userFile.text != "") {
newSound(_root.custom.userFile.text);
_root.user_path = "";
_root.custom.userFile.text = "";
_root.status.text = "PLAYING";
_root.artist.text = "User Defined";
_root.song.text = "";
}
};
};
// };
// Preload work function and sound manipulation
function soundLoadControl() {
if (!this.fullyLoaded) {
// while the sound is streaming do the preloader stuff
var t = currentSound.getBytesTotal();
var l = currentSound.getBytesLoaded();
var pc = l/t;
this.load_mc._width = Math.floor(this._width*pc);
if (t && l == t) {
this.fullyLoaded = true;
}
}
var d = currentSound.duration;
var p = currentSound.position;
var ratio = p/d;
// set the playhead position during play
this.pos_mc._x = Math.floor(this._width*ratio);
// Attempt to allow drag of playhead to set currentsound position
// oldX = this.pos_mc._x;
// np = currentSound.position;
// this.pos_mc.onPress = function() {
// startDrag(this);
// currentSound.stop();
// };
// this.pos_mc.onRelease = function() {
// stopDrag();
// newPosX = this._x;
// this.pos_mc._x = newPosX;
// currentSound.position += Math.floor((newPosX - oldX));
// currentSound.start();
// }
//
// show selection in preview textfield
_root.selectedSong.text = _root.lBox.getSelectedItem().label;
// Percent textfields
_root.percentLoaded.text = parseInt((p/d)*100)+"%"+"Loaded";
if (isNaN(d/p)) {
_root.percentPlayed.text = "0%";
_root.percentLoaded.text = "";
} else {
_root.percentPlayed.text = parseInt((p/d)*100)+"%";
_root.percentLoaded.text = parseInt(l/t*100)+"%";
if (_root.percentLoaded.text>="99") {
_root.percentLoaded.text = "";
}
}
}
// Sound function
function newSound(theURL, song, artist) {
// load the sound
CurrentSound.stop();
delete currentSound;
currentSound = new Sound();
currentSound.loadSound(theURL, true);
currentSound.start();
currentSound.onSoundComplete = function() {
_root.equ.gotoAndStop("stop");
_root.status.text = "STOPPED";
_root.artist.text = "";
_root.song.text = "";
_root.percentLoaded.text = "";
_root.percentPlayed.text = "";
preloader.removeMovieClip();
};
// Attach the preloader
var temp = this.attachMovie("preloader", "preloader", 10);
_root.status.text = "PLAYING";
// Preload position needs to correspond with
// author time position of the player
temp._x = 216;
temp._y = 129;
temp.theURL = theURL;
temp.fullyLoaded = false;
temp.onEnterFrame = soundLoadControl;
}