Skip to content

Commit

Permalink
[Video] Fix safari showing spinner (#5364)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8daf6b7)
  • Loading branch information
mozzius authored and haileyok committed Sep 16, 2024
1 parent 9956572 commit fb71fff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {useLingui} from '@lingui/react'

import {isFirefox} from '#/lib/browser'
import {clamp} from '#/lib/numbers'
import {atoms as a, useTheme} from '#/alf'
import {atoms as a, useTheme, web} from '#/alf'
import {useInteractionState} from '#/components/hooks/useInteractionState'
import {formatTime} from './utils'

Expand Down Expand Up @@ -172,6 +172,7 @@ export function Scrubber({
a.overflow_hidden,
{backgroundColor: 'rgba(255, 255, 255, 0.4)'},
{height: hovered || scrubberActive ? 6 : 3},
web({transition: 'height 0.1s ease'}),
]}>
{duration > 0 && (
<View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useCallback, useEffect, useRef, useState} from 'react'

import {isSafari} from '#/lib/browser'
import {useVideoVolumeState} from '../../VideoVolumeContext'

export function useVideoElement(ref: React.RefObject<HTMLVideoElement>) {
Expand Down Expand Up @@ -37,6 +38,12 @@ export function useVideoElement(ref: React.RefObject<HTMLVideoElement>) {
const handleTimeUpdate = () => {
if (!ref.current) return
setCurrentTime(round(ref.current.currentTime) || 0)
// HACK: Safari randomly fires `stalled` events when changing between segments
// let's just clear the buffering state if the video is still progressing -sfn
if (isSafari) {
if (bufferingTimeout) clearTimeout(bufferingTimeout)
setBuffering(false)
}
}

const handleDurationChange = () => {
Expand Down Expand Up @@ -82,7 +89,7 @@ export function useVideoElement(ref: React.RefObject<HTMLVideoElement>) {
if (bufferingTimeout) clearTimeout(bufferingTimeout)
bufferingTimeout = setTimeout(() => {
setBuffering(true)
}, 200) // Delay to avoid frequent buffering state changes
}, 500) // Delay to avoid frequent buffering state changes
}

const handlePlaying = () => {
Expand All @@ -95,7 +102,7 @@ export function useVideoElement(ref: React.RefObject<HTMLVideoElement>) {
if (bufferingTimeout) clearTimeout(bufferingTimeout)
bufferingTimeout = setTimeout(() => {
setBuffering(true)
}, 200) // Delay to avoid frequent buffering state changes
}, 500) // Delay to avoid frequent buffering state changes
}

const handleEnded = () => {
Expand Down

0 comments on commit fb71fff

Please sign in to comment.