fix: stop YouTube mute-check interval when player is destroyed
The setInterval polling playerRef.current.isMuted() continued running after the YouTube player was destroyed, causing repeated TypeErrors. Now the interval self-clears when it detects the player is invalid. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
cd86b3aac5
commit
67f308e47f
1 changed files with 23 additions and 14 deletions
|
|
@ -55,7 +55,7 @@ const Player = memo(({ videoId, isShort, className }: PlayerProps) => {
|
|||
}
|
||||
})
|
||||
|
||||
let checkMutedInterval: NodeJS.Timeout | null = null
|
||||
let checkMutedInterval: ReturnType<typeof setInterval> | null = null
|
||||
function initPlayer() {
|
||||
try {
|
||||
if (!videoId || !containerRef.current || !window.YT.Player || unmountedRef.current) return
|
||||
|
|
@ -87,20 +87,29 @@ const Player = memo(({ videoId, isShort, className }: PlayerProps) => {
|
|||
}
|
||||
setInitSuccess(true)
|
||||
checkMutedInterval = setInterval(() => {
|
||||
if (playerRef.current && !unmountedRef.current) {
|
||||
const mute = playerRef.current.isMuted()
|
||||
if (mute !== currentMuteState) {
|
||||
currentMuteState = mute
|
||||
if (
|
||||
!playerRef.current ||
|
||||
unmountedRef.current ||
|
||||
typeof playerRef.current.isMuted !== 'function'
|
||||
) {
|
||||
if (checkMutedInterval) {
|
||||
clearInterval(checkMutedInterval)
|
||||
checkMutedInterval = null
|
||||
}
|
||||
return
|
||||
}
|
||||
const mute = playerRef.current.isMuted()
|
||||
if (mute !== currentMuteState) {
|
||||
currentMuteState = mute
|
||||
|
||||
if (mute !== muteStateRef.current) {
|
||||
updateMuteMedia(currentMuteState)
|
||||
}
|
||||
} else if (muteStateRef.current !== mute) {
|
||||
if (muteStateRef.current) {
|
||||
playerRef.current.mute()
|
||||
} else {
|
||||
playerRef.current.unMute()
|
||||
}
|
||||
if (mute !== muteStateRef.current) {
|
||||
updateMuteMedia(currentMuteState)
|
||||
}
|
||||
} else if (muteStateRef.current !== mute) {
|
||||
if (muteStateRef.current) {
|
||||
playerRef.current.mute()
|
||||
} else {
|
||||
playerRef.current.unMute()
|
||||
}
|
||||
}
|
||||
}, 200)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue