SoundcloudPlayer.vue 14 KB

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