AudioController.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // import Howl from '../../3rd_party/howler';
  2. const audioControllerFactory = (function () {
  3. function AudioController(audioFactory) {
  4. this.audios = [];
  5. this.audioFactory = audioFactory;
  6. this._volume = 1;
  7. this._isMuted = false;
  8. }
  9. AudioController.prototype = {
  10. addAudio: function (audio) {
  11. this.audios.push(audio);
  12. },
  13. pause: function () {
  14. var i;
  15. var len = this.audios.length;
  16. for (i = 0; i < len; i += 1) {
  17. this.audios[i].pause();
  18. }
  19. },
  20. resume: function () {
  21. var i;
  22. var len = this.audios.length;
  23. for (i = 0; i < len; i += 1) {
  24. this.audios[i].resume();
  25. }
  26. },
  27. setRate: function (rateValue) {
  28. var i;
  29. var len = this.audios.length;
  30. for (i = 0; i < len; i += 1) {
  31. this.audios[i].setRate(rateValue);
  32. }
  33. },
  34. createAudio: function (assetPath) {
  35. if (this.audioFactory) {
  36. return this.audioFactory(assetPath);
  37. } if (window.Howl) {
  38. return new window.Howl({
  39. src: [assetPath],
  40. });
  41. }
  42. return {
  43. isPlaying: false,
  44. play: function () { this.isPlaying = true; },
  45. seek: function () { this.isPlaying = false; },
  46. playing: function () {},
  47. rate: function () {},
  48. setVolume: function () {},
  49. };
  50. },
  51. setAudioFactory: function (audioFactory) {
  52. this.audioFactory = audioFactory;
  53. },
  54. setVolume: function (value) {
  55. this._volume = value;
  56. this._updateVolume();
  57. },
  58. mute: function () {
  59. this._isMuted = true;
  60. this._updateVolume();
  61. },
  62. unmute: function () {
  63. this._isMuted = false;
  64. this._updateVolume();
  65. },
  66. getVolume: function () {
  67. return this._volume;
  68. },
  69. _updateVolume: function () {
  70. var i;
  71. var len = this.audios.length;
  72. for (i = 0; i < len; i += 1) {
  73. this.audios[i].volume(this._volume * (this._isMuted ? 0 : 1));
  74. }
  75. },
  76. };
  77. return function () {
  78. return new AudioController();
  79. };
  80. }());
  81. export default audioControllerFactory;