audio.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. (function ($, sm) {
  2. sm.setup({
  3. preferFlash:false,
  4. debugMode:false,
  5. onready: function () {
  6. sm.loaded = true;
  7. $(window).trigger('sound_manager.loaded');
  8. }
  9. });
  10. var Player = function (obj, url) {
  11. Player.currentId += 1;
  12. this.initSound('sound-' + Player.currentId, url);
  13. this.buildDom(obj);
  14. this.bindEvents();
  15. };
  16. Player.currentId = 0;
  17. Player.prototype = {
  18. initSound: function (id, url) {
  19. var self = this;
  20. var createSound = function () {
  21. self.sound = sm.createSound({
  22. id: id,
  23. url: url,
  24. volume: 50,
  25. autoload: false,
  26. whileloading: function () { self.whileLoading(); },
  27. whileplaying: function () { self.whilePlaying(); },
  28. onfinish: function () { self.onFinish(); }
  29. });
  30. };
  31. if (sm.loaded) createSound();
  32. else $(window).bind('sound_manager.loaded', createSound);
  33. },
  34. buildDom: function (obj) {
  35. this.dom = {};
  36. this.dom.play = $('<div class="play fa fa-play"></div>').appendTo(obj);
  37. this.dom.timeLeft = $('<div class="time-left">00 : 00</div>').appendTo(obj);
  38. this.dom.progress = $('<div class="progress"></div>').appendTo(obj);
  39. this.dom.loaded = $('<div class="loaded"></div>').appendTo(this.dom.progress);
  40. this.dom.progressPointer = $('<div class="pointer"></div>').appendTo(this.dom.progress);
  41. this.dom.timeRight = $('<div class="time-right">00 : 00</div>').appendTo(obj);
  42. this.dom.mute = $('<div class="mute fa fa-volume-up"></div>').appendTo(obj);
  43. this.dom.volume = $('<div class="volume"></div>').appendTo(obj);
  44. this.dom.volumePointer = $('<div class="pointer"></div>').appendTo(this.dom.volume);
  45. },
  46. bindEvents: function () {
  47. var self = this;
  48. this.dom.play.click(function () {
  49. $(this).toggleClass('fa-play fa-pause');
  50. self.sound.togglePause();
  51. });
  52. this.dom.mute.click(function () {
  53. $(this).toggleClass('fa-volume-up fa-volume-off');
  54. self.sound.toggleMute();
  55. });
  56. this.dom.progress.click(function (event) {
  57. var offsetLeft = event.pageX - $(this).offset().left;
  58. var ratio = offsetLeft / $(this).width();
  59. var position = ratio * self.sound.durationEstimate;
  60. self.sound.setPosition(position);
  61. self.setProgressPointer();
  62. });
  63. this.dom.volume.click(function (event) {
  64. var offsetLeft = event.pageX - $(this).offset().left;
  65. var ratio = offsetLeft / $(this).width();
  66. var volume = ratio * 100;
  67. self.sound.setVolume(volume);
  68. self.setVolumePointer();
  69. });
  70. },
  71. whileLoading: function () {
  72. var ratio = this.sound.bytesLoaded / this.sound.bytesTotal;
  73. var offset = ratio * this.dom.progress.width();
  74. this.dom.loaded.width(offset);
  75. },
  76. whilePlaying: function () {
  77. this.setProgressPointer();
  78. this.setVolumePointer();
  79. this.setTimes();
  80. },
  81. onFinish: function () {
  82. this.dom.play.removeClass('fa-pause').addClass('fa-play');
  83. this.sound.setPosition(0);
  84. this.setProgressPointer();
  85. this.setTimes();
  86. },
  87. setProgressPointer: function () {
  88. var ratio = this.sound.position / this.sound.durationEstimate;
  89. var offset = ratio * this.dom.progress.width();
  90. var radius = this.dom.progressPointer.width() / 2;
  91. var margin = offset - radius;
  92. this.dom.progressPointer.css('margin-left', margin);
  93. },
  94. setVolumePointer: function () {
  95. var ratio = this.sound.volume / 100;
  96. var offset = ratio * this.dom.volume.width();
  97. var radius = this.dom.volumePointer.width() / 2;
  98. var margin = offset - radius;
  99. this.dom.volumePointer.css('margin-left', margin);
  100. },
  101. setTimes: function () {
  102. var addZeros = function (n, w) {
  103. w -= n.toString().length;
  104. if (w > 0) return new Array(w + 1).join('0') + n;
  105. return n + '';
  106. };
  107. var timeLeft = this.sound.position;
  108. var sec = Math.floor(timeLeft / 1000) % 60;
  109. var min = Math.floor(timeLeft / 1000 / 60);
  110. this.dom.timeLeft.text(addZeros(min, 2) + ' : ' + addZeros(sec, 2));
  111. var timeRight = this.sound.durationEstimate - this.sound.position;
  112. var sec = Math.floor(timeRight / 1000) % 60;
  113. var min = Math.floor(timeRight / 1000 / 60);
  114. this.dom.timeRight.text(addZeros(min, 2) + ' : ' + addZeros(sec, 2));
  115. }
  116. };
  117. $.fn.player = function (url) {
  118. new Player(this, url);
  119. };
  120. })(jQuery, soundManager);