Lungo.Device.Audio.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Audio API (HTML5 Feature)
  3. * Pending to final SPEC, now it's a Phonegap Wrapper
  4. *
  5. *
  6. * @namespace LUNGO.Device
  7. * @class Audio
  8. *
  9. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  10. */
  11. LUNGO.Device.Audio = (function(lng, undefined) {
  12. var _background = document.createElement('audio');
  13. var _sound = document.createElement('audio');
  14. /**
  15. * Plays music in background with automatic rewind.
  16. *
  17. * @method background
  18. *
  19. * @param {string} Source of sound file
  20. */
  21. var background = function(source) {
  22. if (source) {
  23. _setSourceAndPlay(_sound, source);
  24. _background.addEventListener('ended', function(){
  25. this.currentTime = 0;
  26. }, false);
  27. }
  28. else {
  29. _background.pause();
  30. }
  31. };
  32. /**
  33. * Play a given sound
  34. *
  35. * @method play
  36. *
  37. * @param {string} Source of sound file
  38. */
  39. var play = function(source) {
  40. _setSourceAndPlay(_sound, source);
  41. };
  42. _setSourceAndPlay = function(container, source) {
  43. container.setAttribute('src', source);
  44. container.play();
  45. };
  46. return {
  47. background: background,
  48. play: play
  49. }
  50. })(LUNGO);