So I finished this tutorial in AS3 Classroom In A Book… but rather unhelpfully there is no code for pausing the music or skipping to the next track. I was wondering if anyone could give me a hand introducing these into the code below…
I have created 3 buttons, one to pause (pause_btn) which also doubles as a play button, one to skip forward a track (next_btn) and one to skip back (prev_btn).
****/ NOT SO IMPORTANT but…
I have also created a space for album artwork and I have assigned one picture to each track. Does anyone know a way for me to upload those pictures to that space depending on what track is playing?
import fl.events.SliderEvent;
//create instances of the three sound related classes that will be used for this project
var snd:Sound;
var channel:SoundChannel;
var trans:SoundTransform;
//create variables to store values for the current song and it's volume and pan settings.
var currSong:String;
var currVol:int;
var currPan:int;
//two new variables to track current song.
var songCount:int = 0;
var songNum:int = 0;
// don't need to see the volume and pan controls until a song is playing
panSlide.visible = false;
volSlide.visible = false;
//link button also
//creates new XML instance and loads the songlist.xml file
var songList_XML:XML;
var xmlReq:URLRequest = new URLRequest("songlist.xml");
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(xmlReq);
//adds an event listener for successful completion of XML load.
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
function errorHandler(event:IOErrorEvent):void {
songTitle.text = "XML loading error: " + event;
}
//that takes place when XML loading is complete
function xmlLoaded(event:Event):void {
songList_XML = new XML(xmlLoader.data);
//Listeners for the onstage song buttons
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);
setSongs();
}
//sets the text field of all of the song buttons to display the names of the songs
//the for loop to set the song titles now takes place when the XML file with the information is loaded.
function setSongs():void {
for(var i = 0; i < 6; i++) {
var titleText:String = songList_XML.song[i + songCount].name;
var artistText:String = songList_XML.song[i + songCount].artist;
var clipTitle = this["song" + (i + 1)].title;
var clipArtist = this["song" + (i + 1)].artist;
clipTitle.text = titleText;
clipArtist.text=artistText;
}
}
//listeners for the volume and pan sliders
panSlide.addEventListener(SliderEvent.CHANGE, panChange);
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].file;
break;
case "song3":
songNum = 2;
currSong = songList_XML.song[songNum].file;
break;
case "song4":
songNum = 3;
currSong = songList_XML.song[songNum].file;
break;
case "song5":
songNum = 4;
currSong = songList_XML.song[songNum].file;
break;
case "song6":
songNum = 5;
currSong = songList_XML.song[songNum].file;
break;
}
if (snd != null) {
channel.stop();
}
snd = new Sound();
//loads the current song into the instance of the Sound class
// This has no error or loading progress checking
snd.load(new URLRequest(currSong));
channel = new SoundChannel;
trans = new SoundTransform(.5, 0);
channel.soundTransform = trans;
channel = snd.play();
panSlide.visible = true;
volSlide.visible = true;
//link button made visible here along with sliders
//currVolume and pan values are used here for display in the text fields next to sliders
currVol = trans.volume * 100;
currPan = trans.pan * 100;
volLabel.text = "Current Volume " + currVol;
panLabel.text = "Current Pan " + currPan;
//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;
info.text = id3.artist;
}
}
// uses volume slider value to control volume
function volumeChange(e:SliderEvent):void {
currVol = e.target.value * 100;
volLabel.text = "Current Volume: " + currVol;
trans.volume = e.target.value;
channel.soundTransform = trans;
}
// uses pan slider value to control pan
function panChange(e:SliderEvent):void {
currPan = e.target.value*100;
panLabel.text = "Current Pan " + currPan;
trans.pan = e.target.value;
channel.soundTransform = trans;
}