disablePreload:'='
//ngAudio:'='
},
- controller: function($scope, $attrs, $element, $timeout) {
-
+ controller: ['$scope', '$attrs', '$element', '$timeout', function($scope, $attrs, $element, $timeout) {
+
/* Loads the sound from destination */
var audio;
function initSound(){
- audio = ngAudio.load($attrs.ngAudio);
+ audio = ngAudio.load($attrs.ngAudio, $scope);
/* Add audio to local scope for modification with nested inputs */
$scope.$audio = audio;
/* Remove watching features for improved performance */
audio.unbind();
- }
+ }
if (!$scope.disablePreload){
initSound();
- }
-
+ }
+
$element.on('click', function() {
if ($scope.clickPlay === false) {
return;
}
-
+
if ($scope.disablePreload){
initSound();
- }
+ }
/* iOS workaround: Call the play method directly in listener function */
audio.audio.play();
-
+
/* Set volume to $scope volume if it exists, or default to audio's current value */
audio.volume = $scope.volume || audio.volume;
audio.loop = $scope.loop;
audio.play();
}, 5);
});
- }
+
+ $element.on('$destroy', function() {
+ audio.destroy();
+ });
+ }]
};
}])
.directive('ngAudioHover', ['$compile', '$q', 'ngAudio', function($compile, $q, ngAudio) {
return {
restrict: 'AEC',
- controller: function($scope, $attrs, $element, $timeout) {
+ controller: ['$scope', '$attrs', '$element', '$timeout', function($scope, $attrs, $element, $timeout) {
- var audio = ngAudio.load($attrs.ngAudioHover);
+ var audio = ngAudio.load($attrs.ngAudioHover, $scope);
$element.on('mouseover rollover hover', function() {
-
+
/* iOS workaround: Call the play method directly in listener function */
audio.audio.play();
-
+
audio.volume = $attrs.volumeHover || audio.volume;
audio.loop = $attrs.loop;
audio.currentTime = $attrs.startHover || 0;
});
- }
+
+ $element.on('$destroy', function() {
+ audio.destroy();
+ });
+ }]
};
}])
.value('ngAudioGlobals', {
muting: false,
- songmuting: false,
performance: 25,
- unlock: true
+ unlock: true,
+ volume:1
})
.factory('NgAudioObject', ['cleverAudioFindingService', '$rootScope', '$interval', '$timeout', 'ngAudioGlobals', function(cleverAudioFindingService, $rootScope, $interval, $timeout, ngAudioGlobals) {
- return function(id) {
-
- if (ngAudioGlobals.unlock) {
-
- window.addEventListener("click",function twiddle(){
- audio.play();
- audio.pause();
- window.removeEventListener("click",twiddle);
- });
+ return function(id, scope) {
+ function twiddle(){
+ audio.play();
+ audio.pause();
+ window.removeEventListener("click",twiddle);
}
-
var $audioWatch,
+ $intervalWatch,
$willPlay = false,
$willPause = false,
$willRestart = false,
$looping,
$isMuting = false,
$observeProperties = true,
+ $destroyed = false,
+ $scope = scope || $rootScope,
audio,
audioObject = this;
$willPlay = true;
return this;
};
-
+
var completeListeners = [];
this.complete = function(callback){
completeListeners.push(callback);
}
};
+ this.destroy = $destroy;
+
+ $scope.$on('$destroy', function() {
+ $destroy();
+ });
+
+ function $destroy() {
+ if (!$destroyed) {
+ if (interval) {
+ $interval.cancel(interval);
+ }
+ if ($intervalWatch) {
+ $intervalWatch();
+ }
+ if ($audioWatch) {
+ $audioWatch();
+ }
+ $destroyed = true;
+ }
+ }
+
function $setWatch() {
- $audioWatch = $rootScope.$watch(function() {
+ if ($destroyed) {
+ return;
+ }
+ $audioWatch = $scope.$watch(function() {
return {
volume: audioObject.volume,
currentTime: audioObject.currentTime,
progress: audioObject.progress,
muting: audioObject.muting,
loop: audioObject.loop,
- playbackRate: audioObject.playbackRate
+ playbackRate: audioObject.playbackRate,
+ globalVolume: ngAudioGlobals.volume
};
}, function(newValue, oldValue) {
+ //console.log("ngaudio watch callback for: " + audioObject.id);
if (newValue.currentTime !== oldValue.currentTime) {
audioObject.setCurrentTime(newValue.currentTime);
}
audioObject.setPlaybackRate(newValue.playbackRate);
}
+ if (newValue.globalVolume !== oldValue.globalVolume) {
+ if (newValue.globalVolume === 0) {
+ audioObject.setMuting(true);
+ } else {
+ audioObject.setMuting(false);
+ audioObject.setVolume(newValue.globalVolume);
+ }
+ }
+
$looping = newValue.loop;
cleverAudioFindingService.find(id)
.then(function(nativeAudio) {
audio = nativeAudio;
+ if (ngAudioGlobals.unlock) {
+
+ window.addEventListener("click", twiddle);
+
+ audio.addEventListener('playing', function() {
+ window.removeEventListener("click",twiddle);
+ });
+
+ }
+
audio.addEventListener('canplay', function() {
audioObject.canPlay = true;
});
-
}, function(error) {
audioObject.error = true;
console.warn(error);
var interval = $interval(checkWatchers, ngAudioGlobals.performance);
- $rootScope.$watch(function(){
+ $intervalWatch = $scope.$watch(function(){
return ngAudioGlobals.performance;
},function(){
$interval.cancel(interval);
interval = $interval(checkWatchers, ngAudioGlobals.performance);
})
-
+
function checkWatchers() {
if ($audioWatch) {
$audioWatch();
}
if (audio) {
- if ($isMuting || ngAudioGlobals.isMuting) {
+ if ($isMuting || ngAudioGlobals.muting) {
audio.volume = 0;
} else {
audio.volume = audioObject.volume !== undefined ? audioObject.volume : 1;
}
if ($willRestart) {
- audio.pause();
- audio.currentTime = 0;
+ audio.src = 'about:blank';
$willRestart = false;
}
audioObject.currentTime = audio.currentTime;
audioObject.duration = audio.duration;
audioObject.remaining = audio.duration - audio.currentTime;
- audioObject.progress = audio.currentTime / audio.duration;
+ audioObject.progress = 0; //We set initial value to 0
audioObject.paused = audio.paused;
audioObject.src = audio.src;
-
+
+ //After we check if progress is bigger than 0, and we set
+ var tempProgress = (audio.currentTime / audio.duration);
+ if(tempProgress > 0 ){
+ audioObject.progress = tempProgress;
+ }
+
if (audioObject.currentTime >= audioObject.duration) {
completeListeners.forEach(function(listener){
listener(audioObject);
}
}
- if (!$isMuting && !ngAudioGlobals.isMuting) {
+ if (!$isMuting && !ngAudioGlobals.muting) {
audioObject.volume = audio.volume;
}
};
}])
.service('ngAudio', ['NgAudioObject', 'ngAudioGlobals', function(NgAudioObject, ngAudioGlobals) {
- this.play = function(id) {
+ this.play = function(id, scope) {
- var audio = new NgAudioObject(id);
+ var audio = new NgAudioObject(id, scope);
audio.play();
return audio;
};
- this.load = function(id) {
- return new NgAudioObject(id);
+ this.load = function(id, scope) {
+ return new NgAudioObject(id, scope);
};
this.mute = function() {
this.setUnlock = function(unlock) {
ngAudioGlobals.unlock = unlock;
};
+
+ this.setGlobalVolume = function(globalVolume) {
+ ngAudioGlobals.volume = globalVolume;
+ };
}])
.filter("trackTime", function(){
/* Conveniently takes a number and returns the track time */
-
+
return function(input){
var totalSec = Math.floor(input | 0);
hours = Math.floor(totalSec / 3600);
minutes = Math.floor((totalSec - (hours * 3600)) / 60);
- seconds = (totalSec - ((minutes * 60) + (hours * 3600)));
+ seconds = (totalSec - ((minutes * 60) + (hours * 3600)));
if (hours.toString().length == 1) {
hours = "0" + (Math.floor(totalSec / 3600)).toString();
- }
+ }
if (minutes.toString().length == 1) {
minutes = "0" + (Math.floor((totalSec - (hours * 3600)) / 60)).toString();
- }
+ }
if (seconds.toString().length == 1) {
- seconds = "0" + (totalSec - ((minutes * 60) + (hours * 3600))).toString();
- }
+ seconds = "0" + (totalSec - ((minutes * 60) + (hours * 3600))).toString();
+ }
output = hours + ":" + minutes + ":" + seconds;
output = totalSec + "s";
}
-
- if (Number.isNaN(output)){
+
+ if (typeof Number.isNaN === "function" && Number.isNaN(output)){
debugger;
}
- return output;
+ return output;
}
});