Created
December 18, 2009 15:35
-
-
Save nzoschke/259569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
player = null; // niftyplayer instance | |
files = []; // playlist order; could be shuffled | |
_files = []; // original order | |
retries = 0; // how many times javascript tried to find player | |
int = null; // setInterval handle | |
// parse all .mp3 links on the page | |
as = document.getElementsByTagName('a'); | |
for (var i = 0; i < as.length; i++) { | |
a = as[i]; | |
if (!a.href) continue; | |
if (!a.href.match('.mp3$')) continue; | |
files.push(a.href); | |
_files.push(a.href); | |
// insert play buttons before all mp3s | |
var button = document.createElement('link'); | |
button.id = 'button' + i; | |
button.className = 'button stopped'; | |
button.href = a.href; | |
// hook up button to player | |
button.addEventListener('click', function(e) { | |
e.preventDefault(); // cancel event | |
changeState(e.target.href); | |
return false; | |
}, false); | |
a.parentNode.insertBefore(button, a); | |
} | |
// embed the player | |
if (files.length) { | |
document.write('<script type="text/javascript" src="/niftyplayer/niftyplayer.js"></scr' + 'ipt>'); | |
foot = document.getElementsByClassName('foot')[0]; | |
foot.innerHTML += '<link rel="stylesheet" type="text/css" href="/niftyplayer/style.css" /> \ | |
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="37" id="player" align=""> \ | |
<param name=movie value="/niftyplayer/niftyplayer.swf"> \ | |
<param name=quality value=high> \ | |
<param name=bgcolor value=#FFFFFF> \ | |
<embed src="/niftyplayer/niftyplayer.swf" quality=high bgcolor=#FFFFFF width="165" height="37" name="player" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"> \ | |
</embed> \ | |
</object> \ | |
<div id="controls"> \ | |
<a id="prev" class="control" href="#">prev</a> \ | |
<a id="next" class="control" href="#">next</a> \ | |
<a id="shuffle" class="control" href="#">shuffle</a> \ | |
<br /><a id="title" class="control"><i>stopped</i></a> \ | |
</div>'; | |
int = setInterval("setupPlayer()", 500); | |
} | |
// get the player instance once its loaded | |
function setupPlayer() { | |
player = niftyplayer('player'); | |
if (!player) return; | |
clearInterval(int); | |
player.registerEvent('onBufferingStarted', 'changeState()'); | |
player.registerEvent('onPlay', 'changeState()'); | |
player.registerEvent('onPause', 'changeState()'); | |
player.registerEvent('onStop', 'changeState()'); | |
player.registerEvent('onSongOver', 'changeState()'); | |
document.getElementById('controls').style.display = 'block'; | |
document.getElementById('prev').addEventListener('click', playPrevNext, true); | |
document.getElementById('next').addEventListener('click', playPrevNext, true); | |
document.getElementById('shuffle').addEventListener('click', setShuffle, true); | |
} | |
function playPrevNext(e) { | |
e.preventDefault(); // cancel event | |
currentSong = player.obj.GetVariable('currentSong'); | |
if (!currentSong) return; | |
for (var i = 0; i < files.length; i++) // find index of current song | |
if (currentSong == files[i]) break; | |
i += (e.target.id == 'prev') ? -1 : 1; // increment or decrement i | |
if (i < 0) | |
i = files.length - 1; | |
if (i == files.length) | |
i = 0; | |
player.loadAndPlay(files[i]); | |
} | |
function setShuffle(e) { | |
e.preventDefault(); // cancel event | |
if (e.target.className == 'control on') { // turn off shuffle | |
e.target.className = 'control'; | |
files = _files; | |
return; | |
} | |
// shuffle `files` in place | |
for(var j, x, i = files.length; i; j = parseInt(Math.random() * i), x = files[--i], files[i] = files[j], files[j] = x); | |
e.target.className = 'control on'; | |
} | |
function changeState(file) { | |
state = player.getState(); | |
currentSong = player.obj.GetVariable('currentSong'); | |
////////// | |
// button state changes | |
if (file) { | |
if (file != currentSong) | |
player.loadAndPlay(file); | |
else | |
player.playToggle(); | |
return; | |
} | |
////////// | |
// internal player state changes | |
if (state == 'empty') return; | |
if (currentSong == "") | |
player.loadAndPlay(files[0]); | |
// advance to next song | |
if (state == 'finished') { | |
for (var i = 0; i < files.length; i++) // find index of current song | |
if (currentSong == files[i]) break; | |
if (i < files.length) | |
player.loadAndPlay(files[i + 1]) | |
}; | |
////////// | |
// update icons | |
buttons = document.getElementsByClassName('button'); | |
title = "<i>stopped</i>"; | |
for (var i = 0; i < buttons.length; i++) { | |
button = buttons[i]; | |
button.className = 'button paused'; | |
if (((state == 'playing') || (state == 'loading')) && (currentSong == button.href)) { | |
button.className = 'button playing'; | |
title = button.nextSibling.text; | |
} | |
} | |
document.getElementById('title').innerHTML = title; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.button { | |
display: block; | |
float: left; | |
width: 11px; | |
height: 11px; | |
background-image: url('/niftyplayer/playpause.png'); | |
margin-right: 3px; | |
} | |
.button.playing { | |
background-position: 0 11px; | |
} | |
#controls { | |
display: none; | |
} | |
a.control { | |
color: #777766; | |
padding: 0 3px; | |
margin-right: -3px; | |
line-height: 1.5; | |
text-decoration: none; | |
background-color: #F0F0DD; | |
border: 1px solid #AAAA99; | |
} | |
a.control.on { | |
color: #000000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment