AudioPlayer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /************************************************************************
  2. * AudioPlayer.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * The {@code AudioPlayer} class represents a GameObject that can playa p5.Audio
  23. * loaded from the AssetHandler.
  24. *
  25. * @author Pedro Schneider
  26. *
  27. * @class
  28. */
  29. class AudioPlayer extends GameObject
  30. {
  31. /**
  32. * @constructor
  33. * Initializes an AudioPlayer GameObject with the given parameters.
  34. *
  35. * @param {String} name name for the AudioPlayer GameObject.
  36. * @param {p5.Audio} p5Audio p5.Audio this AudioPlayer will play from.
  37. */
  38. constructor(name, p5Audio = null)
  39. {
  40. super(name);
  41. this.p5Audio = p5Audio;
  42. }
  43. /**
  44. * Overrides AudioPlayer's p5Audio.
  45. *
  46. * @param {p5.Audio} p5Audio new p5.Audio for this AudioPlayer to play from.
  47. */
  48. setP5Audio(p5Audio)
  49. {
  50. if (this.p5Audio) this.p5Audio.stop();
  51. this.p5Audio = p5Audio;
  52. }
  53. /**
  54. * Returns this AudioPlayer's p5Audio.
  55. *
  56. * @returns {p5.Audio} this AudioPlayer's p5Audio.
  57. */
  58. getP5Audio()
  59. {
  60. return this.p5Audio;
  61. }
  62. /**
  63. * Starts playing this AudioPlayer's p5Audio.
  64. */
  65. play()
  66. {
  67. if (this.p5Audio) this.p5Audio.play();
  68. }
  69. /**
  70. * Stops playing this AudioPlayer's p5Audio.
  71. */
  72. stop()
  73. {
  74. if (this.p5Audio) this.p5Audio.stop();
  75. }
  76. // TODO: This don't worky, make it worky.
  77. // Something to do with new browser audio policy.
  78. /**
  79. * Sets this AudioPlayer's autoplay falg to true, so it starts whenever
  80. * it is ready.
  81. */
  82. autoplay()
  83. {
  84. if (this.p5Audio) this.p5Audio.autoplay(true);
  85. }
  86. }