var AudioPlayer = (function ($) { var instance = 0; function playsMP3() { var a = document.createElement('audio'); return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, '')); } function playsOgg() { var a = document.createElement('audio'); return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')); } function maybe_decode(file) { var fileName, path, parts = []; if (file.indexOf('.mp3') === -1 || file.indexOf('.ogg') === -1) { if (file.indexOf('/') > -1) { parts = file.split('/'); fileName = Base64.decode(parts[parts.length - 1]); parts.pop(); parts.push(fileName); file = parts.join('/'); } else { file = Base64.decode(file); } } if (file.indexOf('http://') === -1) { file = 'http://' + window.location.host + decodeURIComponent(file); } return file; } function createInstance(indx) { return ['
', ''].join(''); } function pauseOthers(index) { var i = 0, aPlayer; if (instance > i) { for (i; i < instance; i += 1) { if (i !== index) { aPlayer = $('#jquery_jplayer' + i); if (aPlayer.jPlayer('getData', 'diag.isPlaying')) { aPlayer.jPlayer('pause'); } aPlayer = null; } } } } return function (wrapper) { var elem = $(wrapper), player, thisInstance = instance, songs, oggs, matches, doOgg = false, bNext, bPrev, bPlay, bPause, playItem = 0, playlist = [], oggFiles = 0, playTime, totalTime, nowPlaying, AUTOPLAY = false; function playlistInit() { if (AUTOPLAY) { playlistChange(playItem); } else { playlistConfig(playItem); } } function playlistConfig(index) { var track = playlist[index], meta; meta = ['“', track.song, '”', '' !== track.album ? (' from ' + track.album + ' ') : '', 'by ', track.artist].join(''); nowPlaying.html(meta); playItem = index; if (doOgg) { player.jPlayer('setFile', '', playlist[playItem].source); } else { player.jPlayer('setFile', playlist[playItem].source, ''); } } function playlistChange(index) { playlistConfig(index); player.jPlayer('play'); } function playlistNext() { var index = (playItem + 1 < playlist.length) ? playItem + 1 : 0; playlistChange(index); } function playlistPrev() { var index = (playItem - 1 >= 0) ? playItem - 1 : playlist.length - 1; playlistChange(index); } function doProgress(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, tTime) { playTime.text($.jPlayer.convertTime(playedTime)); totalTime.text($.jPlayer.convertTime(tTime)); } function doComplete() { playlistNext(); } function doPrev() { pauseOthers(thisInstance); playlistPrev(); $(this).blur(); return false; } function doNext() { pauseOthers(thisInstance); playlistNext(); $(this).blur(); return false; } function doPlay() { pauseOthers(thisInstance); } songs = elem.find('.haudio'); oggs = elem.find('a[type="audio/ogg"]'); matches = songs.length === oggs.length; doOgg = matches && playsOgg(); songs.each(function () { var elem = $(this), audioFile, mp3, ogg; ogg = elem.find('a[type="audio/ogg"]'); mp3 = elem.find('a[type="audio/mpeg"]'); if (playsMP3()) { audioFile = mp3.attr('href'); } else if (doOgg) { audioFile = ogg.attr('href'); } if (!audioFile) { audioFile = mp3.attr('href'); } playlist.push({ song : elem.find('.fn').eq(0).text(), album: elem.find('.album').text(), artist: elem.find('.org').text(), source: maybe_decode(audioFile) }); elem.click(function () { playlistChange(songs.index(this)); }); }); elem.prepend($(createInstance(instance))); // Local copy of jQuery selectors, for performance nowPlaying = elem.find('.now-playing'); playTime = elem.find('#jplayer_play_time' + instance); totalTime = elem.find('#jplayer_total_time' + instance); bNext = elem.find('.jp-next'); bPrev = elem.find('.jp-previous'); bPlay = elem.find('.jp-play'); bPause = elem.find('.jp-pause'); if (!player) { player = $('#jquery_jplayer' + instance); player .jPlayer({ customCssIds: true, swfPath: 'http://' + window.location.host + '/wp-content/plugins/audio/js/', preload: 'none', ready: function () { playlistInit(); }, errorAlerts: true, warningAlerts: true, // Safari 5 breaks, hopefully will fix in future release nativeSupport: !((Object.hasOwnProperty.call(navigator, 'vendor') && navigator.vendor.indexOf('Apple') > -1) && navigator.appVersion.indexOf('5.') > -1 && navigator.appVersion.indexOf('iPhone') === -1 ), oggSupport: doOgg }) .jPlayer('cssId', 'play', 'jplayer_play' + instance) .jPlayer('cssId', 'pause', 'jplayer_pause' + instance) .jPlayer('cssId', 'stop', 'jplayer_stop' + instance) .jPlayer('cssId', 'loadBar', 'jplayer_load_bar' + instance) .jPlayer('cssId', 'playBar', 'jplayer_play_bar' + instance) .jPlayer('cssId', 'volumeMin', 'jplayer_volume_min' + instance) .jPlayer('cssId', 'volumeMax', 'jplayer_volume_max' + instance) .jPlayer('cssId', 'volumeBar', 'jplayer_volume_bar' + instance) .jPlayer('cssId', 'volumeBarValue', 'jplayer_volume_bar_value' + instance) .jPlayer('onProgressChange', doProgress) .jPlayer('onSoundComplete', doComplete); } bPrev.click(doPrev); bNext.click(doNext); bPlay.click(doPlay); instance += 1; return { }; }; }(jQuery)); (function ($) { $(document).ready(function () { wrapper = $('.audio-playlist'); wrapper.each(function () { return new AudioPlayer(this); }); }); }(jQuery))