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

82 lines
2.5 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(){
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();
2011-04-08 18:39:49 +00:00
})
2011-04-10 18:04:35 +00:00
$('#player .pause').click(function(){
audio.pause();
clearTimeout(utid);
$('#player .pause').hide();
$('#player .play').show();
})
$('.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')
);
})
$('#playlist .show-button').click(function(){
$('#playlist ul').toggle();
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) {
playNext();
}
2011-04-10 18:04:35 +00:00
loaded = 0;
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
2011-04-11 04:16:00 +00:00
loaded = Math.round((audio.buffered.end(0) / audio.duration) * 730);
$('#player .time-played').html(formatTime(cur_time));
$('#player .time-left').html(formatTime(duration - cur_time));
progress = Math.round((cur_time / duration) * 730);
$('#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) {
$(audio).attr('src', '/listen/'+ id +'/');
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')
2011-04-11 04:16:00 +00:00
$('#playlist li').removeClass('now-playing');
$('#playlist li[data-id="'+ id +'"]').addClass('now-playing');
2011-04-10 18:04:35 +00:00
$('#player .play').trigger('click');
2011-04-11 04:16:00 +00:00
}
function playNext() {
$('#playlist ul li.now-playing').next().trigger('click');
}
function addToPlaylist(artist, track, length, id) {
$('#playlist ul').append($(
'<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 li[data-id="'+ id +'"]').click(function(){
playTrack(artist, track, id);
})
if ($('#playlist ul li').length == 1) {
playTrack(artist, track, id);
}
2011-04-10 18:04:35 +00:00
}