Sfoglia il codice sorgente

feat(useSoundcloudPlayer): sound_unavailable track state

Owen Diffey 1 anno fa
parent
commit
3397058344

+ 2 - 0
frontend/src/components/SoundcloudPlayer.vue

@@ -255,6 +255,8 @@ onMounted(() => {
 						new Toast(
 							"Failed to start SoundCloud player. Please try to manually start it."
 						);
+					else if (newState === "sound_unavailable")
+						new Toast("Sound is currently unavailable.");
 
 					player.value.paused = true;
 				}

+ 43 - 27
frontend/src/composables/useSoundcloudPlayer.ts

@@ -153,6 +153,20 @@ export const useSoundcloudPlayer = () => {
 		dispatchMessage("isPaused");
 	};
 
+	const soundcloudGetCurrentSound = callback => {
+		let called = false;
+
+		const _callback = value => {
+			if (called) return;
+			called = true;
+
+			callback(value);
+		};
+		addMethodCallback("getCurrentSound", _callback);
+
+		dispatchMessage("getCurrentSound");
+	};
+
 	const attemptToPlay = () => {
 		if (trackState.value === "playing") return;
 
@@ -174,19 +188,35 @@ export const useSoundcloudPlayer = () => {
 					return;
 				}
 
-				// Too many attempts, failed
-				if (attemptsToPlay.value >= 10 && value && !paused.value) {
-					changeTrackState("failed_to_play");
-					attemptsToPlay.value = 0;
-					return;
-				}
-
-				if (playAttemptTimeout.value)
-					clearTimeout(playAttemptTimeout.value);
-				playAttemptTimeout.value = setTimeout(() => {
-					if (trackState.value === "attempting_to_play")
-						attemptToPlay();
-				}, 500);
+				soundcloudGetCurrentSound(sound => {
+					// Sound is not available to play
+					if (
+						value &&
+						!paused.value &&
+						typeof sound === "object" &&
+						(!sound.playable ||
+							!sound.public ||
+							sound.policy === "BLOCK")
+					) {
+						changeTrackState("sound_unavailable");
+						attemptsToPlay.value = 0;
+						return;
+					}
+
+					// Too many attempts, failed
+					if (attemptsToPlay.value >= 10 && value && !paused.value) {
+						changeTrackState("failed_to_play");
+						attemptsToPlay.value = 0;
+						return;
+					}
+
+					if (playAttemptTimeout.value)
+						clearTimeout(playAttemptTimeout.value);
+					playAttemptTimeout.value = setTimeout(() => {
+						if (trackState.value === "attempting_to_play")
+							attemptToPlay();
+					}, 500);
+				});
 			});
 		}, 500);
 	};
@@ -271,20 +301,6 @@ export const useSoundcloudPlayer = () => {
 
 	const soundcloudGetState = () => trackState.value;
 
-	const soundcloudGetCurrentSound = callback => {
-		let called = false;
-
-		const _callback = value => {
-			if (called) return;
-			called = true;
-
-			callback(value);
-		};
-		addMethodCallback("getCurrentSound", _callback);
-
-		dispatchMessage("getCurrentSound");
-	};
-
 	const soundcloudGetTrackId = () => currentTrackId.value;
 
 	const soundcloudGetTrackState = () => trackState.value;