sound-notification.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <dom-module id="sound-notification">
  2. <template>
  3. <audio id="blop" preload="auto" autobuffer="true">
  4. <source src="/sounds/blop.mp3" id="mp3Source" type="audio/mpeg">
  5. <source src="/sounds/blop.ogg" id="oggSource" type="audio/ogg">
  6. </audio>
  7. </template>
  8. </dom-module>
  9. <script>
  10. 'use strict';
  11. Polymer({
  12. is: 'sound-notification',
  13. properties: {
  14. volumes: {
  15. value: {
  16. 'blop': 0.8,
  17. }
  18. }
  19. },
  20. attached: function() {
  21. var that = this;
  22. var hackListener = function() {
  23. that.volumes.blop = 0.1;
  24. that.play();
  25. document.body.removeEventListener('touchstart', hackListener, false);
  26. that.volumes.blop = 0.8;
  27. };
  28. document.body.addEventListener('touchstart', hackListener, false);
  29. },
  30. play: function() {
  31. this._play('blop');
  32. },
  33. _play: function(sound) {
  34. var audio = this.$[sound];
  35. if (!audio) {
  36. console.warn('audio ', sound, ' doesn\'t exist.');
  37. return;
  38. }
  39. if (audio.readyState > 0) {
  40. audio.volume = this.volumes[sound];
  41. audio.pause();
  42. audio.currentTime = 0;
  43. audio.play();
  44. } else {
  45. console.warn('audio not ready yet...');
  46. //play when ready
  47. //TODO: play only if ready within next ~500ms
  48. audio.addEventListener('loadedmetadata', function() {
  49. this._play(sound);
  50. }.bind(this), false);
  51. }
  52. }
  53. });
  54. </script>