169 lines
5.4 KiB
JavaScript
169 lines
5.4 KiB
JavaScript
var audio;
|
|
var prev_audio;
|
|
var next_audio;
|
|
var utid;
|
|
$(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;});
|
|
}
|
|
});
|
|
});
|
|
$('#player .play').click(function(){
|
|
if (! audio) return;
|
|
audio.play();
|
|
utid = window.setTimeout(updatePlayer, 100);
|
|
$('#player .pause').show();
|
|
$('#player .play').hide();
|
|
}).disableTextSelect();
|
|
$('#player .pause').click(function(){
|
|
audio.pause();
|
|
clearTimeout(utid);
|
|
$('#player .pause').hide();
|
|
$('#player .play').show();
|
|
}).disableTextSelect();
|
|
$('.tracks .play').click(function(){
|
|
addToPlaylist(
|
|
$('h1.artist > span > span:first').html(),
|
|
$(this).parent().find('.track-name').html(),
|
|
Math.round($(this).parent().find('.duration').attr('data-length') / 1000),
|
|
$(this).parent().attr('id')
|
|
);
|
|
})
|
|
$('.album > h3.name > span.play').click(function(){
|
|
$(this).parent().parent().find('ul > li > span.play').click();
|
|
})
|
|
$('h1.artist > span > span.play').click(function(){
|
|
$('.album > ul > li > span.play').click();
|
|
})
|
|
$('#player .prev').click(function(){
|
|
playPrev();
|
|
}).disableTextSelect();
|
|
$('#player .next').click(function(){
|
|
playNext();
|
|
}).disableTextSelect();
|
|
$('#player .shuffle, #player .repeat').click(function(){
|
|
$(this).toggleClass('on');
|
|
setNext();
|
|
}).disableTextSelect();
|
|
$('#player .playlist').click(function(){
|
|
$('#playlist').toggle();
|
|
//$('#playlist').data('jsp').reinitialise();
|
|
})
|
|
$('#player .progress-bar').click(function(e){
|
|
if (typeof(audio) !== 'undefined' && typeof(audio.duration) !== 'undefined') {
|
|
desired_progress = Math.abs(e.pageX - $(this).offset().left) / $('#player .progress-bar').innerWidth();
|
|
audio.currentTime = Math.round(audio.duration * desired_progress);
|
|
}
|
|
})
|
|
$('#playlist').hide();
|
|
})
|
|
function updatePlayer() {
|
|
duration = audio.duration;
|
|
cur_time = audio.currentTime;
|
|
if (cur_time > 0 && cur_time == duration) {
|
|
playNext();
|
|
}
|
|
loaded = 0;
|
|
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
|
|
loaded = Math.round((audio.buffered.end(0) / audio.duration) * 390);
|
|
$('#player .time-played').html(formatTime(cur_time));
|
|
$('#player .time-left').html(formatTime(duration - cur_time));
|
|
progress = Math.round((cur_time / duration) * 390);
|
|
$('#player .progress-loaded').css('width', loaded +'px')
|
|
$('#player .progress-point').css('margin-left', progress - $('#player .progress-point').width() +'px')
|
|
|
|
/* Starting buffering next track */
|
|
if (Math.round(cur_time / duration * 100) > 10) {
|
|
addAudio(next_audio);
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
function playTrack(artist, track, id) {
|
|
switchAudio(id);
|
|
setNext();
|
|
$('#player .track-title').html(artist +' — '+ track);
|
|
$('#player .time-played').html('0:00');
|
|
$('#player .time-left').html('0:00');
|
|
$('#player .progress-loaded').css('width', '0px')
|
|
$('#player .progress-point').css('margin-left', '-6px')
|
|
$('#player .play').trigger('click');
|
|
}
|
|
function setPrev() {
|
|
if ($('#playlist ul.list li').length == 0) return false;
|
|
if (!audio) {
|
|
prev_audio = $(audio).attr('data-id');
|
|
}
|
|
}
|
|
function setNext() {
|
|
if ($('#playlist ul.list li').length == 0) return false;
|
|
if ($('#player .shuffle').hasClass('on')) {
|
|
next_audio = $('#playlist ul.list li').rand().attr('data-id');
|
|
}
|
|
if ($('#playlist ul.list li.now-playing').next().length == 0) {
|
|
if ($('#player .repeat').hasClass('on')) {
|
|
next_audio = $('#playlist ul.list li:first').attr('data-id');
|
|
}
|
|
} else {
|
|
next_audio = $('#playlist ul.list li.now-playing').next().attr('data-id');
|
|
}
|
|
}
|
|
function playPrev() {
|
|
return false;
|
|
}
|
|
function playNext() {
|
|
$('#playlist ul.list li[data-id="'+ next_audio +'"]').dblclick();
|
|
}
|
|
function addToPlaylist(artist, track, length, id) {
|
|
$('#playlist ul.list').append($(
|
|
'<li data-id="'+ id +'" title="Double-click To Play">'+
|
|
'<span class="artist">'+ artist +'</span> — '+
|
|
'<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(){
|
|
playTrack(artist, track, id);
|
|
$('#playlist li').removeClass('now-playing');
|
|
$(this).addClass('now-playing');
|
|
setNext();
|
|
})
|
|
if ($('#playlist ul.list li').length == 1) {
|
|
$('#playlist ul.list li:first').dblclick();
|
|
}
|
|
if ($('#playlist ul.list li').length > 6) {
|
|
$('#playlist').jScrollPane();
|
|
}
|
|
$('#player .playlist .count').html($('#playlist ul.list li').length);
|
|
setNext();
|
|
}
|
|
function addAudio(id) {
|
|
if ($('#audio_'+ id).length == 0) {
|
|
$('#audiobox').append('<audio id="audio_'+ id +'" src="" preload="preload"></audio>');
|
|
$('#audio_'+ id).attr('src', '/listen/'+ id +'.mp3');
|
|
}
|
|
}
|
|
function switchAudio(id) {
|
|
if (typeof(audio) != 'undefined' && audio.buffered.length != 0) {
|
|
console.log(audio, audio.buffered.length);
|
|
audio.pause();
|
|
audio.currentTime = 0;
|
|
}
|
|
if ($('#audio_'+ id).length == 0) {
|
|
addAudio(id);
|
|
}
|
|
audio = document.getElementById('audio_'+ id);
|
|
} |