A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Random loading song? AS2/XML

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    resolved [RESOLVED] Random loading song? AS2/XML

    I originally built this in Flash 8, then moved the files over to CS4


    Hi, I have a list component where I've loaded song files through xml and everything is working great.

    I have now been trying to figure out how to make it play one of the 8 files randomly when it starts playing, instead of it always starting with a specified track (currently track one obviously). So each time a visitor comes to the site it is a different song.

    I found a few things on math for AS3 but nothing has worked for this AS2 code that I have tried so far. What am I missing?

    PHP Code:
    this._lockroot true
    //make textfields autosize 
    album_txt.autoSize "left"
    artist_txt.autoSize "left"
    title_txt.autoSize "left"
    //create sound object 
    var songInterval:Number
    var 
    amountLoaded:Number
    var 
    mySound:Sound
    var 
    nextTrack:Number

    //this will contain all the track details from the xml file 
    var tracks_array:Array = new Array(); 
    var 
    totalTracks:Number

    //create the XML object and populate with track details 
    var jukebox_xml:XML = new XML(); 
    jukebox_xml.ignoreWhite true
    var 
    RootNode:XMLNode

    jukebox_xml.onLoad = function(success:Boolean) { 
        if (
    success) { 
        
    RootNode this.firstChild
        
    totalTracks RootNode.childNodes.length
        
    populateTracksArray(); 
        } else { 
        
    trace("error loading xml file"); 
        } 
    }; 
    jukebox_xml.load("tracks.xml"); 

    function 
    populateTracksArray():Void
        for(var 
    i:Number=0totalTracks;i++){ 
            
    tracks_array[i] = RootNode.childNodes[i]; 
            
    tracks_array[i].source RootNode.childNodes[i].attributes.source
            
    tracks_array[i].artist RootNode.childNodes[i].attributes.artist
            
    tracks_array[i].album RootNode.childNodes[i].attributes.album
            
    tracks_array[i].title RootNode.childNodes[i].attributes.title
            } 


    //play first track  
            
    playTrack(tracks_array[0].source,tracks_array[0].artist,tracks_array[0].album,tracks_array[0].title,true); 
    //populate list component with song titles 
            
    populateList(); 


    function 
    playTrack(sourceartistalbumtitlestream):Void
            
    artist_txt.text artist
            
    album_txt.text album
            
    title_txt.text title
            
    mySound = new Sound(); 
             
            
    mySound.onSoundComplete = function():Void
                 
    //----LETS NEXT SONG PLAY---------  
        
    if(continous_comp.selected){ 
                if(
    list_comp.selectedIndex < (totalTracks -1)){ 
        
    list_comp.selectedIndex +=1
        }else{ 
            
    list_comp.selectedIndex 0
        } 
        
    playTrack(tracks_array[list_comp.selectedIndex].source,tracks_array[list_comp.selectedIndex].artist,tracks_array[list_comp.selectedIndex].album,tracks_array[list_comp.selectedIndex].titletrue); 
        } 
                } 
         
            
    mySound.loadSound(source,stream); 
            
    clearInterval(songInterval); 
            
    songInterval setInterval(songProgress,100); 


    function 
    populateList():Void
        for(var 
    i:Number=0i<totalTracks;i++){ 
         
    list_comp.addItem(tracks_array[i].title,tracks_array[i].source); 
         } 
         
    _root.list_comp.selectedIndex 0


    //--------create a listener for the list component------------ 
    var compListener:Object = new Object(); 
    list_comp.addEventListener("change"compListener); 

    compListener.change = function(info:Object):Void
     
    playTrack(info.target.value,tracks_array[list_comp.selectedIndex].artist,tracks_array[list_comp.selectedIndex].album,tracks_array[list_comp.selectedIndex].titletrue); 


  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    here's the AS 2.0 entry for the Math.random method.

    gparis

  3. #3
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    Thanks gparis

    Thanks for the link gparis. I have checked that out and tried to implement it numerous ways but can't seem to get it to connect properly.

    And the search continues!

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    you can use the exact function in that link. since you only want 1 random integer, you don't need the for loop in their example, but the way the function is called still stands.

    And in case you don't know that already, since you're going to use that function with an array, the first (min) integer should be 0.

    gparis

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    Re:

    Thanks for your patience. I can promise you I am trying. So I've made these changes as the latest but to no avail.

    Changed:
    PHP Code:
    //play first track          playTrack(tracks_array[0].source,tracks_array[0].artist,tracks_array[0].album,tracks_array[0].title,true); 
    To:
    PHP Code:
    playTrack(randomNum
    And added this code like you said with the change to 0 instead of 1 for our starting track:

    PHP Code:
    function randRange(min:Numbermax:Number):Number {
        var 
    randomNum:Number Math.floor(Math.random() * (max min 0)) + min;
        return 
    randomNum;


    And the output is coming up as undefined?

    I'm totally missing something completely. I haven't been able to find a tutorial online or in any of my books since 8:30am this morning. Not giving up it is just pretty frustrating.

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    You're not calling the function.. also, you still need to give a range in that randomness

    Example online:
    var n:Number = randRange(4, 11)

    will generate a random number between 4 and 11.

    so use that:

    var n:Number = randRange(0, thenumberofsongs)
    in your case i think thenumberofsongs is totalTracks


    Then, replace 0 with n. as in, for example tracks_array[n]


    gparis

  7. #7
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    Some success! gparis you my friend are a wizard!

    Thanks so much, I really appreciate you taking your time to help me with this and walk me through it. Really really cool.

    So I couldn't get the files to start playing without hard coding the song number - set up like this:

    PHP Code:
    var n:Number randRange(08);
    function 
    randRange(min:Numbermax:Number) :Number{
       var 
    randomNum:Number Math.floor(Math.random() * (max min 0)) + min;
    return 
    randomNum;

    And then like you said, changing the 0 starting song out with n
    So that entire piece is like this now:

    PHP Code:
    var n:Number randRange(08);
    function 
    randRange(min:Numbermax:Number) :Number{
       var 
    randomNum:Number Math.floor(Math.random() * (max min 0)) + min;
    return 
    randomNum;
    }

    function 
    populateTracksArray():Void{
        for(var 
    i:Number=0totalTracks;i++){
            
    tracks_array[i] = RootNode.childNodes[i];
            
    tracks_array[i].source RootNode.childNodes[i].attributes.source;
            
    tracks_array[i].artist RootNode.childNodes[i].attributes.artist;
            
    tracks_array[i].album RootNode.childNodes[i].attributes.album;
            
    tracks_array[i].title RootNode.childNodes[i].attributes.title;
            }

    //play first random track  
            
    playTrack(tracks_array[n].source,tracks_array[i].artist,tracks_array[i].album,tracks_array[i].title,true); 

    So as I refresh the file the actual song does change and is different but the list component doesn't go to (highlight) the correct song that is playing. It just goes to the first box.

    I tried many combinations to see if I could get it to work and ended up with a lot of undefined areas in most cases.

    Any insight?

  8. #8
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    Ah got it!

    Found it! Thanks to all your help gparis.
    To get the highlight to land on the correct track I changed this:


    PHP Code:
    //----LETS NEXT SONG PLAY---------  
        
    if(continous_comp.selected){
                if(
    list_comp.selectedIndex < (totalTracks -1)){
        
    list_comp.selectedIndex +=1;
        }else{
            
    list_comp.selectedIndex n;
        }
        
    playTrack(tracks_array[list_comp.selectedIndex].source,tracks_array[list_comp.selectedIndex].artist,tracks_array[list_comp.selectedIndex].album,tracks_array[list_comp.selectedIndex].titletrue);
        }
                }
         
            
    mySound.loadSound(source,stream);
            
    clearInterval(songInterval);
            
    songInterval setInterval(songProgress,100);
    }

    function 
    populateList():Void{
        for(var 
    i:Number=0i<totalTracks;i++){
         
    list_comp.addItem(tracks_array[i].title,tracks_array[i].source);
         }
         
    _root.list_comp.selectedIndex n;

    I replaced the two 0's with n's.

    Thanks again for all your help. I'm still unsure why I couldn't reference
    the totalTracks instead of the hard numbers

  9. #9
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    is there a way to mark this closed?

    Is there a way to mark this thread closed or anything?

  10. #10
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    thread tools > mark thread resolved

    gparis

  11. #11
    Junior Member
    Join Date
    Feb 2009
    Location
    Charlotte, NC, US
    Posts
    14

    Thanks again

    Again thanks for all your help and time.

    You website is really cool. Love the dog!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center