-
Random XML MP3 Player
I have an MP3 player that is using an external XML file. I want this MP3 player to load a random song each time on runtime. I have this code -
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;
my_total = my_songs.length();
for(var i=0;i<my_songs.length;i++) {
var randomSong = Math.floor(Math.random()*my_songs.length);
var t = my_songs[i];
my_songs[i] = my_songs[randomSong];
my_songs[randomSong] = t;
}
playSong(i);
}
function playSong(mySong:Number):void
{
var myTitle = my_songs[mySong].@TITLE;
var myArtist = my_songs[mySong].@ARTIST;
var myURL = 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 <= my_total)
{
current_song = 2;
}
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);
}
}
Its not producing an error, but the first song from the XML file plays each time. Can anyone help?
-
Senior Member
try this:
var randomSong = Math.floor(Math.random()*my_songs.length);
playSong(randomSong);
- The right of the People to create Flash movies shall not be infringed. -
-
that produces this 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::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader:nComplete()
-
i managed to fix it, it was really simple in the end. I'd missed out some parentheses here for(var i=0;i<my_songs.length;i++) { so it should have been for(var i=0;i<my_songs.length();i++) {
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|