Permalink
Browse files

added validatePlayFunction which if set will be called and must retur…

…n true to play a song. This way we can check cordova connection status outside of the playQueue lib. Also changed how we look for errors on timeout. if canplay was called, its not an error.
  • Loading branch information...
1 parent 0f7a0a7 commit 5d9e4d7af9b45faf0f607d958f3741cf681d175c @dankantor dankantor committed Jan 10, 2013
Showing with 87 additions and 67 deletions.
  1. +87 −67 index.js
View
154 index.js
@@ -65,6 +65,15 @@ function PlayQueue(opts){
// trying to load a song
this.checkOnlineStatus = false;
+ // if not null, this func must return true
+ // to play a song
+ this.validatePlayFunction = null;
+
+ // should we first check the connection type
+ // before setting the timeout time?
+ // for Cordova only
+ this.checkConnection = false;
+
// Arrays for event listeners
this.listeners = {
'nextTrack' : [],
@@ -466,84 +475,94 @@ PlayQueue.prototype.updateListPositions = function(n){
// play a song at a given index
PlayQueue.prototype.play = function(n){
- var shouldLoad = true;
- if(this.checkOnlineStatus === true){
- if(navigator.onLine === false){
- shouldLoad = false;
- if(this.getList()[n]){
- var checkSong = this.getList()[n];
- if(checkSong.offline && checkSong.offline === true){
- shouldLoad = true;
+ this.canPlayCalled = false;
+ var shouldPlay = true;
+ if(this.validatePlayFunction !== null){
+ shouldPlay = this.validatePlayFunction();
+ }
+ if(shouldPlay === true){
+ var shouldLoad = true;
+ if(this.checkOnlineStatus === true){
+ if(navigator.onLine === false){
+ shouldLoad = false;
+ if(this.getList()[n]){
+ var checkSong = this.getList()[n];
+ if(checkSong.offline && checkSong.offline === true){
+ shouldLoad = true;
+ }
}
}
}
- }
- if(shouldLoad === true){
- if(this.getList()[n]){
- clearTimeout(this.loadTimeout);
- this.isStopped = false;
- this.song_half_notified = false;
- this.before_end_notified = false;
- this.queueNumber = n;
- var song = this.getSong();
- var url = song.url;
- if(this.soundcloud_key != null){
- if(url.indexOf("soundcloud.com") != -1){
- if (url.indexOf("?") == -1){
- url = url+"?consumer_key="+this.soundcloud_key;
- }
- else{
- url = url+"&consumer_key="+this.soundcloud_key;
+ if(shouldLoad === true){
+ if(this.getList()[n]){
+ clearTimeout(this.loadTimeout);
+ this.isStopped = false;
+ this.song_half_notified = false;
+ this.before_end_notified = false;
+ this.queueNumber = n;
+ var song = this.getSong();
+ var url = song.url;
+ if(this.soundcloud_key != null){
+ if(url.indexOf("soundcloud.com") != -1){
+ if (url.indexOf("?") == -1){
+ url = url+"?consumer_key="+this.soundcloud_key;
+ }
+ else{
+ url = url+"&consumer_key="+this.soundcloud_key;
+ }
}
}
- }
- this.audio.src = url;
- this.audio.load();
- this.dispatchEvent("loading",
- {
- 'song': song,
- 'queueNumber': this.queueNumber,
- 'audio': this.getAudioProperties()
- }
- );
- if(this.load_timeout != -1){
- this.loadTimeout = setTimeout(
- this.timeoutLoading.bind(this),
- this.load_timeout
- );
- }
- if(this.use_local_storage == true){
- localStorage.setItem(this.localStorageNS+"queueNumber", this.queueNumber);
- }
- if(this.length_cap != -1){
- if(this.getList().length > this.length_cap){
- var cutNumber = this.getList().length - this.length_cap;
- if(this.queueNumber < cutNumber){
- cutNumber = this.queueNumber - 1;
+ this.audio.src = url;
+ this.audio.load();
+ this.dispatchEvent("loading",
+ {
+ 'song': song,
+ 'queueNumber': this.queueNumber,
+ 'audio': this.getAudioProperties()
}
- this.queueNumber -= cutNumber;
- var currentListLen = this.getList().length;
- var removed = this.getList().splice(0, cutNumber);
- this.updateListPositions(0);
- this.dispatchListChanged(
- this.getList(),
- this.queueNumber,
- [],
- removed,
- null,
- currentListLen,
- this.getList().length
+ );
+ if(this.load_timeout != -1){
+ this.loadTimeout = setTimeout(
+ this.timeoutLoading.bind(this),
+ this.load_timeout
);
}
+ if(this.use_local_storage == true){
+ localStorage.setItem(this.localStorageNS+"queueNumber", this.queueNumber);
+ }
+ if(this.length_cap != -1){
+ if(this.getList().length > this.length_cap){
+ var cutNumber = this.getList().length - this.length_cap;
+ if(this.queueNumber < cutNumber){
+ cutNumber = this.queueNumber - 1;
+ }
+ this.queueNumber -= cutNumber;
+ var currentListLen = this.getList().length;
+ var removed = this.getList().splice(0, cutNumber);
+ this.updateListPositions(0);
+ this.dispatchListChanged(
+ this.getList(),
+ this.queueNumber,
+ [],
+ removed,
+ null,
+ currentListLen,
+ this.getList().length
+ );
+ }
+ }
+ }
+ else {
+ throw new TypeError("Index out of bounds. Got: "
+ +n+" Length: "+this.getList().length);
}
- }
- else {
- throw new TypeError("Index out of bounds. Got: "
- +n+" Length: "+this.getList().length);
+ }
+ else{
+ this.dispatchEvent('offline');
}
}
else{
- this.dispatchEvent('offline');
+ this.dispatchEvent('validatePlayFunctionFalse');
}
}
@@ -568,6 +587,7 @@ PlayQueue.prototype.playPause = function(){
// Fires 'playing' event when 'canplay' audio event is fired.
// Adds some useful data
PlayQueue.prototype.canPlay = function(){
+ this.canPlayCalled = true;
this.audio.play();
this.dispatchEvent("playing",
{
@@ -627,7 +647,7 @@ PlayQueue.prototype.timeUpdate = function(){
// This is called when loadTimeout is reached
// If song has not started, next is called
PlayQueue.prototype.timeoutLoading = function(){
- if(this.audio.currentTime < 1){
+ if(this.canPlayCalled === false){
this.error();
}
}

0 comments on commit 5d9e4d7

Please sign in to comment.