fix radio cancellation error on double click (#1419)

This commit is contained in:
jeffvli
2026-01-01 20:40:24 -08:00
parent 55cead87c8
commit e406b27170
2 changed files with 12 additions and 1 deletions
+1 -1
View File
@@ -6,8 +6,8 @@ declare global {
interface Window {
api: PreloadApi;
electron: ElectronAPI;
queryLocalFonts?: () => Promise<Font[]>;
LEGACY_AUTHENTICATION?: boolean;
queryLocalFonts?: () => Promise<Font[]>;
SERVER_LOCK?: boolean;
SERVER_NAME?: string;
SERVER_TYPE?: ServerType;
@@ -1,3 +1,4 @@
import console from 'console';
import IcecastMetadataStats from 'icecast-metadata-stats';
import isElectron from 'is-electron';
import { useEffect, useRef } from 'react';
@@ -134,6 +135,7 @@ export const useRadioAudioInstance = () => {
const volume = usePlayerVolume();
const isMuted = usePlayerMuted();
const audioRef = useRef<HTMLAudioElement | null>(null);
const activeAudioRef = useRef<HTMLAudioElement | null>(null);
const isUsingMpv = playbackType === PlayerType.LOCAL && mpvPlayer;
// Handle mpv playback
@@ -205,6 +207,7 @@ export const useRadioAudioInstance = () => {
const audio = new Audio(currentStreamUrl);
audioRef.current = audio;
activeAudioRef.current = audio;
const linearVolume = volume / 100;
const logVolume = convertToLogVolume(linearVolume);
@@ -231,6 +234,10 @@ export const useRadioAudioInstance = () => {
// Attempt to play
audio.play().catch((error) => {
if (activeAudioRef.current !== audio) {
return;
}
console.error('Failed to play audio:', error);
setIsPlaying(false);
setCurrentStreamUrl(null);
@@ -243,10 +250,14 @@ export const useRadioAudioInstance = () => {
audioRef.current.src = '';
audioRef.current = null;
}
activeAudioRef.current = null;
}
return () => {
if (audioRef.current) {
if (activeAudioRef.current === audioRef.current) {
activeAudioRef.current = null;
}
audioRef.current.pause();
audioRef.current.src = '';
audioRef.current = null;