1
0
Fork 0
oldhaven/public/javascripts/player.js

127 lines
4.0 KiB
JavaScript
Raw Normal View History

2011-04-10 18:04:35 +00:00
var audio;
var utid;
2011-04-08 18:39:49 +00:00
$(function(){
$.extend($.fn.disableTextSelect = function() {
return this.each(function(){
if ($.browser.mozilla) {//Firefox
$(this).css('MozUserSelect','none');
} else if ($.browser.msie) {//IE
$(this).bind('selectstart',function(){return false;});
} else {//Opera, etc.
$(this).mousedown(function(){return false;});
}
});
});
2011-04-10 18:04:35 +00:00
audio = document.getElementsByTagName('audio')[0];
$('#player .play').click(function(){
2011-04-11 04:16:00 +00:00
if (! audio.src) return;
2011-04-10 18:04:35 +00:00
audio.play();
utid = window.setTimeout(updatePlayer, 100);
$('#player .pause').show();
$('#player .play').hide();
}).disableTextSelect();
2011-04-10 18:04:35 +00:00
$('#player .pause').click(function(){
audio.pause();
clearTimeout(utid);
$('#player .pause').hide();
$('#player .play').show();
}).disableTextSelect();
2011-04-10 18:04:35 +00:00
$('.tracks .play').click(function(){
2011-04-11 04:16:00 +00:00
addToPlaylist(
$('h1.artist').html(),
$(this).parent().find('.track-name').html(),
Math.round($(this).parent().find('.duration').attr('data-length') / 1000),
$(this).parent().attr('id')
);
})
$('#player .prev').click(function(){
playPrev();
}).disableTextSelect();
$('#player .next').click(function(){
2011-04-12 21:28:46 +00:00
playNext(false);
}).disableTextSelect();
$('#player .shuffle, #player .repeat').click(function(){
$(this).toggleClass('on');
}).disableTextSelect();
$('#player .playlist').click(function(){
$('#playlist').toggle();
//$('#playlist').data('jsp').reinitialise();
2011-04-10 18:04:35 +00:00
})
$('#playlist').hide();
2011-04-10 18:04:35 +00:00
})
function updatePlayer() {
duration = audio.duration;
cur_time = audio.currentTime;
2011-04-11 04:16:00 +00:00
if (cur_time == duration) {
2011-04-12 21:28:46 +00:00
playNext(true);
2011-04-11 04:16:00 +00:00
}
2011-04-10 18:04:35 +00:00
loaded = 0;
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
loaded = Math.round((audio.buffered.end(0) / audio.duration) * 390);
2011-04-11 04:16:00 +00:00
$('#player .time-played').html(formatTime(cur_time));
$('#player .time-left').html(formatTime(duration - cur_time));
progress = Math.round((cur_time / duration) * 390);
2011-04-11 04:16:00 +00:00
$('#player .progress-loaded').css('width', loaded +'px')
$('#player .progress-point').css('margin-left', progress +'px')
2011-04-10 18:04:35 +00:00
}
utid = window.setTimeout(updatePlayer, 100);
}
function formatTime(sec) {
sec = Math.round(sec);
m = Math.floor(sec / 60);
s = sec - (m * 60);
return m +':'+ (s < 10 ? '0' : '') +s;
}
2011-04-11 04:16:00 +00:00
function playTrack(artist, track, id) {
2011-04-11 16:49:25 +00:00
$(audio).attr('src', '/listen/'+ id +'.mp3');
2011-04-10 18:04:35 +00:00
$('#player .track-title').html(artist +' &mdash; '+ track);
$('#player .time-played').html('0:00');
$('#player .time-left').html('0:00');
$('#player .progress-loaded').css('width', 0 +'px')
$('#player .progress-point').css('margin-left', 0 +'px')
$('#player .play').trigger('click');
2011-04-11 04:16:00 +00:00
}
function playPrev() {
if ($('#playlist ul.list li').length == 0) return false;
if ($('#playlist ul.list li.now-playing').prev().length == 0) {
$('#playlist ul.list li:last').dblclick();
} else {
$('#playlist ul.list li.now-playing').prev().dblclick();
}
}
function playNext(auto) {
if ($('#playlist ul.list li').length == 0) return false;
if ($('#player .shuffle').hasClass('on')) {
$('#playlist ul.list li').rand().dblclick();
}
if ($('#playlist ul.list li.now-playing').next().length == 0) {
2011-04-12 21:28:46 +00:00
if ((auto && $('#player .repeat').hasClass('on')) || !auto) {
$('#playlist ul.list li:first').dblclick();
}
} else {
$('#playlist ul.list li.now-playing').next().dblclick();
}
2011-04-11 04:16:00 +00:00
}
function addToPlaylist(artist, track, length, id) {
$('#playlist ul.list').append($(
2011-04-11 04:16:00 +00:00
'<li data-id="'+ id +'">'+
'<span class="artist">'+ artist +'</span> &mdash; '+
'<span class="track">'+ track +'</span>'+
'<span class="length" data-seconds="'+ length +'">'+ formatTime(length) +'</span>'+
'</li>'
));
$('#playlist ul.list li').disableTextSelect();
$('#playlist ul.list li[data-id="'+ id +'"]').dblclick(function(){
2011-04-11 04:16:00 +00:00
playTrack(artist, track, id);
$('#playlist li').removeClass('now-playing');
$(this).addClass('now-playing');
2011-04-11 04:16:00 +00:00
})
if ($('#playlist ul.list li').length == 1) {
$('#playlist ul.list li:first').dblclick();
2011-04-11 04:16:00 +00:00
}
if ($('#playlist ul.list li').length > 6) {
$('#playlist').jScrollPane();
}
2011-04-11 16:49:25 +00:00
}