Skip to main content
Participating Frequently
September 23, 2015
Question

S.O.S. Help! how to populate xml playlist to list component.

  • September 23, 2015
  • 1 reply
  • 369 views

I am very new to actionscript 3.0 and working on my first project. I am trying to populate an xml driven mp3 playlist to the list component.

The list component is populated but when I click on list to play another song, the same song repeats and plays over the existing sound.

Can anybody give me a little help on this situation.

here's the code im working with. I left out some stuff but here is the basics just to get the list populated and playing correctly.

Thank you for your time.

var mySongList:XMLList;   

var myTotal:Number;   

var mySound:Sound;   

var myChannel:SoundChannel;

var myXMLLoader:URLLoader = new URLLoader();   
myXMLLoader.load(new URLRequest("PlayList5.xml"));   
myXMLLoader.addEventListener(Event.COMPLETE, processXML); 

function processXML(myEvent:Event):void {
    var myXML:XML=new XML(myXMLLoader.data);
    mySongList=myXML.song;
    myTotal=mySongList.length();
    myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
    myXMLLoader=null;
    pause_btn.gotoAndStop(2);

var item:XML;
for each (item in myXML.song) {
var songName:String;
if (item.hasOwnProperty("@songName")>0) {
songName=item.@songName;
}
myList.addItem({label:item.attribute("songName").toXMLString(),str:item.attribute("mySongList").toXMLString(),
data:item.attribute("url").toXMLString(),
URL:songName});
}  
myList.selectedIndex=0;
myList.addEventListener(Event.CHANGE, listListener);
myChannel=myList.selectedItem.data;
myChannel.stop();
lastPosition = 0;

}
function listListener(event:Event):void {
//mySound.play(event.target.selectedItem.mySongList);
mySound.play(event.target.selectedItem.data);

}

function playSong(mySong:Number):void {
    var myTitle=mySongList[mySong].@songName;
    var myArtist=mySongList[mySong].@artist;
    var myURL=mySongList[mySong].@url;
    Artist.text=myArtist;
Title.text=myTitle;
myTimer.start();

    if (myChannel) {
        myChannel.stop();
        myChannel=null;
        play_btn.visible = false;
  pause_btn.visible = true;
     myTimer.stop();
}    
    mySound = new Sound();
    mySound.load(new URLRequest(myURL)); 
    myChannel=mySound.play(lastPosition);
    myChannel.addEventListener(Event.SOUND_COMPLETE, nextSong);
    pause_btn.gotoAndStop(1);   
}
function advanceSong(mySong:Number):void {
    var myTitle=mySongList[mySong].@songName;
    var myArtist=mySongList[mySong].@artist;
    var myURL=mySongList[mySong].@url;
     Artist.text=myArtist;
  Title.text=myTitle;

function playPauseSong(myEvent:MouseEvent):void {
    if (myChannel) {
        lastPosition=myChannel.position;
        myChannel.stop();
        myChannel=null;
        play_btn.visible = true;
  pause_btn.visible = false;
  pause_btn.gotoAndStop(2);
        myTimer.stop();
} else {
        playSong(currentSong);
        play_btn.visible = false;
  pause_btn.visible = true;
     myTimer.start();
}
}   
function nextSong(myEvent:Event):void {
    currentSong++;
    lastPosition=0;
if (currentSong>=myTotal) {
    currentSong=0;
}
if (myChannel) {
    playSong(currentSong);
    myTimer.start();
} else {
     advanceSong(currentSong);
}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
September 24, 2015

stop the previous soundchannel before defining the new one.

function listListener(event:Event):void {

//mySound.play(event.target.selectedItem.mySongList);

myChannel.stop()

myChannel= mySound.play(event.target.selectedItem.data);

}

Participating Frequently
September 24, 2015

Thank You kglad, that stopped the sound from doubling, but when I try to click on another song it still repeats the same song no matter what song I click on.

kglad
Community Expert
Community Expert
September 24, 2015

i don't really understand what you're doing with a change listener but normally you would use a click listener to select items from a list component.

myList.addEventListener(ListEvent.ITEM_CLICK,listListener);

function listListener(e:ListEvent):void{

myChannel.stop();

myChannel=mySound.play(e.item.data);

}

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)