Kaynağa Gözat

Add sound notifications

Robin Linus 9 yıl önce
ebeveyn
işleme
ce61dc7c60

+ 4 - 0
README.md

@@ -52,3 +52,7 @@ ShareDrop uses WebRTC only and isn't compatible with Safari Browsers. Snapdrop u
     * `npm install & bower install`
 * run `gulp serve`
 * In a second shell run `node index.js`
+
+
+## Licences
+* Thanks to [Mark DiAngelo]() for the [Blop Sound](http://soundbible.com/2067-Blop.html)

+ 24 - 0
app/elements/sound-notification/sound-notification-behavior.html

@@ -0,0 +1,24 @@
+<link rel="import" href="sound-notification.html">
+<script>
+'use strict';
+Chat = window.Chat || {};
+Chat.SoundNotificationBehavior = {
+    sounds: function() {
+        var sounds = document.querySelector('sound-notification');
+        if (!sounds) {
+            sounds = Polymer.Base.create('sound-notification');
+            document.body.appendChild(sounds);
+        }
+        return sounds;
+    },
+    attached: function() {
+        //lazy load sound files
+        setTimeout(function() {
+            this.sounds();
+        }.bind(this), 1000);
+    },
+    playSound: function(e) {
+        this.sounds().play();
+    }
+};
+</script>

+ 55 - 0
app/elements/sound-notification/sound-notification.html

@@ -0,0 +1,55 @@
+<dom-module id="sound-notification">
+    <template>
+        <audio id="blop" preload="auto" autobuffer="true">
+            <source src="/sounds/blop.mp3" id="mp3Source" type="audio/mpeg">
+            <source src="/sounds/blop.ogg" id="oggSource" type="audio/ogg">
+        </audio>
+    </template>
+</dom-module>
+<script>
+'use strict';
+Polymer({
+    is: 'sound-notification',
+    properties: {
+        volumes: {
+            value: {
+                'blop': 0.8,
+            }
+        }
+    },
+    attached: function() {
+        var that = this;
+        var hackListener = function() {
+            that.volumes.blop = 0.1;
+            that.play();
+            document.body.removeEventListener('touchstart', hackListener, false);
+            that.volumes.blop = 0.8;
+        };
+        document.body.addEventListener('touchstart', hackListener, false);
+    },
+    play: function() {
+        this._play('blop');
+    },
+    _play: function(sound) {
+        var audio = this.$[sound];
+        if (!audio) {
+            console.warn('audio ', sound, ' doesn\'t exist.');
+            return;
+        }
+
+        if (audio.readyState > 0) {
+            audio.volume = this.volumes[sound];
+            audio.pause();
+            audio.currentTime = 0;
+            audio.play();
+        } else {
+            console.warn('audio not ready yet...');
+            //play when ready
+            //TODO: play only if ready within next ~500ms 
+            audio.addEventListener('loadedmetadata', function() {
+                this._play(sound);
+            }.bind(this), false);
+        }
+    }
+});
+</script>

BIN
app/sounds/blop.mp3


BIN
app/sounds/blop.ogg