ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Music Playlists | 221

The filename of the song clip is passed to the parameterized constructor and set to the


sNameproperty. Theplay( )method loads the songs from a subfolder calledmedia. The


event handler calledsongLoadedis registered to listen to theEvent.COMPLETEevent (line


25). When the song is loaded, thesongLoaded( )method pushes the song into the


aSongQueueplay queue (line 32). It then calls theplayIfIdle( )method that determines


if a song is playing and if not, gets the song fromaSongQueueusing the Shift function


(line 51), starts to play the song, and assigns it to thesoundChannelsound channel (line


56). TheaSongQueuearray functions as a song queue (first-in first-out). The event han-


dler calledplayDone is registered to listen to theEvent.SOUND_COMPLETE event (line 57).


When the sound stops playing, theEvent.SOUND_COMPLETE event will trigger the


playDone( ) method that calls theplayIfIdle( ) method again.


We can now create some playlists and listen to some music.


Building Composite Playlists


Let’s develop two playlists and embed them into a larger playlist. The following code


should be executed from the document class of a Flash document.


// create playlist
var drumlicks:Playlist = new Playlist("drum licks");
drumlicks.add(new Song("bongo.mp3"));
drumlicks.add(new Song("tabla.mp3"));
drumlicks.add(new Song("djembe.mp3"));

// create another playlist
var guitariffs:Playlist = new Playlist("guitar riffs");
guitariffs.add(new Song("acousticguitar.mp3"));
guitariffs.add(new Song("electricguitar.mp3"));

// create composite playlist
var eclectic:Playlist = new Playlist("eclectic");
eclectic.add(drumlicks);
eclectic.add(new Song("balladpiano.mp3"));
eclectic.add(guitariffs);
eclectic.play( );

The example application will first build the playlist named “drum licks.” It will then


build another playlist called “guitar riffs.” Finally, it will build and play a new


58 soundChannel.addEventListener
(Event.SOUND_COMPLETE, this.playDone);
59 }
60 }
61 }
62 }
63 }

Example 6-10. Song.as

Free download pdf