PDA

Volledige versie bekijken : random inladen van xml?


jbulckens
%Europe/Berlin %541 %2007, 12:59
Hoi

Ik heb een mp3-speler gevonden met de code die je hieronder ziet. Wat hij doet is xml inladen waarin de linken naar de mp3-files staan. Ik heb wat zitten uittesten maar vind het niet om het random in te laden. En op de knop 'nextbutton' ook een random te plaatsen. Ik veromed dat het maar iets heel simpel is maar vind het dus niet..

Kan iemand me helpen? (het is veel code maar wel snel duidelijk)

import Song;
import mx.utils.Delegate;

class vPlayer {


// --- mods ----------------------------


// --- variables -----------------------
public var Settings:Object;
public var XMLSource:String;
public var Songs:Array;
public var root:MovieClip;
public var index:Number;
public var playStatus:String;
public var sound:Object;

private var curDuration:Number;
private var pauseOffset:Number;

private var playButton:MovieClip;
private var previousButton:MovieClip;
private var nextButton:MovieClip;
private var pauseButton:MovieClip;
private var stopButton:MovieClip;
private var display:TextField;
private var barWidth:Number;
private var barFrame:MovieClip
private var totalBar:MovieClip;
private var positionBar:MovieClip;
private var totalAUX:MovieClip;
private var positionAUX:MovieClip;
private var XMLRaw:XML;

// --- vPlayer constructor ---------------------
function vPlayer(Settings:Object){

this.Settings = Settings;
if(validateSettings()) init();
}

// --- private methods -------------------------
private function init():Void {

distributeSettings();
setControls();
loadPlaylist();
}

private function loadMods():Void {


}

private function validateSettings():Boolean {

if(Settings == undefined) abort("Settings not found.");
if(!Settings instanceof Object) abort("Settings is not an instance of Object.");
if(Settings.Interface == undefined) abort("Interface not found.");
if(!Settings.Interface instanceof MovieClip) abort("Interface is not an instance of MovieClip.");
if(Settings.Interface._play == undefined) abort("Control element _play not found.");
if(Settings.Interface._previous == undefined) abort("Control element _previous not found.");
if(Settings.Interface._stop == undefined) abort("Control element _stop not found.");
if(Settings.Interface._next == undefined) abort("Control element _next not found.");
if(Settings.Interface._pause == undefined) abort("Control element _pause not found.");
if(Settings.Playlist == undefined) abort("Playlist not found.");
if(typeof Settings.Playlist != "string") abort("Playlist ("+typeof Settings.Playlist+") is not a string type.");
if(Settings.Interface._display == undefined) abort("Interface element _display not found.");
if(Settings.Interface._display._text == undefined) abort("Interface element _display._text not found.");
if(!Settings.Interface._display._text instanceof TextField) abort("Interface element _display is not an instance of TextField.");
if(Settings.Interface._barframe == undefined) abort("Interface element _barframe not found.");
if(!Settings.Interface._barframe instanceof MovieClip) abort("Interface element _barframe is not an instance of MovieClip.");
if(Settings.Interface._totalbar == undefined) abort("Interface element _totalbar not found.");
if(!Settings.Interface._totalbar instanceof MovieClip) abort("Interface element _totalbar is not an instance of MovieClip.");
if(Settings.Interface._positionbar == undefined) abort("Interface element _positionbar not found.");
if(!Settings.Interface._positionbar instanceof MovieClip) abort("Interface element _positionbar is not an instance of MovieClip.");
if(Settings.Interface._totalaux == undefined) abort("Interface element _totalaux not found.");
if(!Settings.Interface._totalaux instanceof MovieClip) abort("Interface element _totalaux is not an instance of MovieClip.");
if(Settings.Interface._positionaux == undefined) abort("Interface element _positionaux not found.");
if(!Settings.Interface._positionaux instanceof MovieClip) abort("Interface element _positionaux is not an instance of MovieClip.");

return true;

}

private function loadSong():Void {

playStatus = "playing";
sound = new Object(new Sound(root));
sound.loadSound(Songs[index].url,true);

totalAUX.onEnterFrame = Delegate.create(this,showTotal);
positionAUX.onEnterFrame = Delegate.create(this,showPosition);
}

private function boot():Void {

curDuration = Songs[index].duration;
showInfo();
loadSong();
}

private function loadPlaylist():Void {

XMLRaw = new XML();
XMLRaw.ignoreWhite = true;
XMLRaw.onLoad = Delegate.create(this,XMLParse);
XMLRaw.load(XMLSource);
}

private function XMLParse(ok:Boolean):Void {

if(ok){

Songs = new Array();
index = 0;
var amount = XMLRaw.firstChild.childNodes.length;

for(var i=0;i<amount;i++){

var curNode = XMLRaw.firstChild.childNodes[i];

var _title = curNode.attributes.title;
var artist = curNode.attributes.artist;
var duration = curNode.attributes.duration;
var url = curNode.firstChild.nodeValue;

if(_title == undefined) abort("Missing title for song "+(i+1)+".");
if(artist == undefined) abort("Missing artist for song "+(i+1)+".");
if(duration == undefined) abort("Missing URL for song "+(i+1)+".");
if(duration == undefined) abort("Missing duration for song "+(i+1)+".");

Songs.push(new Song(_title,artist,duration,url));
}

loadMods();
boot();

} else {

abort("XML Source "+XMLSource+" could not be loaded.");

}
}

private function setControls():Void {

nextButton.onRelease = Delegate.create(this,nextSong);
previousButton.onRelease = Delegate.create(this,previousSong);
stopButton.onRelease = Delegate.create(this,stopSong);
pauseButton.onRelease = Delegate.create(this,pauseSong);
playButton.onRelease = Delegate.create(this,playSong);
}

private function distributeSettings():Void {

// controls
root = Settings.Interface;
playButton = Settings.Interface._play;
previousButton = Settings.Interface._previous;
nextButton = Settings.Interface._next;
pauseButton = Settings.Interface._pause;
stopButton = Settings.Interface._stop;

// bars
barWidth = Settings.Interface._barframe._width;
barFrame = Settings.Interface._barframe;
totalBar = Settings.Interface._totalbar;
positionBar = Settings.Interface._positionbar;

// display
display = Settings.Interface._display._text;

// aux
totalAUX = Settings.Interface._totalaux;
positionAUX = Settings.Interface._positionaux;

// playlist
XMLSource = Settings.Playlist;

}

// --- public methods --------------------------
public function showInfo():Void {

display.htmlText = Songs[index].getInfo();
}

public function showTotal():Void {

totalBar._width = Math.min((sound.duration*barWidth)/curDuration,barWidth);
if(sound.duration > curDuration-900 && sound.duration < curDuration+900){
delete totalAUX.onEnterFrame;
}
}

public function showPosition():Void {

positionBar._width = Math.min((barWidth*sound.position)/curDuration,barWidth);
if(sound.position > curDuration-900 && sound.position < curDuration+900){
delete positionAUX.onEnterFrame;
nextSong();
}
}

public function previousSong():Void {

if(--index < 0) index = Songs.length-1;
stopSong();
boot();
}

public function nextSong():Void {

if(++index >= Songs.length) index = 0;
stopSong();
boot();
}

public function stopSong():Void {

if(playStatus != "stopped"){
sound.stop();
playStatus = "stopped";
delete positionAUX.onEnterFrame;
delete totalAUX.onEnterFrame;
positionBar._width = 0.5;
totalBar._width = 0.5;
}
}

public function pauseSong():Void {

if(playStatus == "playing"){
sound.stop();
playStatus = "paused";
pauseOffset = Math.floor(sound.position/1000)*1000;
delete positionAUX.onEnterFrame;
}
}

public function playSong():Void {

if(playStatus == "paused"){
sound.start(pauseOffset/1000,1);
totalAUX.onEnterFrame = Delegate.create(this,showTotal);
positionAUX.onEnterFrame = Delegate.create(this,showPosition);
playStatus = "playing";
} else if(playStatus == "stopped"){
boot();
}
}

public function changeSong(to:Number):Boolean {

if(to >= Songs.length) return false;
index = to;
stopSong();
boot();
return true;
}

public function abort(wrong:String):Void {

throw new Error("Fatal error:\n"+wrong+"\nExecution terminated.");
}

}

met vriendelijek groeten
joris

Emveedee
%Europe/Berlin %542 %2007, 13:01
Kijk eens in de flash Help bij Actionscript 2.0 Language Reference -> Classes -> Math -> random().
Je kan zo een willekeurig getal laten kiezen en vervolgens dat nummer afspelen.

jbulckens
%Europe/Berlin %557 %2007, 13:22
moet ik dat dan in bovenstaande code doen?

of als xml wordt ingeladen in flash (dat is met deze code)

Settings = new Object();
Settings.Interface = this;
Settings.Playlist = "vplayer.xml";



of hier (komt uit de code)?

Songs = new Array();
index = 0;
var amount = XMLRaw.firstChild.childNodes.length;

for(var i=0;i<amount;i++){

Emveedee
%Europe/Berlin %588 %2007, 14:08
Ik zou het in de functie doen waar je het volgende nummer aanroept :)


Immers:
het is veel code maar wel snel duidelijk

jbulckens
%Europe/Berlin %593 %2007, 14:14
aiaia; dat had ik nooit mogen zeggen ;)
ik bedoelde : heel overzichtelijk