forked from llaske/sugarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
257 lines (228 loc) · 6.04 KB
/
audio.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
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
// Abecedarium audio stuff
// Basic HTML 5 Audio Element encapsulation
enyo.kind({
name: "HTML5.Audio",
kind: enyo.Control,
tag: "audio",
published: {
src: "", crossorigin: "", preload: "auto",
mediagroup: "", loop: false, muted: "", controlsbar: false
},
events: {
onSoundEnded: "",
onSoundTimeupdate: ""
},
// Constructor
create: function() {
this.inherited(arguments);
this.isCordova = (enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http";
this.startPlay = false;
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
this.loopChanged();
this.mutedChanged();
this.controlsbarChanged();
this.handleVolumeButtons();
},
// Handle volume buttons on Android
handleVolumeButtons: function() {
if (this.isCordova && !enyo.platform.iOS) {
// HACK: Need only on Android because Cordova intercept volume buttons
var emptyf = function() {};
document.addEventListener("volumeupbutton", function() {
cordova.plugins.VolumeControl.getVolume(function(value) {
var volume = parseInt(value);
if (volume < 100) {
cordova.plugins.VolumeControl.setVolume((volume+10), emptyf, emptyf);
}
}, emptyf);
}, false);
document.addEventListener("volumedownbutton", function() {
cordova.plugins.VolumeControl.getVolume(function(value) {
var volume = parseInt(value);
if (volume > 0) {
cordova.plugins.VolumeControl.setVolume((volume-1), emptyf, emptyf);
}
}, emptyf);
}, false);
}
},
// Render
rendered: function() {
this.inherited(arguments);
// Handle init
if (this.hasNode()) {
// Handle sound ended event
var audio = this;
enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
audio.doSoundEnded();
});
enyo.dispatcher.listen(audio.hasNode(), "timeupdate", function(s) {
audio.doSoundTimeupdate({timeStamp: s.timeStamp});
});
}
},
// Property changed
srcChanged: function() {
this.setAttribute("src", this.src);
},
crossoriginChanged: function() {
this.setAttribute("crossorigin", this.crossorigin);
},
preloadChanged: function() {
this.setAttribute("preload", this.preload);
},
loopChanged: function() {
this.setAttribute("loop", this.loop);
},
mutedChanged: function() {
if (this.muted.length != 0)
this.setAttribute("muted", this.muted);
},
controlsbarChanged: function() {
this.setAttribute("controls", this.controlsbar);
},
// Test if component could play a file type
canPlayType: function(typename) {
var node = this.hasNode();
if (!node)
return false;
return node.canPlayType(typename);
},
// Play audio
play: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if (this.isCordova) {
// Compute full path
var database = Abcd.context.getDatabase();
var src = ((database.length == 0 || this.src.indexOf("database") == -1 ) ? location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src : this.src);
var that = this;
if (this.media) {
this.media.src = "";
this.media.pause();
this.media.release();
}
// Ready to play
var playMedia = function(url, that) {
// Create the Media object
that.media = new Media(url, function() { }, function() { },
function(status) {
if (status == 4 && this.src != "") {
that.doSoundEnded();
}
}
);
// Play
that.media.play();
}
// HACK: On iOS, remote MP3 should be downloaded locally First
if (enyo.platform.ios && src.substr(0,4) == "http") {
var fileTransfer = new FileTransfer();
var fileURL = "cdvfile://localhost/temporary/sugarizer/abecedarium.mp3";
fileTransfer.download(
src,
fileURL,
function(entry) {
playMedia(fileURL, that);
},
function(error) {
console.log("download error code " + error.code);
},
true
);
} else {
playMedia(src, this);
}
return;
}
var node = this.hasNode();
if (!node)
return;
this.started = false;
var promise = node.play();
if (promise) {
var that = this;
promise.then(function() {
that.started = true;
}).catch(function() {});
} else {
this.started = true;
}
},
// Pause audio
pause: function() {
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
if (this.isCordova) {
if (!this.media)
return;
this.media.src = "";
this.media.pause();
this.media.release();
return;
}
var node = this.hasNode();
if (!node)
return;
if (this.started) {
node.pause();
}
},
// Test if audio is paused
paused: function() {
var node = this.hasNode();
if (!node)
return false;
return node.paused;
},
// Test if audio is ended
ended: function() {
var node = this.hasNode();
if (!node)
return false;
return node.ended;
}
});
// Abecedarium Audio engine
enyo.kind({
name: "Abcd.Audio",
kind: enyo.Control,
components: [
{ name: "sound", kind: "HTML5.Audio", preload: "auto", autobuffer: true, controlsbar: false,
onSoundEnded: "broadcastEnd", onSoundTimeupdate: "broadcastUpdate" }
],
// Constructor
create: function() {
this.inherited(arguments);
this.format = null;
},
// First render, test sound format supported
rendered: function() {
this.inherited(arguments);
if (this.$.sound.canPlayType("audio/ogg"))
this.format = ".ogg";
else if (this.$.sound.canPlayType("audio/mpeg"))
this.format = ".mp3";
},
// Play a sound
play: function(sound) {
if (this.format == null)
return;
this.$.sound.setSrc(sound+this.format);
this.timeStamp = new Date().getTime();
this.$.sound.play();
},
// Pause
pause: function() {
if (this.format == null)
return;
this.$.sound.pause();
},
// End of sound detected, broadcast the signal
broadcastEnd: function() {
enyo.Signals.send("onEndOfSound", {sound:this.$.sound.src.substring(0,this.$.sound.src.length-4)});
},
broadcastUpdate: function(s, e) {
enyo.Signals.send("onSoundTimeupdate", {timestamp:e.timeStamp-this.timeStamp});
}
});