Skip to main content
July 25, 2012
Question

xml playlist loading lag when new song is chosen

  • July 25, 2012
  • 2 replies
  • 1262 views

My site has multiple xml playlists for individual CD icons.  When a song is chosen it loads well and plays, however, when a new song is chosen while the initial choice plays, the first song stops as expected, but the next song has long lags (not always) before it loads and plays.  It does eventually load and play.  I suspect the new song won't load until the first song completes its load.  How can I code a stop load if this is indeed what is occuring.  Here's the initial xml code I am using:

var snd:Sound;

var channel:SoundChannel;

var trans:SoundTransform;

//create variables to store values for the current song and it's volume.

var currSong:String;

var currVol:Number = .5;

var songCount:int = 0;

var songNum:int = 0;

var songList_XML:XML;

var xmlReq:URLRequest = new URLRequest("songlist.xml");

var xmlLoader:URLLoader = new URLLoader();

var songList2_XML:XML;

var xmlReq2:URLRequest = new URLRequest("songlist2.xml");

var xmlLoader2:URLLoader = new URLLoader();

var songList3_XML:XML;

var xmlReq3:URLRequest = new URLRequest("songlist3.xml");

var xmlLoader3:URLLoader = new URLLoader();

var songList4_XML:XML;

var xmlReq4:URLRequest = new URLRequest("songlist4.xml");

var xmlLoader4:URLLoader = new URLLoader();

var songList5_XML:XML;

var xmlReq5:URLRequest = new URLRequest("songlist5.xml");

var xmlLoader5:URLLoader = new URLLoader();

var songList6_XML:XML;

var xmlReq6:URLRequest = new URLRequest("songlist6.xml");

var xmlLoader6:URLLoader = new URLLoader();

var songList7_XML:XML;

var xmlReq7:URLRequest = new URLRequest("songlist7.xml");

var xmlLoader7:URLLoader = new URLLoader();

if (snd != null) {

        channel.stop();

    }

Here is code from one of the CD's stops:

xmlLoader.load(xmlReq);

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

function errorHandler(event:IOErrorEvent):void {

            songTitle.text = "XML loading error: " + event;

        }

function xmlLoaded(event:Event):void {

    songList_XML = new XML(xmlLoader.data);

   

    song1.addEventListener(MouseEvent.CLICK, chooseSong);

    song2.addEventListener(MouseEvent.CLICK, chooseSong);

    song3.addEventListener(MouseEvent.CLICK, chooseSong);

    song4.addEventListener(MouseEvent.CLICK, chooseSong);

    song5.addEventListener(MouseEvent.CLICK, chooseSong);

    song6.addEventListener(MouseEvent.CLICK, chooseSong);

    song7.addEventListener(MouseEvent.CLICK, chooseSong);

    song8.addEventListener(MouseEvent.CLICK, chooseSong);

    song9.addEventListener(MouseEvent.CLICK, chooseSong);

    song10.addEventListener(MouseEvent.CLICK, chooseSong);

    song11.addEventListener(MouseEvent.CLICK, chooseSong);

    song12.addEventListener(MouseEvent.CLICK, chooseSong);

   

    setSongs();

}

//sets the text field of all of the song buttons to display the names of the songs in the songList array

function setSongs():void {

    for(var i = 0; i < 12; i++) {

        var titleText:String = songList_XML.song[i + songCount].name;

        var clipTitle = this["song" + (i + 1)].title;

        clipTitle.text = titleText;

        }

}

volSlide.addEventListener(SliderEvent.CHANGE, volumeChange);

//switch statement to set the current song based on which song button was clicked.

function chooseSong(e:MouseEvent):void {

    switch (e.currentTarget.name) {

        case "song1":

        //sets the currSong based on the XML file

        // the songNum variable is based on which button was selected

            songNum = 0;

            //the songCount variable makes it possible to have more songs in the XML file than buttons on the interface. songCount is incremented when the user clicks the moreSongs button onstage.

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song2":

            songNum = 1;

            currSong = songList_XML.song[songNum+songCount].file;

            break;

        case "song3":

            songNum = 2;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song4":

            songNum = 3;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song5":

            songNum = 4;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song6":

            songNum = 5;

            currSong = songList_XML.song[songNum + songCount].file;

            break;   

           

        case "song7":

            songNum = 6;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song8":

            songNum = 7;

            currSong = songList_XML.song[songNum + songCount].file;

            break;   

           

        case "song9":

            songNum = 8;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song10":

            songNum = 9;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song11":

            songNum = 10;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song12":

            songNum = 11;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

               

                   

    }

    if (snd != null) {

        channel.stop();

    }

    snd = new Sound();

    snd.load(new URLRequest(currSong));

    channel = new SoundChannel  ;

    trans = new SoundTransform(currVol);

    channel = snd.play();

    channel.soundTransform = trans;

   

    volSlide.visible = true;

    //currVolume and pan values are used here for display in the text fields next to sliders

    volLabel.text = "Current Volume " + int(currVol * 100);

   

    //listens for arrival of ID3 tags

    snd.addEventListener(Event.ID3, id3Handler);

}

//triggered when id3 tags are available

//sets info text field to display current song information from id3 tags.

function id3Handler(event:Event):void {

    var id3:ID3Info = snd.id3;

    if (id3.songName != null) {

        songTitle.text = id3.songName + "\n";

       

    }

}

// uses volume slider value to control volume

function volumeChange(e:SliderEvent):void {

    currVol = e.target.value;

    volLabel.text = "Current Volume: " + int(currVol*100);

    trans.volume = currVol;

    channel.soundTransform = trans;

}

If it is not what I suspect please point me in the right direction.  Any help with this is greatly appreciated.

mellowcore

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
July 27, 2012

:

var loading:Boolean;

var snd:Sound;

var channel:SoundChannel;

var trans:SoundTransform;

//create variables to store values for the current song and it's volume.

var currSong:String;

var currVol:Number = .5;

var songCount:int = 0;

var songNum:int = 0;

var songList_XML:XML;

var xmlReq:URLRequest = new URLRequest("songlist.xml");

var xmlLoader:URLLoader = new URLLoader();

var songList2_XML:XML;

var xmlReq2:URLRequest = new URLRequest("songlist2.xml");

var xmlLoader2:URLLoader = new URLLoader();

var songList3_XML:XML;

var xmlReq3:URLRequest = new URLRequest("songlist3.xml");

var xmlLoader3:URLLoader = new URLLoader();

var songList4_XML:XML;

var xmlReq4:URLRequest = new URLRequest("songlist4.xml");

var xmlLoader4:URLLoader = new URLLoader();

var songList5_XML:XML;

var xmlReq5:URLRequest = new URLRequest("songlist5.xml");

var xmlLoader5:URLLoader = new URLLoader();

var songList6_XML:XML;

var xmlReq6:URLRequest = new URLRequest("songlist6.xml");

var xmlLoader6:URLLoader = new URLLoader();

var songList7_XML:XML;

var xmlReq7:URLRequest = new URLRequest("songlist7.xml");

var xmlLoader7:URLLoader = new URLLoader();

if (snd != null) {

        channel.stop();

    }

Here is code from one of the CD's stops:

xmlLoader.load(xmlReq);

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

function errorHandler(event:IOErrorEvent):void {

            songTitle.text = "XML loading error: " + event;

        }

function xmlLoaded(event:Event):void {

    songList_XML = new XML(xmlLoader.data);

   

    song1.addEventListener(MouseEvent.CLICK, chooseSong);

    song2.addEventListener(MouseEvent.CLICK, chooseSong);

    song3.addEventListener(MouseEvent.CLICK, chooseSong);

    song4.addEventListener(MouseEvent.CLICK, chooseSong);

    song5.addEventListener(MouseEvent.CLICK, chooseSong);

    song6.addEventListener(MouseEvent.CLICK, chooseSong);

    song7.addEventListener(MouseEvent.CLICK, chooseSong);

    song8.addEventListener(MouseEvent.CLICK, chooseSong);

    song9.addEventListener(MouseEvent.CLICK, chooseSong);

    song10.addEventListener(MouseEvent.CLICK, chooseSong);

    song11.addEventListener(MouseEvent.CLICK, chooseSong);

    song12.addEventListener(MouseEvent.CLICK, chooseSong);

   

    setSongs();

}

//sets the text field of all of the song buttons to display the names of the songs in the songList array

function setSongs():void {

    for(var i = 0; i < 12; i++) {

        var titleText:String = songList_XML.song[i + songCount].name;

        var clipTitle = this["song" + (i + 1)].title;

        clipTitle.text = titleText;

        }

}

volSlide.addEventListener(SliderEvent.CHANGE, volumeChange);

//switch statement to set the current song based on which song button was clicked.

function chooseSong(e:MouseEvent):void {

    switch (e.currentTarget.name) {

        case "song1":

        //sets the currSong based on the XML file

        // the songNum variable is based on which button was selected

            songNum = 0;

            //the songCount variable makes it possible to have more songs in the XML file than buttons on the interface. songCount is incremented when the user clicks the moreSongs button onstage.

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song2":

            songNum = 1;

            currSong = songList_XML.song[songNum+songCount].file;

            break;

        case "song3":

            songNum = 2;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song4":

            songNum = 3;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song5":

            songNum = 4;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

        case "song6":

            songNum = 5;

            currSong = songList_XML.song[songNum + songCount].file;

            break;   

           

        case "song7":

            songNum = 6;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song8":

            songNum = 7;

            currSong = songList_XML.song[songNum + songCount].file;

            break;   

           

        case "song9":

            songNum = 8;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song10":

            songNum = 9;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song11":

            songNum = 10;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

           

        case "song12":

            songNum = 11;

            currSong = songList_XML.song[songNum + songCount].file;

            break;

               

                   

    }

    if (snd != null) {

        channel.stop();

    }

if(loading){

snd.close();

}

    snd = new Sound();

snd.addEventListener(Event.COMPLETE,loadcompleteF);

loading=true;

    snd.load(new URLRequest(currSong));

    channel = new SoundChannel  ;

    trans = new SoundTransform(currVol);

    channel = snd.play();

    channel.soundTransform = trans;

   

    volSlide.visible = true;

    //currVolume and pan values are used here for display in the text fields next to sliders

    volLabel.text = "Current Volume " + int(currVol * 100);

   

    //listens for arrival of ID3 tags

    snd.addEventListener(Event.ID3, id3Handler);

}

function loadcompleteF(e:Event):void{

loading=false;

}

//triggered when id3 tags are available

//sets info text field to display current song information from id3 tags.

function id3Handler(event:Event):void {

    var id3:ID3Info = snd.id3;

    if (id3.songName != null) {

        songTitle.text = id3.songName + "\n";

       

    }

}

// uses volume slider value to control volume

function volumeChange(e:SliderEvent):void {

    currVol = e.target.value;

    volLabel.text = "Current Volume: " + int(currVol*100);

    trans.volume = currVol;

    channel.soundTransform = trans;

}

If it is not what I suspect please point me in the right direction.  Any help with this is greatly appreciated.

mellowcore

July 30, 2012

kGlad:

I appreciate the help, but continue to struggle with fully understanding the application of this snd.close(); to my multi CD and xml songlist site.  I still seem to be suffering the load lags despite introducing the snd.close(); code.  I thought I might see no song load lags on the CD stop where this code was introduced, but still have lags on the other CD stops that haven't added snd.close code, but there are still lags even on the doced CD stop. 

I am thinking each CD stop will require a unique version of the snd.close(); code or does this coding on the first CD stop direct all similar occurrences within the .swf.

OR

If the viewer chooses anything other than CD 1 (the stop where the snd.close():  code is placed)  as their initial choice, is the code not yet active - therefore requiring additional variables for var loading:boolean; such as adding "var loading2:boolean", "var loading3:boolean;,...etc.  as well as multiple definitions of var snd=Sound - "var snd2=Sound2.

Here's CD stop #2's code:

stop();

soTastee_btn3.addEventListener(MouseEvent.CLICK, goSoTastee3);

function goSoTastee3(E:MouseEvent):void {

    gotoAndPlay("soTastee");

}

noTicketBack_btn3.addEventListener(MouseEvent.CLICK, goNoTicketBack3);

function goNoTicketBack3(E:MouseEvent):void {

    gotoAndPlay("noTicketBack");

}

tranquasaurusRx_btn3.addEventListener(MouseEvent.CLICK, goTranquasaurusRx3);

function goTranquasaurusRx3(E:MouseEvent):void {

    gotoAndPlay("tranquasaurusRx");

}

cruiseomatic_btn3.addEventListener(MouseEvent.CLICK, goCruiseomatic3);

function goCruiseomatic3(E:MouseEvent):void {

    gotoAndPlay("cruiseomatic");

}

consume_btn3.addEventListener(MouseEvent.CLICK, goConsume3);

function goConsume3(E:MouseEvent):void {

    gotoAndPlay("consume");

}

greatestHits_btn3.addEventListener(MouseEvent.CLICK, goGreatestHits3);

function goGreatestHits3(E:MouseEvent):void {

    gotoAndPlay("greatestHits");

}

volSlide.visible=false;

xmlLoader.load(xmlReq2);

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded2);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler2);

function errorHandler2(event:IOErrorEvent):void {

            songTitle.text = "XML loading error: " + event;

        }

function xmlLoaded2(event:Event):void {

    songList2_XML = new XML(xmlLoader.data);

   

    song1.addEventListener(MouseEvent.CLICK, chooseSong2);

    song2.addEventListener(MouseEvent.CLICK, chooseSong2);

    song3.addEventListener(MouseEvent.CLICK, chooseSong2);

    song4.addEventListener(MouseEvent.CLICK, chooseSong2);

    song5.addEventListener(MouseEvent.CLICK, chooseSong2);

    song6.addEventListener(MouseEvent.CLICK, chooseSong2);

    song7.addEventListener(MouseEvent.CLICK, chooseSong2);

    song8.addEventListener(MouseEvent.CLICK, chooseSong2);

    song9.addEventListener(MouseEvent.CLICK, chooseSong2);

    song10.addEventListener(MouseEvent.CLICK, chooseSong2);

    song11.addEventListener(MouseEvent.CLICK, chooseSong2);

    song12.addEventListener(MouseEvent.CLICK, chooseSong2);

   

    setSongs2();

}

//sets the text field of all of the song buttons to display the names of the songs in the songList array

function setSongs2():void {

    for(var i = 0; i < 12; i++) {

        var titleText:String = songList2_XML.song[i + songCount].name;

        var clipTitle = this["song" + (i + 1)].title;

        clipTitle.text = titleText;

        }

}

volSlide.addEventListener(SliderEvent.CHANGE, volumeChange2);

//switch statement to set the current song based on which song button was clicked.

function chooseSong2(e:MouseEvent):void {

    switch (e.currentTarget.name) {

        case "song1":

        //sets the currSong based on the XML file

        // the songNum variable is based on which button was selected

            songNum = 0;

            //the songCount variable makes it possible to have more songs in the XML file than buttons on the interface. songCount is incremented when the user clicks the moreSongs button onstage.

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

        case "song2":

            songNum = 1;

            currSong = songList2_XML.song[songNum+songCount].file;

            break;

        case "song3":

            songNum = 2;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

        case "song4":

            songNum = 3;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

        case "song5":

            songNum = 4;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

        case "song6":

            songNum = 5;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;   

           

        case "song7":

            songNum = 6;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

           

        case "song8":

            songNum = 7;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;   

           

        case "song9":

            songNum = 8;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

           

        case "song10":

            songNum = 9;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

           

        case "song11":

            songNum = 10;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

           

        case "song12":

            songNum = 11;

            currSong = songList2_XML.song[songNum + songCount].file;

            break;

                                   

    }

    if (snd != null) {

        channel.stop();

    }

    snd = new Sound();

    snd.load(new URLRequest(currSong));

    channel = new SoundChannel  ;

    trans = new SoundTransform(currVol);

    channel = snd.play();

    channel.soundTransform = trans;

   

    volSlide.visible = true;

    //currVolume and pan values are used here for display in the text fields next to sliders

    volLabel.text = "Current Volume " + int(currVol * 100);

   

    //listens for arrival of ID3 tags

    snd.addEventListener(Event.ID3, id3Handler2);

}

//triggered when id3 tags are available

//sets info text field to display current song information from id3 tags.

function id3Handler2(event:Event):void {

    var id3:ID3Info = snd.id3;

    if (id3.songName != null) {

        songTitle.text = id3.songName + "\n";

       

    }

}

// uses volume slider value to control volume

function volumeChange2(e:SliderEvent):void {

    currVol = e.target.value;

    volLabel.text = "Current Volume: " + int(currVol*100);

    trans.volume = currVol;

    channel.soundTransform = trans;

}

Hope you can guide me further.

mellowcore

kglad
Community Expert
Community Expert
July 30, 2012

you need to use snd.close() on each stream that's still loading.  that should happen if you use the code i suggested and that looks like the code you're using.

if you're using no other code, other than what you've shown, there's should be no time when 2 or more streams are loading at the same time.

however, you mention "Here's CD stop #2's code" and something about other CD stops.  what do you mean by that?

kglad
Community Expert
Community Expert
July 25, 2012

if a sound is still loading (use the complete event), you can use the sound class'es close() method to stop the download.

July 27, 2012

kgald:

Thanks for this - close class() suggestion.  I am struggling a bit with how to incorporate it.  In my chooseSong function I switch to the new song and suspect need to add a snd.close() to stop the loading of the cuurent song.  It is the action of clicking the new song title/button that triggers both the chooseSong function that finds and plays the new song, yet will also need to trigger the snd.close() to stop the loading of the current song. 

Can you provide a bit more guidance ?

mellowcore