Hey there all!,
I have been making a Jukebox in flash but I got stucked at roandomizing the songs - every song must be played randomized before it's allowed to be played a 2nd tim -
Some people told me to do this with an Array, but I am pretty new in this so I have NO CLUE how.
Please help me as soon as possible, it's urgent !
Heavinly much thanks in Advance
p.s: This is the flash code I got for my Jukebox :
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(); // naakt een nieuwe XML loader
myXMLLoader.load(new URLRequest("playlist.xml")); // load playlist.xml
myXMLLoader.addEventListener(Event.COMPLETE, processXML); // event aan maken
function processXML (e:Event):void // roept funcite aan
{
var myXML:XML = new XML(e.target.data);
my_songs = myXML.SONG;
my_total = my_songs.length();
playSong(1);
}
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, onNext);
}
next_btn.addEventListener(MouseEvent.CLICK, onNext);
function onNext(e:Event):void
{
current_song++;
if (current_song>=my_total)
{
current_song=0;
}
playSong(current_song);
}
prev_btn.addEventListener(MouseEvent.CLICK, onPrev);
function onPrev(e:MouseEvent):void
{
current_song--;
if (current_song<0)
{
current_song = my_total-1;
}
playSong(current_song);
}
pause_btn.addEventListener(MouseEvent.CLICK, onPause);
function onPause(e:MouseEvent):void
{
if (my_channel)
{
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
}
play_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onPlay(e:MouseEvent):void
{
if (song_paused)
{
my_channel = my_sound.play(song_position);
song_paused=false;
}
}
What you could do (to keep most of the approach you currently have, it to create a new array that just holds the numbers for the songs and then shuffle that array to randomize it. The new code is shown in bold below
var singNums:Array = new Array();
function processXML (e:Event):void // roept funcite aan
{
var myXML:XML = new XML(e.target.data);
my_songs = myXML.SONG;
my_total = my_songs.length();
for(var i:uint=0; i<my_total; i++){
songNums.push(i); // this just builds an array of numbers [0,1,2,3,4,....]
}
shuffle(songNums); // randomizes the numbers in the array
playSong(songNums[current_song] );
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
I putted it into my code and that made me this :
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(); // naakt een nieuwe XML loader
myXMLLoader.load(new URLRequest("playlist.xml")); // load playlist.xml
myXMLLoader.addEventListener(Event.COMPLETE, processXML); // event aan maken
var singNums:Array = new Array();
function processXML (e:Event):void // roept funcite aan
{
var myXML:XML = new XML(e.target.data);
my_songs = myXML.SONG;
my_total = my_songs.length();
for(var i:uint=0; i<my_total; i++){
songNums.push(i); // this just builds an array of numbers [0,1,2,3,4,....]
}
shuffle(songNums); // randomizes the numbers in the array
playSong(songNums[current_song] );
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
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, onNext);
}
next_btn.addEventListener(MouseEvent.CLICK, onNext);
function onNext(e:Event):void
{
current_song++;
if (current_song>=my_total)
{
current_song=0;
}
playSong(current_song);
}
prev_btn.addEventListener(MouseEvent.CLICK, onPrev);
function onPrev(e:MouseEvent):void
{
current_song--;
if (current_song<0)
{
current_song = my_total-1;
}
playSong(current_song);
}
pause_btn.addEventListener(MouseEvent.CLICK, onPause);
function onPause(e:MouseEvent):void
{
if (my_channel)
{
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
}
play_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onPlay(e:MouseEvent):void
{
if (song_paused)
{
my_channel = my_sound.play(song_position);
song_paused=false;
}
}
I am getting 3 errrors :
Scene 1, layer 'action', frame 1, sentence 28 1120: Using undefined songNums
Scene 1, layer 'action', frame 1, sentence 31 1120: Using undefined songNums
Scene 1, layer 'action', frame 1, sentence 33 1120: Using undefined songNums
How can I fix this?
Many thanks in advance!
I have added the code, but he keeps doing the same as before:
number 1 then 2 then 3 it doesn't randomize... and neither its giving an error since i fixed that typo....
Any clue?
I'll post the code again :
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(); // naakt een nieuwe XML loader
myXMLLoader.load(new URLRequest("playlist.xml")); // load playlist.xml
myXMLLoader.addEventListener(Event.COMPLETE, processXML); // event aan maken
var songNums:Array = new Array();
function processXML (e:Event):void // roept funcite aan
{
var myXML:XML = new XML(e.target.data);
my_songs = myXML.SONG;
my_total = my_songs.length();
for(var i:uint=0; i<my_total; i++){
songNums.push(i); // this just builds an array of numbers [0,1,2,3,4,....]
}
shuffle(songNums); // randomizes the numbers in the array
playSong(songNums[current_song] );
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
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, onNext);
}
next_btn.addEventListener(MouseEvent.CLICK, onNext);
function onNext(e:Event):void
{
current_song++;
if (current_song>=my_total)
{
current_song=0;
}
playSong(current_song);
}
prev_btn.addEventListener(MouseEvent.CLICK, onPrev);
function onPrev(e:MouseEvent):void
{
current_song--;
if (current_song<0)
{
current_song = my_total-1;
}
playSong(current_song);
}
pause_btn.addEventListener(MouseEvent.CLICK, onPause);
function onPause(e:MouseEvent):void
{
if (my_channel)
{
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
}
play_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onPlay(e:MouseEvent):void
{
if (song_paused)
{
my_channel = my_sound.play(song_position);
song_paused=false;
}
}
dont forget the mp3 files are linked through xml
thanks in advance ![]()
I got that done giving me this code, but still not doing its job ![]()
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(); // naakt een nieuwe XML loader
myXMLLoader.load(new URLRequest("playlist.xml")); // load playlist.xml
myXMLLoader.addEventListener(Event.COMPLETE, processXML); // event aan maken
var songNums:Array = new Array();
function processXML (e:Event):void // roept funcite aan
{
var myXML:XML = new XML(e.target.data);
my_songs = myXML.SONG;
my_total = my_songs.length();
for(var i:uint=0; i<my_total; i++){
songNums.push(i); // this just builds an array of numbers [0,1,2,3,4,....]
}
shuffle(songNums); // randomizes the numbers in the array
playSong(songNums[current_song]);
}
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
}
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, onNext);
}
next_btn.addEventListener(MouseEvent.CLICK, onNext);
function onNext(e:Event):void
{
current_song++;
if (current_song>=my_total)
{
current_song=0;
}
playSong(songNums[current_song]);
}
prev_btn.addEventListener(MouseEvent.CLICK, onPrev);
function onPrev(e:MouseEvent):void
{
current_song--;
if (current_song<0)
{
current_song = my_total-1;
}
playSong(songNums[current_song]);
}
pause_btn.addEventListener(MouseEvent.CLICK, onPause);
function onPause(e:MouseEvent):void
{
if (my_channel)
{
song_position = my_channel.position;
my_channel.stop();
song_paused=true;
}
}
play_btn.addEventListener(MouseEvent.CLICK, onPlay);
function onPlay(e:MouseEvent):void
{
if (song_paused)
{
my_channel = my_sound.play(song_position);
song_paused=false;
}
}
You'll have to use the trace() function to follow thru the processing to see where things are not processing as they should. Try tracing the singNums array before and after shuffling to make sure it is getting shuffled. Then try tracing the calls to the playSong function to see what the value of mySong is. Keep poking around until you find something that doesn't do what you expect it should..
North America
Europe, Middle East and Africa
Asia Pacific