-
Notifications
You must be signed in to change notification settings - Fork 172
/
PxLoaderAudio.js
129 lines (105 loc) · 3.95 KB
/
PxLoaderAudio.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
// PxLoader plugin to load audio elements
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['pxloader'], function(PxLoader) {
return (root.PxLoaderAudio = factory(PxLoader));
});
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('pxloader'));
} else {
// Browser globals
root.PxLoaderAudio = factory(root.PxLoader);
}
}(this, function(PxLoader) {
function PxLoaderAudio(url, tags, priority, options) {
options = options || {};
var self = this,
loader = null,
audio;
this.readyEventName = 'canplaythrough';
audio = this.audio = document.createElement('audio');
if (options.origin) {
audio.crossOrigin = options.origin;
}
audio.preload = 'auto';
this.tags = tags;
this.priority = priority;
var onReadyStateChange = function() {
if (self.audio.readyState !== 4) {
return;
}
onLoad();
};
var onLoad = function() {
loader.onLoad(self);
cleanup();
};
var onError = function() {
loader.onError(self);
cleanup();
};
var onTimeout = function() {
loader.onTimeout(self);
cleanup();
};
var cleanup = function() {
self.unbind('load', onLoad);
self.unbind(self.readyEventName, onReadyStateChange);
self.unbind('error', onError);
// Force browser to release connection
self.audio.src = '';
};
this.start = function(pxLoader) {
// we need the loader ref so we can notify upon completion
loader = pxLoader;
// NOTE: Must add event listeners before the src is set. We
// also need to use the readystatechange because sometimes
// load doesn't fire when an audio is in the cache.
self.bind('load', onLoad);
self.bind(self.readyEventName, onReadyStateChange);
self.bind('error', onError);
// sometimes the browser will intentionally stop downloading
// the audio. In that case we'll consider the audio loaded
self.bind('suspend', onLoad);
self.audio.src = url;
self.audio.load();
};
// called by PxLoader to check status of audio (fallback in case
// the event listeners are not triggered).
this.checkStatus = function() {
onReadyStateChange();
};
// called by PxLoader when it is no longer waiting
this.onTimeout = function() {
if (self.audio.readyState !== 4) {
onLoad();
} else {
onTimeout();
}
};
// returns a name for the resource that can be used in logging
this.getName = function() {
return url;
};
// cross-browser event binding
this.bind = function(eventName, eventHandler) {
self.audio.addEventListener(eventName, eventHandler, false);
};
// cross-browser event un-binding
this.unbind = function(eventName, eventHandler) {
self.audio.removeEventListener(eventName, eventHandler, false);
};
}
// add a convenience method to PxLoader for adding audio
PxLoader.prototype.addAudio = function(url, tags, priority, options) {
var audioLoader = new PxLoaderAudio(url, tags, priority, options);
this.add(audioLoader);
// return the audio element to the caller
return audioLoader.audio;
};
return PxLoaderAudio;
}));