Spaces:
Sleeping
Sleeping
File size: 6,558 Bytes
b615593 21986a1 f57be92 21986a1 93b25cc ba6c831 f57be92 21986a1 eb2c7d1 b615593 ba6c831 eb2c7d1 ba6c831 f57be92 eb2c7d1 b615593 ba6c831 b615593 ba6c831 eb2c7d1 b615593 ba6c831 93b25cc f57be92 93b25cc ba6c831 93b25cc b615593 f57be92 62c5ab3 ba6c831 11c56f5 b615593 11c56f5 93b25cc 11c56f5 f57be92 11c56f5 b615593 f57be92 93b25cc 11c56f5 93b25cc 11c56f5 b615593 11c56f5 93b25cc 11c56f5 93b25cc 11c56f5 b615593 f57be92 93b25cc 11c56f5 93b25cc 11c56f5 93b25cc 11c56f5 b615593 11c56f5 96b94b7 62c5ab3 96b94b7 11c56f5 b615593 93b25cc 62c5ab3 93b25cc 11c56f5 62c5ab3 f57be92 62c5ab3 f57be92 62c5ab3 11c56f5 f57be92 21986a1 eb2c7d1 b615593 ba6c831 eb2c7d1 ba6c831 eb2c7d1 b615593 eb2c7d1 ba6c831 eb2c7d1 ba6c831 eb2c7d1 ba6c831 eb2c7d1 ba6c831 eb2c7d1 ba6c831 eb2c7d1 ba6c831 21986a1 f57be92 21986a1 11c56f5 f57be92 b615593 f57be92 eb2c7d1 ba6c831 b615593 21986a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import React, {
createContext,
useContext,
useRef,
useState,
useEffect,
} from "react";
const MusicPlayerContext = createContext();
export const MusicPlayerProvider = ({ children }) => {
const videoRef = useRef(null);
const [src, setSrc] = useState("");
const [title, setTitle] = useState("");
const [nowPlaying, setNowPlaying] = useState("");
const [isPlayerVisible, setIsPlayerVisible] = useState(false);
const [isPlayerMaximized, setIsPlayerMaximized] = useState(false);
const [loadingProgress, setLoadingProgress] = useState(null);
const [abortController, setAbortController] = useState(null);
const [didDestroy, setDidDestroy] = useState(false);
// Playqueue and index
const [playqueue, setPlayqueue] = useState([]);
const [isPlayEueOpen, setIsPlayEueOpen] = useState(false);
const [currentIndex, setCurrentIndex] = useState(0);
const canPlayPrevious = currentIndex > 0;
const canPlayNext = currentIndex < playqueue.length - 1;
const initializePlayer = async (source, title) => {
// Check if song already exists in playqueue
const songIndex = playqueue.findIndex((song) => song.source === source);
if (songIndex !== -1) {
setCurrentIndex(songIndex); // Set to existing song index
} else {
// Add new song and set to that new index
const newSong = { source, title };
setPlayqueue((prev) => [...prev, newSong]);
setCurrentIndex(playqueue.length); // Set index to the end (new song position)
}
if (abortController) {
abortController.abort();
}
const newAbortController = new AbortController();
setAbortController(newAbortController);
const extractedFileName = source.split("/").pop();
setTitle(title);
if (videoRef.current) {
videoRef.current.pause();
}
try {
const response = await fetch(
`/api/get/music/${encodeURIComponent(extractedFileName)}`,
{
signal: newAbortController.signal,
}
);
const data = await response.json();
if (data.status === "Download started") {
const progressUrl = data.progress_url;
checkProgress(progressUrl, extractedFileName, newAbortController);
} else {
startPlayer(data.url);
}
} catch (error) {
if (error.name !== "AbortError") {
console.error("Error initializing player:", error);
}
}
};
const checkProgress = (progressUrl, fileName, abortController) => {
const intervalId = setInterval(async () => {
try {
const response = await fetch(progressUrl, {
signal: abortController.signal,
});
const progressData = await response.json();
setLoadingProgress(progressData.progress);
if (progressData.progress.status === "Completed") {
clearInterval(intervalId);
fetchFinalUrl(fileName, abortController);
}
} catch (error) {
if (error.name === "AbortError") {
clearInterval(intervalId);
} else {
console.error("Error fetching progress:", error);
}
}
}, 1000);
};
const fetchFinalUrl = async (fileName, abortController, attempt = 1) => {
try {
const response = await fetch(
`/api/get/music/${encodeURIComponent(fileName)}`,
{
signal: abortController.signal,
}
);
const data = await response.json();
if (data.url) {
startPlayer(data.url);
} else {
retryFetchFinalUrl(fileName, abortController, attempt);
}
} catch (error) {
if (error.name !== "AbortError") {
console.error("Error fetching final URL:", error);
retryFetchFinalUrl(fileName, abortController, attempt);
}
}
};
const retryFetchFinalUrl = (fileName, abortController, attempt) => {
const retryDelay = 2000;
setTimeout(() => {
console.log(`Retry attempt ${attempt} for ${fileName}`);
fetchFinalUrl(fileName, abortController, attempt + 1);
}, retryDelay);
};
const startPlayer = (source) => {
setDidDestroy(false);
setSrc(source);
setIsPlayerVisible(true);
if (videoRef.current) {
videoRef.current.src = source;
videoRef.current.load();
}
};
const togglePlayerSize = () => setIsPlayerMaximized(!isPlayerMaximized);
const addToPlayqueue = (song) => {
const songIndex = playqueue.findIndex(
(track) => track.source === song.source
);
if (songIndex === -1) {
setPlayqueue((prev) => [...prev, song]);
}
};
const removeFromPlayqueue = (source) => {
setPlayqueue((prev) => prev.filter((track) => track.source !== source));
if (currentIndex >= playqueue.length) {
setCurrentIndex(playqueue.length - 1);
}
};
const playNext = () => {
if (canPlayNext) {
const nextIndex = currentIndex + 1;
setCurrentIndex(nextIndex);
const { source, title } = playqueue[nextIndex];
initializePlayer(source, title);
}
};
const playPrevious = () => {
if (canPlayPrevious) {
const prevIndex = currentIndex - 1;
setCurrentIndex(prevIndex);
const { source, title } = playqueue[prevIndex];
initializePlayer(source, title);
}
};
const playAtIndex = (index) => {
if (index >= 0 && index < playqueue.length) {
setCurrentIndex(index);
const { source, title } = playqueue[index];
initializePlayer(source, title);
}
};
useEffect(() => {
const handleEnded = () => playNext();
if (videoRef.current) {
videoRef.current.addEventListener("ended", handleEnded);
}
return () => {
if (videoRef.current) {
videoRef.current.removeEventListener("ended", handleEnded);
}
};
}, [currentIndex, playqueue]);
return (
<MusicPlayerContext.Provider
value={{
videoRef,
initializePlayer,
isPlayerVisible,
src,
title,
isPlayerMaximized,
togglePlayerSize,
setIsPlayerVisible,
loadingProgress,
nowPlaying,
setNowPlaying,
didDestroy,
setDidDestroy,
playqueue,
setPlayqueue,
addToPlayqueue,
removeFromPlayqueue,
playNext,
playPrevious,
playAtIndex,
currentIndex,
canPlayPrevious,
canPlayNext,
isPlayEueOpen,
setIsPlayEueOpen,
}}
>
{children}
</MusicPlayerContext.Provider>
);
};
export const useMusicPlayer = () => {
return useContext(MusicPlayerContext);
};
|