Skip to content
Permalink
Newer
Older
100644 197 lines (171 sloc) 4.43 KB
1
// TankOp audio stuff
2
3
// Basic HTML 5 Audio Element encapsulation
4
enyo.kind({
5
name: "HTML5.Audio",
6
kind: enyo.Control,
7
tag: "audio",
8
published: {
9
src: "", crossorigin: "", preload: "auto",
10
mediagroup: "", loop: false, muted: "", controlsbar: false
11
},
12
events: {
13
onSoundEnded: "",
14
onSoundTimeupdate: ""
15
},
16
17
// Constructor
18
create: function() {
19
this.inherited(arguments);
20
21
this.srcChanged();
22
this.crossoriginChanged();
23
this.preloadChanged();
24
this.loopChanged();
25
this.mutedChanged();
26
this.controlsbarChanged();
27
},
28
29
// Render
30
rendered: function() {
31
this.inherited(arguments);
32
33
// Handle init
34
if (this.hasNode()) {
35
// Handle sound ended event
36
var audio = this;
37
enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
38
audio.doSoundEnded();
39
});
40
enyo.dispatcher.listen(audio.hasNode(), "timeupdate", function(s) {
41
audio.doSoundTimeupdate({timeStamp: s.timeStamp});
42
});
43
}
44
},
45
46
// Property changed
47
srcChanged: function() {
48
this.setAttribute("src", this.src);
49
},
50
51
crossoriginChanged: function() {
52
this.setAttribute("crossorigin", this.crossorigin);
53
},
54
55
preloadChanged: function() {
56
this.setAttribute("preload", this.preload);
57
},
58
59
loopChanged: function() {
60
this.setAttribute("loop", this.loop);
61
},
62
63
mutedChanged: function() {
64
if (this.muted.length != 0)
65
this.setAttribute("muted", this.muted);
66
},
67
68
controlsbarChanged: function() {
69
this.setAttribute("controls", this.controlsbar);
70
},
71
72
// Test if component could play a file type
73
canPlayType: function(typename) {
74
var node = this.hasNode();
75
if (!node)
76
return false;
77
return node.canPlayType(typename);
78
},
79
80
// Play audio
81
play: function() {
82
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
83
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
84
// Compute full path
85
var src = location.pathname.substring(0,1+location.pathname.lastIndexOf('/'))+this.src;
86
var that = this;
87
if (this.media) {
88
this.media.src = "";
89
this.media.pause();
90
this.media.release();
91
}
92
93
// Create the Media object
94
this.media = new Media(src, function() { }, function() { },
95
function(status) {
96
if (status == 4 && this.src != "") {
97
that.doSoundEnded();
98
}
99
}
100
);
101
102
// Play
103
this.media.play();
104
return;
105
}
106
var node = this.hasNode();
107
if (!node)
108
return;
109
node.play();
110
},
111
112
// Pause audio
113
pause: function() {
114
// HACK: HTML5 Audio don't work in PhoneGap on Android and iOS, use Media PhoneGap component instead
115
if ((enyo.platform.android || enyo.platform.androidChrome || enyo.platform.ios) && document.location.protocol.substr(0,4) != "http") {
116
if (!this.media)
117
return;
118
this.media.src = "";
119
this.media.pause();
120
this.media.release();
121
return;
122
}
123
var node = this.hasNode();
124
if (!node)
125
return;
126
node.pause();
127
},
128
129
// Test if audio is paused
130
paused: function() {
131
var node = this.hasNode();
132
if (!node)
133
return false;
134
return node.paused;
135
},
136
137
// Test if audio is ended
138
ended: function() {
139
var node = this.hasNode();
140
if (!node)
141
return false;
142
return node.ended;
143
}
144
});
145
146
// TankOp Audio engine
147
enyo.kind({
148
name: "TankOp.Audio",
149
kind: enyo.Control,
150
components: [
151
{ name: "sound", kind: "HTML5.Audio", preload: "auto", autobuffer: true, controlsbar: false,
152
onSoundEnded: "broadcastEnd", onSoundTimeupdate: "broadcastUpdate" }
153
],
154
155
// Constructor
156
create: function() {
157
this.inherited(arguments);
158
this.format = null;
159
},
160
161
// First render, test sound format supported
162
rendered: function() {
163
this.inherited(arguments);
164
165
if (this.$.sound.canPlayType("audio/ogg"))
166
this.format = ".ogg";
167
else if (this.$.sound.canPlayType("audio/mpeg"))
168
this.format = ".mp3";
169
},
170
171
// Play a sound
February 27, 2014 15:35
172
play: function(sound, loop) {
173
if (this.format == null)
174
return;
175
this.$.sound.setSrc(sound+this.format);
February 27, 2014 15:35
176
this.$.sound.setLoop(loop === true);
177
this.timeStamp = new Date().getTime();
178
this.render();
179
this.$.sound.play();
180
},
181
182
// Pause
183
pause: function() {
184
if (this.format == null)
185
return;
186
this.$.sound.pause();
187
},
188
189
// End of sound detected, broadcast the signal
190
broadcastEnd: function() {
191
enyo.Signals.send("onEndOfSound", this.$.sound.src.substring(0,this.$.sound.src.length-4));
192
},
193
194
broadcastUpdate: function(s, e) {
195
enyo.Signals.send("onSoundTimeupdate", e.timeStamp-this.timeStamp);
196
}
197
});