1
0
Fork 0

Progress bar click moves track position

This commit is contained in:
magnolia-fan 2011-04-15 00:24:24 +04:00
parent be7f9000f3
commit e5237b5fab
2 changed files with 33 additions and 13 deletions

View File

@ -46,7 +46,7 @@ $(function(){
playPrev(); playPrev();
}).disableTextSelect(); }).disableTextSelect();
$('#player .next').click(function(){ $('#player .next').click(function(){
playNext(false); playNext();
}).disableTextSelect(); }).disableTextSelect();
$('#player .shuffle, #player .repeat').click(function(){ $('#player .shuffle, #player .repeat').click(function(){
$(this).toggleClass('on'); $(this).toggleClass('on');
@ -55,13 +55,19 @@ $(function(){
$('#playlist').toggle(); $('#playlist').toggle();
//$('#playlist').data('jsp').reinitialise(); //$('#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(); $('#playlist').hide();
}) })
function updatePlayer() { function updatePlayer() {
duration = audio.duration; duration = audio.duration;
cur_time = audio.currentTime; cur_time = audio.currentTime;
if (cur_time == duration) { if (cur_time == duration) {
playNext(true); playNext();
} }
loaded = 0; loaded = 0;
if ((audio.buffered != undefined) && (audio.buffered.length != 0)) { if ((audio.buffered != undefined) && (audio.buffered.length != 0)) {
@ -73,7 +79,7 @@ function updatePlayer() {
$('#player .progress-point').css('margin-left', progress +'px') $('#player .progress-point').css('margin-left', progress +'px')
/* Starting buffering next track */ /* Starting buffering next track */
if (Math.round(cur_time / duration * 100) > 70) { if (Math.round(cur_time / duration * 100) > 10) {
addAudio(next_audio); addAudio(next_audio);
} }
} }
@ -87,6 +93,7 @@ function formatTime(sec) {
} }
function playTrack(artist, track, id) { function playTrack(artist, track, id) {
switchAudio(id); switchAudio(id);
setNext();
$('#player .track-title').html(artist +' — '+ track); $('#player .track-title').html(artist +' — '+ track);
$('#player .time-played').html('0:00'); $('#player .time-played').html('0:00');
$('#player .time-left').html('0:00'); $('#player .time-left').html('0:00');
@ -94,27 +101,31 @@ function playTrack(artist, track, id) {
$('#player .progress-point').css('margin-left', 0 +'px') $('#player .progress-point').css('margin-left', 0 +'px')
$('#player .play').trigger('click'); $('#player .play').trigger('click');
} }
function playPrev() { function setPrev() {
if ($('#playlist ul.list li').length == 0) return false; if ($('#playlist ul.list li').length == 0) return false;
if ($('#playlist ul.list li.now-playing').prev().length == 0) { if (!audio) {
$('#playlist ul.list li:last').dblclick(); prev_audio = $(audio).attr('data-id');
} else {
$('#playlist ul.list li.now-playing').prev().dblclick();
} }
} }
function playNext(auto) { function setNext() {
if ($('#playlist ul.list li').length == 0) return false; if ($('#playlist ul.list li').length == 0) return false;
if ($('#player .shuffle').hasClass('on')) { if ($('#player .shuffle').hasClass('on')) {
$('#playlist ul.list li').rand().dblclick(); next_audio = $('#playlist ul.list li').rand().attr('data-id');
} }
if ($('#playlist ul.list li.now-playing').next().length == 0) { if ($('#playlist ul.list li.now-playing').next().length == 0) {
if ((auto && $('#player .repeat').hasClass('on')) || !auto) { if ($('#player .repeat').hasClass('on')) {
$('#playlist ul.list li:first').dblclick(); next_audio = $('#playlist ul.list li:first').attr('data-id');
} }
} else { } else {
$('#playlist ul.list li.now-playing').next().dblclick(); 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) { function addToPlaylist(artist, track, length, id) {
$('#playlist ul.list').append($( $('#playlist ul.list').append($(
'<li data-id="'+ id +'" title="Double-click To Play">'+ '<li data-id="'+ id +'" title="Double-click To Play">'+
@ -136,6 +147,7 @@ function addToPlaylist(artist, track, length, id) {
$('#playlist').jScrollPane(); $('#playlist').jScrollPane();
} }
$('#player .playlist .count').html($('#playlist ul.list li').length); $('#player .playlist .count').html($('#playlist ul.list li').length);
setNext();
} }
function addAudio(id) { function addAudio(id) {
if ($('#audio_'+ id).length == 0) { if ($('#audio_'+ id).length == 0) {
@ -143,6 +155,10 @@ function addAudio(id) {
} }
} }
function switchAudio(id) { function switchAudio(id) {
if (audio) {
audio.pause();
audio.currentTime = 0;
}
if ($('#audio_'+ id).length == 0) { if ($('#audio_'+ id).length == 0) {
addAudio(id); addAudio(id);
} }

View File

@ -90,6 +90,9 @@ audio {
width: 390px; width: 390px;
height: 3px; height: 3px;
} }
#player .progress-bar:hover {
cursor: pointer;
}
#player .progress-loaded { #player .progress-loaded {
float: left; float: left;
width: 0; width: 0;
@ -185,6 +188,7 @@ audio {
} }
#playlist ul li.now-playing { #playlist ul li.now-playing {
background-color: #013; background-color: #013;
font-weight: bold;
} }
#playlist ul li .length { #playlist ul li .length {
margin: 0 0.5em; margin: 0 0.5em;