Skip to content

Commit

Permalink
Pre-load next song (close #179 & #114)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiix committed Dec 17, 2016
1 parent 8d14904 commit 8002180
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/Controller/SongsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public function download($id = null) {
}

$this->response->file($song['Song']['path'], array('download' => true));
$this->response->cache('-1 minute', '+2 hours');
return $this->response;
}

Expand Down
21 changes: 21 additions & 0 deletions app/webroot/js/Player.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function Player() {

var audioElement = document.createElement('audio');
var bufferAudioElement = document.createElement('audio');
var buffered = false;
var playlist = new Playlist();
var self = this;
var selected = null;
Expand All @@ -21,6 +23,20 @@ function Player() {
this.addEventListener = function(event, callback) {
audioElement.addEventListener(event, callback, true);
};
this.addEventListener('progress', function() {
setTimeout(function() {
if(self.getBuffered() == self.getDuration() && self.hasNext() && !buffered) {
var index = playlist.getCurrentIndex()+1;
if (index == playlist.size()) {
index = 0;
}
bufferAudioElement.src = playlist.getByIndex(index).url;
bufferAudioElement.play();
bufferAudioElement.pause();
buffered = true;
}
}, 500);
});
this.getPlaylist = function() {
return playlist.getSongs();
};
Expand All @@ -45,6 +61,10 @@ function Player() {
this.seek = function(currentTime) {
if(currentTime <= audioElement.duration){
audioElement.currentTime = currentTime;
if (this.isPlaying()) {
audioElement.pause();
audioElement.play();
}
}
};
this.canPlay = function() {
Expand All @@ -63,6 +83,7 @@ function Player() {
}else if(audioElement.src == "" && selected != null) {
audioElement.src = selected.url;
}
buffered = false;
if(audioElement.src != "") {
audioElement.play();
}
Expand Down
4 changes: 3 additions & 1 deletion app/webroot/js/player-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ function init() {
});

player.addEventListener('progress', function() {
$timeBar.slider('buffered', player.getBuffered());
setTimeout(function() {
$timeBar.slider('buffered', player.getBuffered());
}, 500);
});

player.addEventListener('volumechange', function() {
Expand Down

0 comments on commit 8002180

Please sign in to comment.