# Random mp3 play

**URL:** https://forum.kirupa.com/t/random-mp3-play/230492
**Category:** Uncategorized
**Created:** [June 26, 2007, 6:15pm UTC](https://forum.kirupa.com/t/random-mp3-play/230492 "2007-06-26T18:15:47Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![karrakela](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@karrakela](https://forum.kirupa.com/u/karrakela)
#### Post date: [June 26, 2007, 6:15pm UTC](https://forum.kirupa.com/t/random-mp3-play/230492/1 "2007-06-26T18:15:47Z")

</div>

i have this code for an mp3 player and i would like to have it play the songs randomly…  
Is there an easy way to make the code so it picks a random song everytime??

[COLOR=Blue]function getData (url:String, exitFunction:Function):Void {

```
var gXML:XML = new XML();
gXML.ignoreWhite = true;
gXML.onLoad = exitFunction;
gXML.load(url);

```

}  
/////////////////////////////////////////////////////////////

function loadPlaylist (loaded:Boolean) {  
if (loaded) {

```
    var mainStream_xml:XMLNode = this.childNodes[0];
    var stream_array:Array = [];
    
    for (var i:Number = 0; i &lt; (mainStream_xml.childNodes.length); i ++) {
        stream_array* = new Array();
        stream_array*["file"] = mainStream_xml.childNodes*.attributes.file;
        stream_array*["name"] = mainStream_xml.childNodes*.attributes.name;
        
    } // end for looping through childNodes
    _root.playlistArray = stream_array;
    
    gotoNextSong();
    
}

```

}

function gotoNextSong () {  
if (\_root.playlistArray.length \> 0) {  
if (\_root.currentSong == 0) {  
\_root.bgSound\_snd.loadSound(\_root.playlistArray[\_root.currentSong][“file”], true);  
trace (\_root.playlistArray[\_root.currentSong][“name”]);  
\_root.songName\_txt.text = \_root.playlistArray[\_root.currentSong][“name”];  
\_root.currentSong++;  
} else {  
trace (\_root.playlistArray[\_root.currentSong][“name”]);  
\_root.songName\_txt.text = \_root.playlistArray[\_root.currentSong][“name”];  
\_root.bgSound\_snd.loadSound(\_root.playlistArray[\_root.currentSong][“file”], true);  
\_root.currentSong++;  
}  
}  
}

var playlistArray = new Array();  
var currentSong = new Number(0);  
// load and set the loop for the sound  
var bgSound\_snd:Sound = new Sound();

bgSound\_snd.setVolume(50);

bgSound\_snd.onSoundComplete = function() {  
gotoNextSong();  
}

getData(“playlist.xml”, loadPlaylist);  
[/COLOR]  
I should say I got the code from these forums, i am just getting into actionscript and tried to modify it myself but is not working  
any help would be much appreciated…

Thanks kirupis!! :love:
