I have built an MP3 player using XML and this works fine. But I want this MP3 player to load a random song from the XML list everytime it runs. I have this code
var my_songs:XMLList;
var my_total:Number;
var my_sound:Sound;
var my_channel:SoundChannel;
var current_song:Number = 0;
var song_position:Number;
var song_paused:Boolean;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("playlist.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(evt:Event):void
{
var myXML:XML = new XML(evt.target.data);
my_songs = myXML.SONG;
for(var i=0;i<my_songs.length;i++) {
var randomSong = Math.floor(Math.random()*my_songs.length);
var t = my_songs*;
my_songs* = my_songs[randomSong];
my_songs[randomSong] = t;
}
playSong(randomSong);
}
function playSong(mySong:Number):void
{
var myTitle = my_songs[mySong].@TITLE;
var myArtist = my_songs[mySong].@ARTIST;
var myURL = my_songs[mySong].@URL;
trace(my_songs[mySong].@URL)
title_txt.text = myTitle;
artist_txt.text = myArtist;
if(my_channel)
{
my_channel.stop();
}
my_sound = new Sound();
my_sound.load(new URLRequest(myURL));
my_channel = my_sound.play();
my_channel.addEventListener(Event.SOUND_COMPLETE, nextSong);
}
next_btn.addEventListener(MouseEvent.CLICK, nextSong);
function nextSong(evt:Event):void
{
current_song++;
if(current_song >= my_total)
{
current_song = 0;
}
playSong(current_song);
}
prev_btn.addEventListener(MouseEvent.CLICK, prevSong);
function prevSong(evt:MouseEvent):void
{
current_song--;
if(current_song <= 0)
{
current_song = 9;
}
playSong(current_song);
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSong);
function pauseSong(evt:MouseEvent):void
{
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
play_btn.addEventListener(MouseEvent.CLICK, play_Song);
function play_Song(evt:MouseEvent):void
{
if (song_paused)
{
my_channel = my_sound.play(song_position);
song_paused = false;
}
else if (!my_channel)
{
playSong(current_song);
}
}
but it produces the error -
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at flashPlayer_fla::MainTimeline/playSong()
at flashPlayer_fla::MainTimeline/processXML()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()
I have also tried to trace the problem by putting trace(my_songs[mySong].@URL) in the playSong() function but this doesn’t do anything. Can anyone help?