Skip to content

Commit 0b04583

Browse files
committed
added sound.js for ring-in sound effects.
1 parent 3794d3b commit 0b04583

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<!-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -->
1616
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
1717
<script src="webrtc.js"></script>
18+
<script src="sound.js"></script>
1819
<script>(function(){
1920

2021
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@@ -50,7 +51,9 @@
5051
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
5152
phone.receiver(function(session){
5253
console.log(session);
53-
// ...
54+
55+
// Play Sound Incoming Call
56+
sounds.play('tone/ring-in');
5457

5558
// Hangup Call
5659
//session.hangup();

sound.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Hey, let's be friends! http://twitter.com/pubnub */
2+
// -----------------------------------------------------------------------
3+
// SOUNDS
4+
// -----------------------------------------------------------------------
5+
var sounds = (function(){
6+
var soundbank = {}
7+
, p = PUBNUB;
8+
9+
function stop(audio) {
10+
if (!audio) return;
11+
audio.pause();
12+
reset(audio);
13+
}
14+
15+
function reset(audio) {
16+
try { audio.currentTime = 0.0 }
17+
catch (e) { }
18+
}
19+
20+
return {
21+
play : function( sound, duration ) {
22+
var audio = soundbank[sound] || (function(){
23+
var audio = soundbank[sound] = p.create('audio');
24+
25+
p.css( audio, { display : 'none' } );
26+
27+
p.attr( audio, 'prelaod', 'auto' );
28+
p.attr( audio, 'loop', 'true' );
29+
p.attr( audio, 'autoplay', 'true' );
30+
31+
audio.innerHTML = p.supplant(
32+
"<source src={file}.ogg><source src={file}.mp3>",
33+
{ file : sound }
34+
);
35+
36+
p.search('body')[0].appendChild(audio);
37+
38+
return audio;
39+
})();
40+
41+
stop(audio);
42+
audio.load();
43+
audio.play();
44+
45+
// Play a Set Portion of Audio
46+
clearTimeout(audio.timer);
47+
if (duration) audio.timer = setTimeout( function() {
48+
stop(audio);
49+
}, duration );
50+
},
51+
stop : function(sound) {
52+
stop(soundbank[sound]);
53+
},
54+
stopAll : function() {
55+
p.each( soundbank, function( _, audio ) {
56+
stop(audio);
57+
} );
58+
}
59+
};
60+
})();

webrtc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ var PHONE = window.PHONE = function(config) {
229229
// If Peer Connection is Successfully Established
230230
if (message.packet.establish) talk.establish(talk);
231231

232-
// If Peer Calling In
232+
// If Peer Calling Inbound (Incoming)
233233
if (message.packet.receiver) receivercb(talk);
234234

235235
// Update Peer Connection with SDP Offer or ICE Routes

0 commit comments

Comments
 (0)