AudioPlayer.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  26. * ! of another GameObject on the tree or as a root).
  27. *
  28. * @author Pedro Schneider
  29. *
  30. * @class
  31. */
  32. class AudioPlayer extends GameObject
  33. {
  34. /**
  35. * Initializes an AudioPlayer GameObject with the given parameters.
  36. *
  37. * @param {String} name name for the AudioPlayer GameObject.
  38. * @param {p5.Audio} p5Audio p5.Audio this AudioPlayer will play from.
  39. *
  40. * @constructor
  41. */
  42. constructor(name, p5Audio = null)
  43. {
  44. super(name);
  45. this.p5Audio = p5Audio;
  46. }
  47. /**
  48. * Overrides AudioPlayer's p5Audio.
  49. *
  50. * @param {p5.Audio} p5Audio new p5.Audio for this AudioPlayer to play from.
  51. */
  52. setP5Audio(p5Audio)
  53. {
  54. if (this.p5Audio) this.p5Audio.stop();
  55. this.p5Audio = p5Audio;
  56. }
  57. /**
  58. * Returns this AudioPlayer's p5Audio.
  59. *
  60. * @returns {p5.Audio} this AudioPlayer's p5Audio.
  61. */
  62. getP5Audio()
  63. {
  64. return this.p5Audio;
  65. }
  66. /**
  67. * Starts playing this AudioPlayer's p5Audio.
  68. */
  69. play()
  70. {
  71. if (this.p5Audio) this.p5Audio.play();
  72. }
  73. /**
  74. * Stops playing this AudioPlayer's p5Audio.
  75. */
  76. stop()
  77. {
  78. if (this.p5Audio) this.p5Audio.stop();
  79. }
  80. // TODO: This don't worky, make it worky.
  81. // Something to do with new browser audio policy.
  82. /**
  83. * Sets this AudioPlayer's autoplay falg to true, so it starts whenever
  84. * it is ready.
  85. */
  86. autoplay()
  87. {
  88. if (this.p5Audio) this.p5Audio.autoplay(true);
  89. }
  90. }