SoundcloudPlayer.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <script setup lang="ts">
  2. import { computed, onBeforeUnmount, onMounted, ref } from "vue";
  3. import Toast from "toasters";
  4. import { useSoundcloudPlayer } from "@/composables/useSoundcloudPlayer";
  5. import { useStationStore } from "@/stores/station";
  6. import aw from "@/aw";
  7. const props = defineProps<{
  8. song: {
  9. mediaSource: string;
  10. title: string;
  11. artists: string[];
  12. duration: number;
  13. };
  14. }>();
  15. const TAG = "[SP]";
  16. const {
  17. soundcloudIframeElement: playerElement,
  18. soundcloudGetDuration,
  19. soundcloudLoadTrack,
  20. soundcloudSetVolume,
  21. soundcloudPlay,
  22. soundcloudPause,
  23. soundcloudSeekTo,
  24. soundcloudOnTrackStateChange,
  25. soundcloudBindListener,
  26. soundcloudGetPosition,
  27. soundcloudGetCurrentSound,
  28. soundcloudGetTrackState,
  29. soundcloudUnload
  30. } = useSoundcloudPlayer();
  31. const stationStore = useStationStore();
  32. const { updateMediaModalPlayingAudio } = stationStore;
  33. const interval = ref(null);
  34. const durationCanvas = ref(null);
  35. const activityWatchMediaDataInterval = ref(null);
  36. const activityWatchMediaLastStatus = ref("");
  37. const activityWatchMediaLastStartDuration = ref(0);
  38. const canvasWidth = ref(760);
  39. const player = ref<{
  40. error: boolean;
  41. errorMessage: string;
  42. paused: boolean;
  43. currentTime: number;
  44. duration: number;
  45. muted: boolean;
  46. volume: number;
  47. }>({
  48. error: false,
  49. errorMessage: "",
  50. paused: true,
  51. currentTime: 0,
  52. duration: 0,
  53. muted: false,
  54. volume: 20
  55. });
  56. const playerVolumeControlIcon = computed(() => {
  57. const { muted, volume } = player.value;
  58. if (muted) return "volume_mute";
  59. if (volume >= 50) return "volume_up";
  60. return "volume_down";
  61. });
  62. const soundcloudTrackId = computed(() => props.song.mediaSource.split(":")[1]);
  63. const playerPlay = () => {
  64. console.debug(TAG, "PLAYER PLAY");
  65. soundcloudPlay();
  66. };
  67. const playerPause = () => {
  68. console.debug(TAG, "PLAYER PAUSE");
  69. soundcloudPause();
  70. };
  71. const playerStop = () => {
  72. console.debug(TAG, "PLAYER STOP");
  73. soundcloudPause();
  74. soundcloudSeekTo(0);
  75. };
  76. const playerHardStop = () => {
  77. console.debug(TAG, "PLAYER HARD STOP");
  78. playerStop();
  79. };
  80. const playerSetTrackPosition = event => {
  81. console.debug(TAG, "PLAYER SET TRACK POSITION");
  82. playerPlay();
  83. soundcloudGetDuration(duration => {
  84. soundcloudSeekTo(
  85. Number(
  86. Number(duration / 1000) *
  87. ((event.pageX - event.target.getBoundingClientRect().left) /
  88. canvasWidth.value)
  89. ) * 1000
  90. );
  91. });
  92. };
  93. const playerToggleMute = () => {
  94. console.debug(TAG, "PLAYER TOGGLE MUTE");
  95. player.value.muted = !player.value.muted;
  96. const { muted, volume } = player.value;
  97. localStorage.setItem("muted", `${muted}`);
  98. if (muted) {
  99. soundcloudSetVolume(0);
  100. player.value.volume = 0;
  101. } else if (volume > 0) {
  102. soundcloudSetVolume(volume);
  103. player.value.volume = volume;
  104. localStorage.setItem("volume", `${volume}`);
  105. } else {
  106. soundcloudSetVolume(20);
  107. player.value.volume = 20;
  108. localStorage.setItem("volume", `${20}`);
  109. }
  110. };
  111. const playerChangeVolume = () => {
  112. console.debug(TAG, "PLAYER CHANGE VOLUME");
  113. const { muted, volume } = player.value;
  114. localStorage.setItem("volume", `${volume}`);
  115. soundcloudSetVolume(volume);
  116. if (muted && volume > 0) {
  117. player.value.muted = false;
  118. localStorage.setItem("muted", `${false}`);
  119. } else if (!muted && volume === 0) {
  120. player.value.muted = true;
  121. localStorage.setItem("muted", `${true}`);
  122. }
  123. };
  124. const drawCanvas = () => {
  125. const canvasElement = durationCanvas.value;
  126. if (!canvasElement) return;
  127. const ctx = canvasElement.getContext("2d");
  128. const videoDuration = Number(player.value.duration);
  129. const _duration = Number(player.value.duration);
  130. const afterDuration = videoDuration - _duration;
  131. canvasWidth.value = Math.min(document.body.clientWidth - 40, 760);
  132. const width = canvasWidth.value;
  133. const { currentTime } = player.value;
  134. const widthDuration = (_duration / videoDuration) * width;
  135. const widthAfterDuration = (afterDuration / videoDuration) * width;
  136. const widthCurrentTime = (currentTime / videoDuration) * width;
  137. const durationColor = "#03A9F4";
  138. const afterDurationColor = "#41E841";
  139. const currentDurationColor = "#3b25e8";
  140. ctx.fillStyle = durationColor;
  141. ctx.fillRect(0, 0, widthDuration, 20);
  142. ctx.fillStyle = afterDurationColor;
  143. ctx.fillRect(widthDuration, 0, widthAfterDuration, 20);
  144. ctx.fillStyle = currentDurationColor;
  145. ctx.fillRect(widthCurrentTime, 0, 1, 20);
  146. };
  147. const formatDuration = duration => duration.toFixed(3);
  148. const sendActivityWatchMediaData = () => {
  149. if (!player.value.paused && soundcloudGetTrackState() === "playing") {
  150. if (activityWatchMediaLastStatus.value !== "playing") {
  151. activityWatchMediaLastStatus.value = "playing";
  152. soundcloudGetPosition(position => {
  153. activityWatchMediaLastStartDuration.value = Math.floor(
  154. Number(position / 1000)
  155. );
  156. });
  157. }
  158. const videoData = {
  159. title: props.song.title,
  160. artists: props.song.artists?.join(", ") || "",
  161. mediaSource: props.song.mediaSource,
  162. muted: player.value.muted,
  163. volume: player.value.volume,
  164. startedDuration:
  165. activityWatchMediaLastStartDuration.value <= 0
  166. ? 0
  167. : activityWatchMediaLastStartDuration.value,
  168. source: `viewMedia#${props.song.mediaSource}`,
  169. hostname: window.location.hostname,
  170. playerState: "",
  171. playbackRate: 1
  172. };
  173. aw.sendMediaData(videoData);
  174. } else {
  175. activityWatchMediaLastStatus.value = "not_playing";
  176. }
  177. };
  178. onMounted(() => {
  179. console.debug(TAG, "ON MOUNTED");
  180. // Generic
  181. let volume = parseFloat(localStorage.getItem("volume"));
  182. volume = typeof volume === "number" && !Number.isNaN(volume) ? volume : 20;
  183. localStorage.setItem("volume", `${volume}`);
  184. player.value.volume = volume;
  185. let muted: boolean | string = localStorage.getItem("muted");
  186. muted = muted === "true";
  187. localStorage.setItem("muted", `${muted}`);
  188. player.value.muted = muted;
  189. if (muted) player.value.volume = 0;
  190. soundcloudSetVolume(volume);
  191. // SoundCloud specific
  192. soundcloudBindListener("ready", value => {
  193. console.debug(TAG, "Bind on ready", value);
  194. soundcloudGetCurrentSound(sound => {
  195. player.value.duration = sound.duration / 1000;
  196. });
  197. soundcloudOnTrackStateChange(newState => {
  198. console.debug(TAG, `New state: ${newState}`);
  199. const { paused } = player.value;
  200. if (
  201. newState === "attempting_to_play" ||
  202. newState === "failed_to_play"
  203. ) {
  204. if (!paused) {
  205. if (newState === "failed_to_play")
  206. new Toast(
  207. "Failed to start SoundCloud player. Please try to manually start it."
  208. );
  209. else if (newState === "sound_unavailable")
  210. new Toast("Sound is currently unavailable.");
  211. player.value.paused = true;
  212. }
  213. } else if (newState === "paused") {
  214. player.value.paused = true;
  215. } else if (newState === "playing") {
  216. player.value.paused = false;
  217. } else if (newState === "finished") {
  218. player.value.paused = true;
  219. } else if (newState === "error") {
  220. player.value.paused = true;
  221. }
  222. if (player.value.paused) updateMediaModalPlayingAudio(false);
  223. else updateMediaModalPlayingAudio(true);
  224. });
  225. soundcloudBindListener("seek", () => {
  226. console.debug(TAG, "Bind on seek");
  227. });
  228. soundcloudBindListener("error", value => {
  229. console.debug(TAG, "Bind on error", value);
  230. });
  231. });
  232. soundcloudLoadTrack(soundcloudTrackId.value, 0, true);
  233. interval.value = setInterval(() => {
  234. soundcloudGetPosition(position => {
  235. player.value.currentTime = position / 1000;
  236. drawCanvas();
  237. });
  238. }, 200);
  239. activityWatchMediaDataInterval.value = setInterval(() => {
  240. sendActivityWatchMediaData();
  241. }, 1000);
  242. });
  243. onBeforeUnmount(() => {
  244. clearInterval(interval.value);
  245. clearInterval(activityWatchMediaDataInterval.value);
  246. updateMediaModalPlayingAudio(false);
  247. soundcloudUnload();
  248. });
  249. </script>
  250. <template>
  251. <div class="player-section">
  252. <div class="player-container">
  253. <iframe
  254. ref="playerElement"
  255. style="width: 100%; height: 100%; min-height: 426px"
  256. scrolling="no"
  257. frameborder="no"
  258. allow="autoplay"
  259. ></iframe>
  260. </div>
  261. <div v-show="player.error" class="player-error">
  262. <h2>{{ player.errorMessage }}</h2>
  263. </div>
  264. <canvas
  265. ref="durationCanvas"
  266. class="duration-canvas"
  267. v-show="!player.error"
  268. height="20"
  269. :width="canvasWidth"
  270. @click="playerSetTrackPosition($event)"
  271. ></canvas>
  272. <div class="player-footer">
  273. <div class="player-footer-left">
  274. <button
  275. v-if="player.paused"
  276. class="button is-primary"
  277. @click="playerPlay()"
  278. @keyup.enter="playerPlay()"
  279. content="Resume Playback"
  280. v-tippy
  281. >
  282. <i class="material-icons">play_arrow</i>
  283. </button>
  284. <button
  285. v-else
  286. class="button is-primary"
  287. @click="playerPause()"
  288. @keyup.enter="playerPause()"
  289. content="Pause Playback"
  290. v-tippy
  291. >
  292. <i class="material-icons">pause</i>
  293. </button>
  294. <button
  295. class="button is-danger"
  296. @click.exact="playerStop()"
  297. @click.shift="playerHardStop()"
  298. @keyup.enter.exact="playerStop()"
  299. @keyup.shift.enter="playerHardStop()"
  300. content="Stop Playback"
  301. v-tippy
  302. >
  303. <i class="material-icons">stop</i>
  304. </button>
  305. </div>
  306. <div class="player-footer-center">
  307. <span>
  308. <span>
  309. {{ formatDuration(player.currentTime) }}
  310. </span>
  311. /
  312. <span>
  313. {{ formatDuration(player.duration) }}
  314. </span>
  315. </span>
  316. </div>
  317. <div class="player-footer-right">
  318. <p id="volume-control">
  319. <i
  320. class="material-icons"
  321. @click="playerToggleMute()"
  322. :content="`${player.muted ? 'Unmute' : 'Mute'}`"
  323. v-tippy
  324. >{{ playerVolumeControlIcon }}</i
  325. >
  326. <input
  327. v-model.number="player.volume"
  328. type="range"
  329. min="0"
  330. max="100"
  331. class="volume-slider active"
  332. @change="playerChangeVolume()"
  333. @input="playerChangeVolume()"
  334. />
  335. </p>
  336. </div>
  337. </div>
  338. </div>
  339. </template>
  340. <style lang="less" scoped>
  341. .night-mode {
  342. .player-section {
  343. background-color: var(--dark-grey-3) !important;
  344. border: 0 !important;
  345. .duration-canvas {
  346. background-color: var(--dark-grey-2) !important;
  347. }
  348. }
  349. }
  350. .player-section {
  351. display: flex;
  352. flex-direction: column;
  353. margin: 10px auto 0 auto;
  354. border: 1px solid var(--light-grey-3);
  355. border-radius: @border-radius;
  356. overflow: hidden;
  357. .player-container {
  358. position: relative;
  359. padding-bottom: 56.25%; /* proportion value to aspect ratio 16:9 (9 / 16 = 0.5625 or 56.25%) */
  360. height: 0;
  361. overflow: hidden;
  362. :deep(iframe) {
  363. position: absolute;
  364. top: 0;
  365. left: 0;
  366. width: 100%;
  367. height: 100%;
  368. min-height: 426px;
  369. }
  370. }
  371. .duration-canvas {
  372. background-color: var(--light-grey-2);
  373. }
  374. .player-error {
  375. display: flex;
  376. height: 428px;
  377. align-items: center;
  378. * {
  379. margin: 0;
  380. flex: 1;
  381. font-size: 30px;
  382. text-align: center;
  383. }
  384. }
  385. .player-footer {
  386. display: flex;
  387. justify-content: space-between;
  388. height: 54px;
  389. padding-left: 10px;
  390. padding-right: 10px;
  391. > * {
  392. width: 33.3%;
  393. display: flex;
  394. align-items: center;
  395. }
  396. .player-footer-left {
  397. flex: 1;
  398. & > .button:not(:first-child) {
  399. margin-left: 5px;
  400. }
  401. & > .playerRateDropdown {
  402. margin-left: 5px;
  403. margin-bottom: unset !important;
  404. .control.has-addons {
  405. margin-bottom: unset !important;
  406. & > .button {
  407. font-size: 24px;
  408. }
  409. }
  410. }
  411. :deep(.tippy-box[data-theme~="dropdown"]) {
  412. max-width: 100px !important;
  413. .nav-dropdown-items .nav-item {
  414. justify-content: center !important;
  415. border-radius: @border-radius !important;
  416. &.active {
  417. background-color: var(--primary-color);
  418. color: var(--white);
  419. }
  420. }
  421. }
  422. }
  423. .player-footer-center {
  424. justify-content: center;
  425. align-items: center;
  426. flex: 2;
  427. font-size: 18px;
  428. font-weight: 400;
  429. width: 200px;
  430. margin: 0 5px;
  431. img {
  432. height: 21px;
  433. margin-right: 12px;
  434. filter: invert(26%) sepia(54%) saturate(6317%) hue-rotate(2deg)
  435. brightness(92%) contrast(115%);
  436. }
  437. }
  438. .player-footer-right {
  439. justify-content: right;
  440. flex: 1;
  441. #volume-control {
  442. margin: 3px;
  443. margin-top: 0;
  444. display: flex;
  445. align-items: center;
  446. cursor: pointer;
  447. .volume-slider {
  448. width: 100%;
  449. padding: 0 15px;
  450. background: transparent;
  451. min-width: 100px;
  452. }
  453. input[type="range"] {
  454. -webkit-appearance: none;
  455. margin: 7.3px 0;
  456. }
  457. input[type="range"]:focus {
  458. outline: none;
  459. }
  460. input[type="range"]::-webkit-slider-runnable-track {
  461. width: 100%;
  462. height: 5.2px;
  463. cursor: pointer;
  464. box-shadow: 0;
  465. background: var(--light-grey-3);
  466. border-radius: @border-radius;
  467. border: 0;
  468. }
  469. input[type="range"]::-webkit-slider-thumb {
  470. box-shadow: 0;
  471. border: 0;
  472. height: 19px;
  473. width: 19px;
  474. border-radius: 100%;
  475. background: var(--primary-color);
  476. cursor: pointer;
  477. -webkit-appearance: none;
  478. margin-top: -6.5px;
  479. }
  480. input[type="range"]::-moz-range-track {
  481. width: 100%;
  482. height: 5.2px;
  483. cursor: pointer;
  484. box-shadow: 0;
  485. background: var(--light-grey-3);
  486. border-radius: @border-radius;
  487. border: 0;
  488. }
  489. input[type="range"]::-moz-range-thumb {
  490. box-shadow: 0;
  491. border: 0;
  492. height: 19px;
  493. width: 19px;
  494. border-radius: 100%;
  495. background: var(--primary-color);
  496. cursor: pointer;
  497. -webkit-appearance: none;
  498. margin-top: -6.5px;
  499. }
  500. input[type="range"]::-ms-track {
  501. width: 100%;
  502. height: 5.2px;
  503. cursor: pointer;
  504. box-shadow: 0;
  505. background: var(--light-grey-3);
  506. border-radius: @border-radius;
  507. }
  508. input[type="range"]::-ms-fill-lower {
  509. background: var(--light-grey-3);
  510. border: 0;
  511. border-radius: 0;
  512. box-shadow: 0;
  513. }
  514. input[type="range"]::-ms-fill-upper {
  515. background: var(--light-grey-3);
  516. border: 0;
  517. border-radius: 0;
  518. box-shadow: 0;
  519. }
  520. input[type="range"]::-ms-thumb {
  521. box-shadow: 0;
  522. border: 0;
  523. height: 15px;
  524. width: 15px;
  525. border-radius: 100%;
  526. background: var(--primary-color);
  527. cursor: pointer;
  528. -webkit-appearance: none;
  529. margin-top: 1.5px;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. </style>