AudioPlayer.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. class AudioPlayer extends GameObject
  22. {
  23. constructor(name, p5Audio = null)
  24. {
  25. super(name);
  26. this.p5Audio = p5Audio;
  27. }
  28. setP5Audio(p5Audio)
  29. {
  30. if (this.p5Audio) this.p5Audio.stop();
  31. this.p5Audio = p5Audio;
  32. }
  33. getP5Audio()
  34. {
  35. return this.p5Audio;
  36. }
  37. play()
  38. {
  39. if (this.p5Audio) this.p5Audio.play();
  40. }
  41. stop()
  42. {
  43. if (this.p5Audio) this.p5Audio.stop();
  44. }
  45. // TODO: This don't worky, make it worky
  46. // Something to do with new browser audio policy
  47. autoplay()
  48. {
  49. if (this.p5Audio) this.p5Audio.autoplay(true);
  50. }
  51. }