Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.)
Copy link to clipboard
Copied
Hey kglad thanks again for your time and help, but that code didn't change anything, the same song still repeats when I click on another song in the list component.
Copy link to clipboard
Copied
use the trace statement to see if your xml parsing code is correct:
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("mySong List").toXMLString(),
data:item.attribute("url").toXMLString(),
URL:songName});
}
trace(item.attribute("url").toXMLString())
myList.selectedIndex=0;
myList.addEventListener(Event.CHANGE, listListener);
myChannel=myList.selectedItem.data;
myChannel.stop();
lastPosition = 0;
}
myList.addEventListener(ListEvent.ITEM_CLICK,listListener);
function listListener(e:ListEvent):void{
myChannel.stop();
trace(e.item.data);
myChannel=mySound.play(e.item.data);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now