Refactor AudioPlayer to reset metadata and state on station change, improve WebSocket cleanup logic, and ensure proper title updates for active stations.
This commit is contained in:
@@ -278,13 +278,12 @@ export default function Player({ user }) {
|
|||||||
|
|
||||||
// Handle station changes: reset and start new stream
|
// Handle station changes: reset and start new stream
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsPlaying(false);
|
// Reset metadata and state when switching stations
|
||||||
setTrackTitle("");
|
setTrackTitle("");
|
||||||
setTrackArtist("");
|
setTrackArtist("");
|
||||||
setTrackGenre("");
|
setTrackGenre("");
|
||||||
setTrackAlbum("");
|
setTrackAlbum("");
|
||||||
setCoverArt("/images/radio_art_default.jpg");
|
setCoverArt("/images/radio_art_default.jpg");
|
||||||
|
|
||||||
setLyrics([]);
|
setLyrics([]);
|
||||||
setCurrentLyricIndex(0);
|
setCurrentLyricIndex(0);
|
||||||
setElapsedTime(0);
|
setElapsedTime(0);
|
||||||
@@ -295,7 +294,8 @@ export default function Player({ user }) {
|
|||||||
lastUpdateTimestamp.current = Date.now();
|
lastUpdateTimestamp.current = Date.now();
|
||||||
|
|
||||||
initializeStream(activeStation);
|
initializeStream(activeStation);
|
||||||
// If no track is loaded, update page title to just station
|
|
||||||
|
// Update page title to reflect the new station
|
||||||
document.title = `${metaData.title} - Radio [${activeStation}]`;
|
document.title = `${metaData.title} - Radio [${activeStation}]`;
|
||||||
}, [activeStation]);
|
}, [activeStation]);
|
||||||
|
|
||||||
@@ -394,6 +394,7 @@ export default function Player({ user }) {
|
|||||||
const initializeWebSocket = useCallback((station) => {
|
const initializeWebSocket = useCallback((station) => {
|
||||||
// Clean up existing WebSocket
|
// Clean up existing WebSocket
|
||||||
if (wsInstance.current) {
|
if (wsInstance.current) {
|
||||||
|
wsInstance.current.onclose = null; // Prevent triggering reconnection logic
|
||||||
wsInstance.current.close();
|
wsInstance.current.close();
|
||||||
wsInstance.current = null;
|
wsInstance.current = null;
|
||||||
}
|
}
|
||||||
@@ -481,15 +482,27 @@ export default function Player({ user }) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// ensure the ref points to the current activeStation for in-flight guards
|
// Ensure the ref points to the current activeStation for in-flight guards
|
||||||
activeStationRef.current = activeStation;
|
activeStationRef.current = activeStation;
|
||||||
|
|
||||||
// Initialize WebSocket connection for metadata
|
// Clean up the existing WebSocket connection before initializing a new one
|
||||||
initializeWebSocket(activeStation);
|
const cleanupWebSocket = async () => {
|
||||||
|
if (wsInstance.current) {
|
||||||
|
wsInstance.current.onclose = null; // Prevent triggering reconnection logic
|
||||||
|
wsInstance.current.close();
|
||||||
|
wsInstance.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cleanupWebSocket().then(() => {
|
||||||
|
// Initialize WebSocket connection for metadata
|
||||||
|
initializeWebSocket(activeStation);
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
// Clean up WebSocket on station change or component unmount
|
// Clean up WebSocket on station change or component unmount
|
||||||
if (wsInstance.current) {
|
if (wsInstance.current) {
|
||||||
|
wsInstance.current.onclose = null; // Prevent triggering reconnection logic
|
||||||
wsInstance.current.close();
|
wsInstance.current.close();
|
||||||
wsInstance.current = null;
|
wsInstance.current = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user