forked from stephenlb/webrtc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
58 lines (49 loc) · 1.72 KB
/
sound.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
/* Hey, let's be friends! http://twitter.com/pubnub */
// -----------------------------------------------------------------------
// SOUNDS
// -----------------------------------------------------------------------
var sounds = (function(){
var soundbank = {};
function stop(audio) {
if (!audio) return;
//audio.pause();
//reset(audio);
}
function reset(audio) {
try { audio.currentTime = 0.0 }
catch (e) { }
}
return {
play : function( sound, duration ) {
var audio = soundbank[sound] || (function(){
var audio = soundbank[sound]=document.createElement('audio');
audio.setAttribute( 'style', 'display:none' );
audio.setAttribute( 'prelaod', 'auto' );
audio.setAttribute( 'autoplay', 'true' );
audio.innerHTML = "<source src=" + sound +
".ogg><source src=" + sound +
".mp3>";
document.getElementsByTagName('body')[0].appendChild(audio);
return audio;
})();
setTimeout( () => {
stop(audio);
//audio.load();
try { audio.play() } catch(e) {}
}, 10 );
// Play a Set Portion of Audio
clearTimeout(audio.timer);
if (duration) audio.timer = setTimeout( function() {
stop(audio);
}, duration );
},
stop : function(sound) {
stop(soundbank[sound]);
},
stopAll : function() {
soundbank.forEach(function(audio){
stop(audio);
});
}
};
})();