Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 10. Working with an XML Playlist > Updating the chooseSong() function

Updating the chooseSong() function

Recall from Lesson 9 that when any one of the song clips (song1 through song6) is clicked, the chooseSong() function is called. For this lesson’s version of the project, most of this functionality will remain the same. The one element that you will need to modify, to take into account the XML source of the song data, is the switch statement that determines the currently playing song.

1.
Locate the switch statement in the chooseSong() function. The first few lines of this statement should read:

switch (e.currentTarget.name) {
 case "song1" :
 currSong = "../MP3s/" + songList[0] as String;
 break;

To keep track of the songs in the list, you’ll give a different value (depending on which song clip is clicked) to the songNum variable that you created earlier.

2.
Below the line that reads:

case "song1":

add the following new line:

songNum = 0;

Very soon it will be clearer how this value will be used.

Notice that the currSong variable is still taking its value from the songList array that you deleted earlier. Update this value so that it now gets the current song from the XML data.

3.
Change the line that reads:

currSong = "../MP3s/" + songList[0] as String;

so that it now reads:

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

This approach is similar to the way that you retrieved the title and artist information for the song clips, only now you are retrieving the file element from the XML data. Since the value of songCount is initially 0, the variable will have no effect when added to songNum at this point. However, you will make use of this variable soon.

The first condition in the switch statement should now read:

case "song1":
songNum = 0;
currSong = songList_XML.song[songNum + songCount].file;
break;

4.
Update the rest of the switch statement in a similar manner. The full switch statement should read:

switch (e.currentTarget.name) {
 case "song1":
  songNum = 0;
  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;
}

					  

You should now be able to test the file without getting error messages.

5.
Test the movie to see the results so far.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial