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
|
// 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: ""
},
// Constructor
create: function() {
this.inherited(arguments);
this.srcChanged();
this.crossoriginChanged();
this.preloadChanged();
this.loopChanged();
this.mutedChanged();
this.controlsbarChanged();
},
// Render
rendered: function() {
this.inherited(arguments);
// Handle init
if (this.hasNode() != null) {
// Handle sound ended event
var audio = this;
enyo.dispatcher.listen(audio.hasNode(), "ended", function() {
audio.doSoundEnded();
});
}
},
// 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 == null)
return false;
return node.canPlayType(typename);
},
// Play audio
play: function() {
var node = this.hasNode();
if (node == null)
return;
node.play();
},
// Pause audio
pause: function() {
var node = this.hasNode();
if (node == null)
return;
node.pause();
},
// Test if audio is paused
paused: function() {
var node = this.hasNode();
if (node == null)
return false;
return node.paused;
},
// Test if audio is ended
ended: function() {
var node = this.hasNode();
if (node == null)
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" }
],
// 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.$.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", this.$.sound.src.substring(0,this.$.sound.src.length-4));
}
});
|