-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlwAudio.js
138 lines (105 loc) · 2.74 KB
/
lwAudio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
angular.module('lwAudio', [])
.factory('lwAudio', function($interval, $q) {
var Audio = function () {
//Native object
this.nativeObject = document.createElement("AUDIO");
//Watchers
this.cancelWatchers = null;
this.volume = 0;
this.duration = 0;
this.currentTime = 0;
this.remaining = 0;
this.toFinishCallback = {
active: false,
time: null,
callback: null
};
//Events
this.canPlay = false;
}
//Update watcher properties
Audio.prototype.updateWatchers = function() {
//Update wachers
this.volume = this.nativeObject.volume;
this.duration = this.nativeObject.duration;
this.currentTime = this.nativeObject.currentTime;
this.remaining = this.nativeObject.duration - this.nativeObject.currentTime;
//Check callback events
if (this.toFinishCallback.active && (this.remaining <= this.toFinishCallback.time)) {
this.toFinishCallback.active = false;
this.toFinishCallback.callback();
}
}
//Start watchers
Audio.prototype.startWatchers = function(delay) {
if (!this.cancelWatchers) {
var self = this;
this.cancelWatchers = $interval(function() {
self.updateWatchers();
}, delay);
}
};
//Stop watchers
Audio.prototype.stopWatchers = function() {
if (this.cancelWatchers) {
$interval.cancel(this.cancelWatchers);
this.cancelWatchers = null;
}
}
//Load audio
Audio.prototype.load = function(src) {
var deferred = $q.defer();
this.canPlay = false;
//Load audio file
if (src) {
this.nativeObject.src = src;
}
this.nativeObject.load();
var self = this;
//Capture canplay event
var canplayEvent = function() {
self.canPlay = true;
self.updateWatchers();
self.nativeObject.removeEventListener('canplay', canplayEvent);
self.nativeObject.removeEventListener('error', errorEvent);
deferred.resolve();
}
this.nativeObject.addEventListener('canplay', canplayEvent);
//Capture canplay event
var errorEvent = function() {
self.canPlay = false;
self.nativeObject.removeEventListener('canplay', canplayEvent);
self.nativeObject.removeEventListener('error', errorEvent);
deferred.reject();
}
this.nativeObject.addEventListener('error', errorEvent);
return deferred.promise;
};
//Play audio
Audio.prototype.play = function() {
if (this.canPlay) {
this.nativeObject.play();
}
};
//Pause audio
Audio.prototype.pause = function() {
this.nativeObject.pause();
};
//Update volumen
Audio.prototype.setVolume = function(volume) {
this.nativeObject.volume = volume;
};
//Set to finish callback
Audio.prototype.setToFinishCallback = function(time, callback) {
this.toFinishCallback = {
active: true,
time: time,
callback: callback
};
};
return {
getInstance: function () {
return new Audio();
}
};
});