PLAYER INFORMATION
1. Version 4.5
2. autostart: true
SCENARIO
1. The page `VV` is loaded with a player initialized with an empty playlist. The page itself has list of songs from various playlist.
2. The user `XX` is clicking on the song `YY` which is in the playlist `ZZ`. By clicking this, the current song will be stopped, the selected playlist will be loaded to the player, and the player will be instructed to jump to the selected song.
CODE
var trackNumber = this.id;
player.sendEvent('STOP');
player.sendEvent('LOAD', 'playlist.xml');
player.sendEvent('ITEM', trackNumber);
PROBLEM
Even though the selected playlist is loaded, the selected song is not playing. Instead, the first song in the playlist is the one being played.
OTHERS
I have tried various `trick`, but none seem to work.
player.sendEvent('STOP');
player.sendEvent('LOAD', 'playlist.xml');
player.sendEvent('ITEM', trackNumber);
player.sendEvent('STOP');
setTimeout("player.sendEvent('LOAD', 'playlist.xml')", 500); # various amount of time has been tested (10-2500)
player.sendEvent('ITEM', trackNumber);
player.sendEvent('STOP');
player.sendEvent('LOAD', 'playlist.xml');
setTimeout("player.sendEvent('ITEM', trackNumber), 500); # various amount of time has been tested (10-2500)
player.sendEvent('STOP');
player.sendEvent('LOAD', 'playlist.xml');
player.sendEvent('STOP');
player.sendEvent('ITEM', trackNumber);
player.sendEvent('STOP');
player.sendEvent('LOAD', 'playlist.xml');
player.sendEvent('STOP');
player.sendEvent('ITEM', trackNumber);
player.sendEvent('PLAY');
Using these tricks sometimes make the player don't play any song at all. Has anyone experience this? Any help is greatly appreciated.

This mis-behavior is probably happening because it takes time (usually 250-1500 ms) for the playlist to be loaded before you can index to a particular track.
This sequence of Events is a very crude way to make it work. Note that trackNumber MUST be a global variable.
player.sendEvent('STOP');player.sendEvent('LOAD', 'playlist.xml');
setTimeout("player.sendEvent('ITEM', trackNumber), 500);
The more sophisticated, robust way to do this, is to use a playlist Listener, which will report when the playlist has been loaded and indexed. Then you can select the particular track.
<script type="text/javascript">var player = null;
var playlist = null;
var currentTrack = null;
function playerReady(obj)
{
player = gid(obj.id);
addListeners();
};
function addListeners()
{
playlist = player.getPlaylist();
if((playlist !== null) && (playlist.length > 0))
{
player.addControllerListener('PLAYLIST', 'playlistMonitor');
}
else
{
alert('Try Again!');
setTimeout("addListeners();", 100);
}
};
function playlistMonitor(obj)
{
player.sendEvent('ITEM', currentTrack);
};
function loadNindex(playlist, track)
{
currentTrack = track;
player.sendEvent('STOP');
player.sendEvent('LOAD', playlist);
};
function gid(name)
{
return document.getElementById(name);
};
</script>
Where your anchor element would look like this:
<a href="#" onclick="loadNindex('http://www.domain.com/path/playlist4.xml', 23); return false;">LOAD Playlist 4, Track 22</a>