Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer songs preemtively to avoid waiting on song change #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion app/webroot/js/Player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function Player(ajax_url){
var playlist = new Playlist(ajax_url);
var audioElement = document.createElement('audio');
var audioElementBuffer = document.createElement('audio');
var hasBufferedNext=false;
var selected = null;
var self = this;
var loop = false;
Expand Down Expand Up @@ -36,6 +38,14 @@ function Player(ajax_url){
this.getCurrentTime = function() {
return audioElement.currentTime;
};
this.bufferNext = function() {
if(playlist.hasNext(loop) && !hasBufferedNext) {
hasBufferedNext=true;
audioElementBuffer.src=playlist.getNext(loop).url;
audioElementBuffer.play();
audioElementBuffer.pause(); //Force cache
}
};
this.getBuffered = function() {
if(audioElement.buffered.length){
return audioElement.buffered.end(audioElement.buffered.length-1);
Expand Down Expand Up @@ -134,6 +144,7 @@ function Player(ajax_url){
audioElement.src = selected.url;
}
audioElement.play();
hasBufferedNext=false;
};
this.playIndex = function(index) {
selected = playlist.get(index, "index");
Expand Down Expand Up @@ -374,6 +385,20 @@ function Playlist(ajax_url){
}
return songs[(index-1)] !== undefined;
};
this.getNext = function(loop) {
if(this.hasNext(loop)){
var s = random ? shuffleSongs : songs;
var aux;
if(loop){
aux= (s[(index+1)] !== undefined ? index+1 : 0);
}else{
aux=index+1;
}
return s[aux];
}
return null;
};

this.next = function(loop) {
if(this.hasNext(loop)){
var s = random ? shuffleSongs : songs;
Expand Down Expand Up @@ -598,4 +623,4 @@ function Playlist(ajax_url){
return index;
};
this.onChange = function(){};
}
}
5 changes: 4 additions & 1 deletion app/webroot/js/player-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ $(function(){
player.addEventListener('timeupdate', function(){
$('#timebar').slider('value', player.getCurrentTime());
$('.currentTime').text(getFormatedTime(player.getCurrentTime()));
var timeLeft = player.getDuration() - player.getCurrentTime();
if (timeLeft < 15)
player.bufferNext();
});
player.addEventListener('progress', function(){
$('#timebar').slider('buffered', player.getBuffered());
Expand Down Expand Up @@ -395,4 +398,4 @@ $(function(){
player.shuffle(true);
}

});
});