|
@@ -1,12 +1,10 @@
|
|
import { ref, watch } from "vue";
|
|
import { ref, watch } from "vue";
|
|
-
|
|
|
|
-const debug = (...args) =>
|
|
|
|
- process.env.NODE_ENV === "development" && console.debug("[USP]", ...args);
|
|
|
|
|
|
+import utils from "@/utils";
|
|
|
|
|
|
const soundcloudDomain = "https://w.soundcloud.com";
|
|
const soundcloudDomain = "https://w.soundcloud.com";
|
|
|
|
|
|
export const useSoundcloudPlayer = () => {
|
|
export const useSoundcloudPlayer = () => {
|
|
- debug("Init start");
|
|
|
|
|
|
+ const uuid = utils.guid();
|
|
|
|
|
|
const soundcloudIframeElement = ref();
|
|
const soundcloudIframeElement = ref();
|
|
const widgetId = ref();
|
|
const widgetId = ref();
|
|
@@ -15,6 +13,8 @@ export const useSoundcloudPlayer = () => {
|
|
const attemptsToPlay = ref(0);
|
|
const attemptsToPlay = ref(0);
|
|
const debouncePause = ref(null);
|
|
const debouncePause = ref(null);
|
|
|
|
|
|
|
|
+ const iframeUrl = ref("");
|
|
|
|
+
|
|
const playAttemptTimeout = ref();
|
|
const playAttemptTimeout = ref();
|
|
|
|
|
|
const paused = ref(true);
|
|
const paused = ref(true);
|
|
@@ -24,6 +24,15 @@ export const useSoundcloudPlayer = () => {
|
|
const eventListenerCallbacks = {};
|
|
const eventListenerCallbacks = {};
|
|
const stateChangeCallbacks = [];
|
|
const stateChangeCallbacks = [];
|
|
|
|
|
|
|
|
+ const debug = (...args) =>
|
|
|
|
+ process.env.NODE_ENV === "development" &&
|
|
|
|
+ console.debug("[USP]", uuid, widgetId.value, ...args);
|
|
|
|
+
|
|
|
|
+ debug("Init start");
|
|
|
|
+
|
|
|
|
+ if (!window.soundcloudIframeLockUuids)
|
|
|
|
+ window.soundcloudIframeLockUuids = new Set();
|
|
|
|
+
|
|
/*
|
|
/*
|
|
EVENTS:
|
|
EVENTS:
|
|
LOAD_PROGRESS: "loadProgress",
|
|
LOAD_PROGRESS: "loadProgress",
|
|
@@ -90,10 +99,16 @@ export const useSoundcloudPlayer = () => {
|
|
if (event.origin !== soundcloudDomain) return;
|
|
if (event.origin !== soundcloudDomain) return;
|
|
|
|
|
|
const data = JSON.parse(event.data);
|
|
const data = JSON.parse(event.data);
|
|
- if (data.method !== "getPosition" && data.method !== "getDuration")
|
|
|
|
|
|
+ if (
|
|
|
|
+ data.method !== "getPosition" &&
|
|
|
|
+ data.method !== "getDuration" &&
|
|
|
|
+ (data.method === "ready" || data.widgetId === widgetId.value)
|
|
|
|
+ )
|
|
debug("MESSAGE DATA", data);
|
|
debug("MESSAGE DATA", data);
|
|
|
|
|
|
if (data.method === "ready") {
|
|
if (data.method === "ready") {
|
|
|
|
+ if (window.soundcloudIframeLockUuid !== uuid) return;
|
|
|
|
+
|
|
widgetId.value = data.widgetId;
|
|
widgetId.value = data.widgetId;
|
|
|
|
|
|
if (readyCallback.value) readyCallback.value();
|
|
if (readyCallback.value) readyCallback.value();
|
|
@@ -103,9 +118,14 @@ export const useSoundcloudPlayer = () => {
|
|
callback(data.value);
|
|
callback(data.value);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ window.soundcloudIframeLockUuid = null;
|
|
|
|
+ document.dispatchEvent(new Event("soundcloudUnlock"));
|
|
|
|
+
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (data.widgetId !== widgetId.value) return;
|
|
|
|
+
|
|
if (methodCallbacks[data.method]) {
|
|
if (methodCallbacks[data.method]) {
|
|
methodCallbacks[data.method].forEach(callback => {
|
|
methodCallbacks[data.method].forEach(callback => {
|
|
callback(data.value);
|
|
callback(data.value);
|
|
@@ -221,18 +241,59 @@ export const useSoundcloudPlayer = () => {
|
|
}, 500);
|
|
}, 500);
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ const changeIframeUrl = url => {
|
|
|
|
+ iframeUrl.value = url;
|
|
|
|
+ if (!widgetId.value && window.soundcloudIframeLockUuid !== uuid) return; // Don't change the iframe src if the player hasn't initialized and isn't allowed to initialize yet
|
|
|
|
+ soundcloudIframeElement.value.setAttribute("src", url);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const documentUnlockEventListener = () => {
|
|
|
|
+ if (
|
|
|
|
+ !window.soundcloudIframeLockUuid &&
|
|
|
|
+ window.soundcloudIframeLockUuids.size > 0 &&
|
|
|
|
+ window.soundcloudIframeLockUuids.keys().next().value === uuid
|
|
|
|
+ ) {
|
|
|
|
+ window.soundcloudIframeLockUuid = uuid;
|
|
|
|
+ window.soundcloudIframeLockUuids.delete(uuid);
|
|
|
|
+ changeIframeUrl(iframeUrl.value);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
watch(soundcloudIframeElement, (newElement, oldElement) => {
|
|
watch(soundcloudIframeElement, (newElement, oldElement) => {
|
|
if (oldElement) {
|
|
if (oldElement) {
|
|
oldElement.removeEventListener("load", onLoadListener);
|
|
oldElement.removeEventListener("load", onLoadListener);
|
|
|
|
|
|
window.removeEventListener("message", onMessageListener);
|
|
window.removeEventListener("message", onMessageListener);
|
|
|
|
+
|
|
|
|
+ if (window.soundcloudIframeLockUuid === uuid)
|
|
|
|
+ window.soundcloudIframeLockUuid = null;
|
|
|
|
+ window.soundcloudIframeLockUuids.delete(uuid);
|
|
|
|
+
|
|
|
|
+ document.removeEventListener(
|
|
|
|
+ "soundcloudUnlock",
|
|
|
|
+ documentUnlockEventListener
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
if (newElement) {
|
|
if (newElement) {
|
|
newElement.addEventListener("load", onLoadListener);
|
|
newElement.addEventListener("load", onLoadListener);
|
|
|
|
|
|
window.addEventListener("message", onMessageListener);
|
|
window.addEventListener("message", onMessageListener);
|
|
|
|
+
|
|
|
|
+ window.soundcloudIframeLockUuids.add(uuid);
|
|
|
|
+
|
|
|
|
+ document.removeEventListener(
|
|
|
|
+ "soundcloudUnlock",
|
|
|
|
+ documentUnlockEventListener
|
|
|
|
+ );
|
|
|
|
+ document.addEventListener(
|
|
|
|
+ "soundcloudUnlock",
|
|
|
|
+ documentUnlockEventListener
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (!window.soundcloudIframeLockUuid)
|
|
|
|
+ document.dispatchEvent(new Event("soundcloudUnlock"));
|
|
});
|
|
});
|
|
|
|
|
|
/* Exported functions */
|
|
/* Exported functions */
|
|
@@ -311,8 +372,7 @@ export const useSoundcloudPlayer = () => {
|
|
debug("Soundcloud load track");
|
|
debug("Soundcloud load track");
|
|
|
|
|
|
const url = `${soundcloudDomain}/player?autoplay=false&buying=false&sharing=false&download=false&show_artwork=false&show_playcount=false&show_user=false&url=${`https://api.soundcloud.com/tracks/${trackId}`}`;
|
|
const url = `${soundcloudDomain}/player?autoplay=false&buying=false&sharing=false&download=false&show_artwork=false&show_playcount=false&show_user=false&url=${`https://api.soundcloud.com/tracks/${trackId}`}`;
|
|
-
|
|
|
|
- soundcloudIframeElement.value.setAttribute("src", url);
|
|
|
|
|
|
+ changeIframeUrl(url);
|
|
|
|
|
|
paused.value = _paused;
|
|
paused.value = _paused;
|
|
currentTrackId.value = trackId;
|
|
currentTrackId.value = trackId;
|
|
@@ -350,7 +410,7 @@ export const useSoundcloudPlayer = () => {
|
|
if (!soundcloudIframeElement.value) return;
|
|
if (!soundcloudIframeElement.value) return;
|
|
|
|
|
|
const url = `${soundcloudDomain}/player?autoplay=false&buying=false&sharing=false&download=false&show_artwork=false&show_playcount=false&show_user=false&url=${`https://api.soundcloud.com/tracks/${0}`}`;
|
|
const url = `${soundcloudDomain}/player?autoplay=false&buying=false&sharing=false&download=false&show_artwork=false&show_playcount=false&show_user=false&url=${`https://api.soundcloud.com/tracks/${0}`}`;
|
|
- soundcloudIframeElement.value.setAttribute("src", url);
|
|
|
|
|
|
+ changeIframeUrl(url);
|
|
|
|
|
|
currentTrackId.value = null;
|
|
currentTrackId.value = null;
|
|
|
|
|