make sure event handlers aren't added multiple times
This commit is contained in:
parent
feb102251e
commit
06cc9ecd54
1 changed files with 33 additions and 11 deletions
|
@ -15,6 +15,8 @@ angular.module('podcasts.services', ['podcasts.utilities', 'podcasts.queueList',
|
||||||
audio.setAttribute("mozaudiochannel", "content");
|
audio.setAttribute("mozaudiochannel", "content");
|
||||||
var currentFeedItem = null;
|
var currentFeedItem = null;
|
||||||
var nowPlaying = {position: 0, duration: 0, title: '', description: '', feed: '', date: 0};
|
var nowPlaying = {position: 0, duration: 0, title: '', description: '', feed: '', date: 0};
|
||||||
|
var hasOfflineErrorHandler = false;
|
||||||
|
var hasPauseEventListener = false;
|
||||||
|
|
||||||
var acm = navigator.mozAudioChannelManager;
|
var acm = navigator.mozAudioChannelManager;
|
||||||
|
|
||||||
|
@ -26,10 +28,38 @@ angular.module('podcasts.services', ['podcasts.utilities', 'podcasts.queueList',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addOfflineErrorHandler($scope)
|
||||||
|
{
|
||||||
|
if (!hasOfflineErrorHandler) {
|
||||||
|
audio.addEventListener("error", function(event) {
|
||||||
|
console.log('Error when loading audio file, continuing to next file');
|
||||||
|
|
||||||
|
var nextFeedItemPromise = feedItems.getNextInQueue(currentFeedItem);
|
||||||
|
$rootScope.$apply(nextFeedItemPromise.then(function(nextFeedItem) {
|
||||||
|
play(nextFeedItem, $scope);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
hasOfflineErrorHandler = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addPauseEventListener()
|
||||||
|
{
|
||||||
|
if (!hasPauseEventListener) {
|
||||||
|
audio.addEventListener("pause", function(event) {
|
||||||
|
console.log('paused audio');
|
||||||
|
currentFeedItem.position = Math.floor(event.target.currentTime);
|
||||||
|
db.put("feedItem", currentFeedItem);
|
||||||
|
});
|
||||||
|
|
||||||
|
hasPauseEventListener = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function play(feedItem, $scope)
|
function play(feedItem, $scope)
|
||||||
{
|
{
|
||||||
var delayPlay = false;
|
var delayPlay = false;
|
||||||
|
|
||||||
if (feedItem) {
|
if (feedItem) {
|
||||||
console.log('playing: ' + feedItem.title);
|
console.log('playing: ' + feedItem.title);
|
||||||
|
|
||||||
|
@ -47,6 +77,8 @@ angular.module('podcasts.services', ['podcasts.utilities', 'podcasts.queueList',
|
||||||
audio.src = audioSrc;
|
audio.src = audioSrc;
|
||||||
updateSong(feedItem, $scope);
|
updateSong(feedItem, $scope);
|
||||||
|
|
||||||
|
addOfflineErrorHandler($scope);
|
||||||
|
|
||||||
if (feedItem.position) {
|
if (feedItem.position) {
|
||||||
delayPlay = true;
|
delayPlay = true;
|
||||||
angular.element(audio).bind('canplay', function(event) {
|
angular.element(audio).bind('canplay', function(event) {
|
||||||
|
@ -69,7 +101,6 @@ angular.module('podcasts.services', ['podcasts.utilities', 'podcasts.queueList',
|
||||||
console.log('got promise for next feed item');
|
console.log('got promise for next feed item');
|
||||||
$rootScope.$apply(nextFeedItemPromise.then(function(nextFeedItem) {
|
$rootScope.$apply(nextFeedItemPromise.then(function(nextFeedItem) {
|
||||||
console.log('Got next Feed Item:');
|
console.log('Got next Feed Item:');
|
||||||
console.log(nextFeedItem);
|
|
||||||
console.log(nextFeedItem.title);
|
console.log(nextFeedItem.title);
|
||||||
play(nextFeedItem, $scope);
|
play(nextFeedItem, $scope);
|
||||||
|
|
||||||
|
@ -86,15 +117,6 @@ angular.module('podcasts.services', ['podcasts.utilities', 'podcasts.queueList',
|
||||||
|
|
||||||
angular.element(this).unbind();
|
angular.element(this).unbind();
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO: handle save when feedItem is not passed in
|
|
||||||
angular.element(audio).bind('pause', function(event) {
|
|
||||||
console.log('paused audio');
|
|
||||||
feedItem.position = Math.floor(event.target.currentTime);
|
|
||||||
db.put("feedItem", feedItem);
|
|
||||||
|
|
||||||
angular.element(this).unbind();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pause()
|
function pause()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue