JW Player, Play playlist item by filename, rather than index

Problem:

I have a playlist of 45 items that will be played via javascript throughout one page (it’s a glossary page with audio files).  I found myself repeatedly making mistakes on which index belongs to what word.  As it stands, you would have to remember that the 40th item belongs to a specific word and play it by using jwplayer(‘playerID’).playlistItem(39).  What happens if I insert an item on the playlist?  I either have to put it at the end, putting things out of alphabetical order, or have to go back and change all the index references in the page.

Solution:

When the page is loaded, I go through the entire playlist once and create a key/value object with the filename and index.  Now I can play the items by name.  Instead of jwplayer(‘playerID’).playlistItem(39), it will be a much more readable playItemByName(‘the40thitem.mp3’).  This will be MUCH more easier to debug and maintain.

This could easily be modified to play using the title or description instead.

The code:

// Go through the playlist and create a key/value pair array so that we can assign playlist index numbers (since jw player can only reference playlist items by index, not name)
var playlistIndices = new Array();
$(document).ready(function() {
var i = 0;
for (i=0; i<glossaryPlaylistItems.length; i++) {
var path = glossaryPlaylistItems[i].file.split(‘/’); // split up the path by ‘/’ for next step
path = path[path.length-1]; // get the filename, lose the folder reference part
playlistIndices[path]=i;
}
});

function playItemByName(itemName) {
jwplayer(‘glossaryPlaylist’).playlistItem(playlistIndices[itemName]);
}


Posted

in

by

Tags: