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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/**
* @fileOverview A jaws.Audio object that detects and loads supported filetypes
*
* The jaws.Audio object uses jaws.assets to determine if a particular
* reported MIME audio filetype can be played within the current enviroment.
*
* Note: If loading fails at any point or no supported types were found,
* the 'audio' property is set to null.
*
* @see jaws.assets.file_type
* @see jaws.assets.can_play
*
* @class jaws.Audio
* @property {array|string|Audio|null} audio A string or array of file locations initally;
* replaced with an Audio object if loading was successful
*
* @example
* var audio = new jaws.Audio({audio: ["file.mp3", "file.ogg"], volume: 1.0});
*
* audio.play() //Assuming either MP3 or OGG is supported
*
* //Because play(), stop(), and other audio functions will log an error
* // if 'audio' is not an Audio object, its load status can be checked too.
*
* if(audio.isLoaded()) {
* audio.play();
* }
*
*/
var jaws = (function(jaws) {
/**
* jaws.Audio object
* @constructor
* @param {object} options Object-literal of constructor properties
* @param {bool} [options.loop] Whether to initially loop the playing or not
* @param {bool} [options.mute] Whether to initially mute the audio or not
* @param {bool} [options.pause] Whether to initially pause the audio or not
* @param {bool} [options.autoplay] Whether to start playing after loading
* @param {int} [options.volume] Initial audio volume, between 0.1 and 1.0
* @param {function} [options.onend] Initial callback on "end" event for audio
* @param {function} [options.onplay] Initial callback on "play" event for audio
* @param {function} [options.onpause] Initial callback on "pause" event for audio
* @see jaws.audio.set()
*/
jaws.Audio = function Audio(options) {
if (!(this instanceof arguments.callee))
return new arguments.callee(options);
if (typeof Audio !== "undefined") {
this.set(options);
} else {
jaws.log.error("jaws.Audio (constructor): 'Audio' object does not exist.");
this.audio = null;
}
};
/**
* The default properties and their values
* @type {object}
*/
jaws.Audio.prototype.default_options = {
audio: null,
autoplay: false,
loop: false,
volume: 0,
onend: null,
onplay: null,
onpause: null,
_constructor: null
};
/**
* Compares default_options to the constructor options and loads an audio resource if able
* @param {object} options Object-literal of constructor properties
* @this {jaws.Audio}
* @returns {object} The 'this' scope of the calling instance of an jaws.Audio object
*/
jaws.Audio.prototype.set = function(options) {
jaws.parseOptions(this, options, this.default_options);
if (this.audio) {
if (jaws.isString(this.audio)) {
var type = jaws.assets.getPostfix(this.audio);
if (jaws.assets.file_type[type] && jaws.assets.can_play[type]) {
this.setAudio(this.audio);
} else {
jaws.log.warn("jaws.Audio.set: Unknown or unplayable MIME filetype.");
this.audio = null;
}
} else if (jaws.isArray(this.audio)) {
for (var i = 0; i < this.audio.length; i++) {
if (jaws.isString(this.audio[i])) {
var type = jaws.assets.getPostfix(this.audio[i]);
if (jaws.assets.file_type[type] && jaws.assets.can_play[type]) {
this.setAudio(this.audio[i]);
break;
}
}
}
if (!(this.audio instanceof Audio)) {
jaws.log.warn("jaws.Audio.set: No known or playable MIME filetypes were found.");
this.audio = null;
}
} else {
jaws.log.error("jaws.Audio.set: Passed in 'audio' property is neither a String nor Array");
this.audio = null;
}
}
return this;
};
/**
* Loads a reference from the jaws.assets cache or requests that an Audio resource URL be loaded
* @param {type} value
* @this {jaws.Audio}
* @returns {object} The 'this' scope of the calling instance of an jaws.Audio object
*/
jaws.Audio.prototype.setAudio = function(value) {
var self = this;
if (jaws.assets.isLoaded(value)) {
var audio = jaws.assets.get(value);
if (audio instanceof Audio) {
this.audio = audio;
if (this.volume >= 0 && this.volume <= 1.0 && jaws.isNumber(this.volume))
this.audio.volume = this.volume;
if (this.loop)
this.audio.loop = this.loop;
if (this.onend && jaws.isFunction(this.onend)) {
this.audio.addEventListener('end', this.onend);
}
if (this.onplay && jaws.isFunction(this.onplay)) {
this.audio.addEventListener('play', this.onplay);
}
if (this.onpause && jaws.isFunction(this.onplay)) {
this.audio.addEventListener('pause', this.onpause);
}
if (this.autoplay)
this.audio.autoplay = this.autoplay;
} else {
this.audio = null;
}
} else {
jaws.log.warn("jaws.Audio.setAudio: Audio '" + value + "' not preloaded with jaws.assets.add().");
jaws.assets.load(value, function() {
var audio = jaws.assets.get(value);
if (audio instanceof Audio) {
self.audio = audio;
if (self.volume >= 0 && self.volume <= 1.0 && jaws.isNumber(self.volume))
self.audio.volume = self.volume;
if (self.loop)
self.audio.loop = self.loop;
if (self.hasOwnProperty("onend") && jaws.isFunction(self.onend)) {
self.audio.addEventListener('end', self.onend);
}
if (self.hasOwnProperty("onplay") && jaws.isFunction(self.onplay)) {
self.audio.addEventListener('play', self.onplay);
}
if (self.hasOwnProperty("onpause") && jaws.isFunction(self.onplay)) {
self.audio.addEventListener('pause', self.onpause);
}
if (self.autoplay)
self.audio.autoplay = self.autoplay;
} else {
self.audio = null;
}
}, function() {
jaws.log.error("jaws.Audio.setAudio: Could not load Audio resource URL " + value);
self.audio = null;
});
}
return this;
};
/**
* Plays audio
* @this {jaws.Audio}
*/
jaws.Audio.prototype.play = function() {
if (this.audio instanceof Audio) {
this.audio.play();
} else {
jaws.log.error("jaws.Audio.play: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Stops audio
* @this {jaws.Audio}
*/
jaws.Audio.prototype.stop = function() {
if (this.audio instanceof Audio) {
this.audio.pause();
this.audio.currentTime = 0;
} else {
jaws.log.error("jaws.Audio.stop: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Pauses audio
* @this {jaws.Audio}
*/
jaws.Audio.prototype.pause = function() {
if (this.audio instanceof Audio) {
this.audio.pause();
} else {
jaws.log.error("jaws.Audio.pause: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Mutes the audio
* @this {jaws.Audio}
*/
jaws.Audio.prototype.mute = function() {
if (this.audio instanceof Audio) {
this.audio.mute = true;
} else {
jaws.log.error("jaws.Audio.mute: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Unmute the audio
* @this {jaws.Audio}
*/
jaws.Audio.prototype.unmute = function() {
if (this.audio instanceof Audio) {
this.audio.mute = false;
} else {
jaws.log.error("jaws.Audio.unmute: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Seeks to a position in audio
* @param {type} value
* @this {jaws.Audio}
*/
jaws.Audio.prototype.seekTo = function(value) {
if (this.audio instanceof Audio) {
if (jaws.isNumber(value)) {
if (value <= this.audio.duration) {
this.audio.currentTime = value;
} else {
this.audio.currentTime = this.audio.duration;
}
}
} else {
jaws.log.warn("jaws.Audio.seekTo: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Sets the volume of the audio
* @param {type} value The new volume within the range 0.0 to 1.0
* @this {jaws.Audio}
*/
jaws.Audio.prototype.setVolume = function(value) {
if (this.audio instanceof Audio) {
if (jaws.isNumber(value) && value <= 1.0 && value >= 0) {
this.audio.volume = value;
}
} else {
jaws.log.warn("jaws.Audio: jaws.setVolume: Either 'audio' was loaded incorrectly or does not exist");
}
};
/**
* Returns if 'audio' is actually an Audio object
*/
jaws.Audio.prototype.isLoaded = function() {
return (this.audio instanceof Audio);
};
/**
* Returns a String containing value properties
* @this {jaws.Audio}
* @returns {String}
*/
jaws.Audio.prototype.toString = function() {
var properties = "[Audio ";
if (this.audio instanceof Audio) {
properties += this.audio.src + ", ";
properties += this.audio.currentTime + ", ";
properties += this.audio.duration + ", ";
properties += this.audio.volume + " ]";
} else {
properties += null + " ]";
}
return properties;
};
/**
* Returns an object created with values from 'this' properties
* @this {jaws.Audio}
* @returns {object}
*/
jaws.Audio.prototype.attributes = function() {
var object = this.options;
object["_constructor"] = this._constructor || "jaws.Audio";
if (this.audio instanceof Audio) {
object["audio"] = this.audio.src;
object["loop"] = this.loop;
object["muted"] = this.audio.muted;
object["volume"] = this.audio.volume;
} else {
object["audio"] = null;
}
if (this.hasOwnProperty("autoplay"))
object["autoplay"] = this.autoplay;
return object;
};
/**
* Return the properties of the current object as a JSON-encoded string
* @this {jaws.Audio}
* @returns {string} The properties of the jaws.Audio object as a JSON-encoded string
*/
jaws.Audio.prototype.toJSON = function() {
return JSON.stringify(this.attributes());
};
return jaws;
})(jaws || {});
// Support CommonJS require()
if (typeof module !== "undefined" && ('exports' in module)) {
module.exports = jaws.Audio;
}
|