Player.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <f7-toolbar
  3. style="position: fixed; height: 80px"
  4. bottom
  5. v-if="status != 'empty'"
  6. >
  7. <!--
  8. il player avra' due stati aperto e chiuso? non so quanto mi piace, mi sembra che
  9. le informazioni utili possano stare benissimo in questa toolbar.
  10. nel caso cmq bisogna fare una classe css per questi stati con l'altezza che varia.
  11. -->
  12. <f7-block>
  13. <f7-icon
  14. class="cursor-pointer"
  15. to="#"
  16. size="52px"
  17. f7="play_circle"
  18. @click="onPlay"
  19. v-if="!loading && status == 'pause'"
  20. />
  21. <f7-icon
  22. class="cursor-pointer"
  23. size="52px"
  24. f7="pause_circle"
  25. @click="onPause"
  26. v-if="!loading && status == 'playing'"
  27. />
  28. <f7-preloader size="42px" color="white" v-if="loading" />
  29. </f7-block>
  30. <f7-block>
  31. <f7-link v-if="!loading">{{ title }}</f7-link>
  32. </f7-block>
  33. </f7-toolbar>
  34. </template>
  35. <script>
  36. import eventBus from "../js/eventBus";
  37. import { Howl } from "howler";
  38. export default {
  39. components: {},
  40. data() {
  41. return {
  42. loading: true,
  43. status: "empty",
  44. volume: 100,
  45. title: "RadioBlackout > DIRETTA",
  46. subtitle: "",
  47. live: true,
  48. player: null,
  49. };
  50. },
  51. destroyed() {
  52. eventBus.$off("play:now");
  53. },
  54. mounted() {
  55. eventBus.$on("play:now", (url, metadata) => {
  56. this.play(url, metadata);
  57. });
  58. eventBus.$on("play:pause", () => {
  59. if (this.player) {
  60. this.player.pause();
  61. }
  62. });
  63. eventBus.$on("play:resume", () => {
  64. if (this.player) {
  65. this.player.play();
  66. }
  67. });
  68. },
  69. methods: {
  70. onPlay() {
  71. eventBus.$emit("play:resume");
  72. },
  73. onPause() {
  74. eventBus.$emit("play:pause");
  75. },
  76. updateStatusFromPlayer() {
  77. this.status = this.player.playing() ? "playing" : "pause";
  78. },
  79. play(url, metadata) {
  80. this.loading = true;
  81. let newPlayer = new Howl({
  82. autoSuspend: false,
  83. html5: true,
  84. preload: true,
  85. src: Array.isArray(url) ? url : [url],
  86. });
  87. for (const eventName of ["pause", "play", "end"]) {
  88. newPlayer.on(eventName, this.updateStatusFromPlayer);
  89. }
  90. newPlayer.once("play", () => {
  91. if (this.player) {
  92. this.player.stop();
  93. this.player.unload();
  94. }
  95. this.player = newPlayer;
  96. if (metadata !== undefined) {
  97. this.title = metadata.title;
  98. this.live = metadata.live;
  99. }
  100. this.loading = false;
  101. this.updateStatusFromPlayer();
  102. });
  103. newPlayer.play();
  104. },
  105. stop() {
  106. if (this.player) {
  107. this.player.stop();
  108. }
  109. this.$refs.button.stop();
  110. },
  111. },
  112. };
  113. </script>