Adjust shared media player controls
This commit is contained in:
1 parent
44518b955e
commit
db14c02558
2 files changed
+232
-81
No files matched your search
@@ -15,9 +15,11 @@
|
||||
autoplay = false,
|
||||
class: className = '',
|
||||
mediaClass = '',
|
||||
showSeekButtons = true,
|
||||
showSeekButtons: _showSeekButtons = true,
|
||||
showVolume = true,
|
||||
showFullscreen = true,
|
||||
showGraph = false,
|
||||
graphVisible = false,
|
||||
qualityOptions = [],
|
||||
selectedQuality = '',
|
||||
currentTime: externalCurrentTime = null,
|
||||
@@ -30,6 +32,8 @@
|
||||
onEnded,
|
||||
onSeek,
|
||||
onSkip,
|
||||
onQualityChange,
|
||||
onToggleGraph,
|
||||
endControls,
|
||||
}: {
|
||||
kind?: MediaPlayerKind
|
||||
@@ -39,9 +43,12 @@
|
||||
autoplay?: boolean
|
||||
class?: string
|
||||
mediaClass?: string
|
||||
/** @deprecated Seeking is available with the arrow keys and mobile double-tap. */
|
||||
showSeekButtons?: boolean
|
||||
showVolume?: boolean
|
||||
showFullscreen?: boolean
|
||||
showGraph?: boolean
|
||||
graphVisible?: boolean
|
||||
qualityOptions?: MediaPlayerQualityOption[]
|
||||
selectedQuality?: string
|
||||
currentTime?: number | null
|
||||
@@ -54,6 +61,8 @@
|
||||
onEnded?: (event: Event) => void
|
||||
onSeek?: (value: number) => void
|
||||
onSkip?: (seconds: number) => void
|
||||
onQualityChange?: (value: string) => void
|
||||
onToggleGraph?: () => void
|
||||
endControls?: Snippet
|
||||
} = $props()
|
||||
|
||||
@@ -66,7 +75,9 @@
|
||||
let muted = $state(false)
|
||||
let fullscreen = $state(false)
|
||||
let controlsVisible = $state(true)
|
||||
let orientationLocked = $state(false)
|
||||
let controlsHideTimeout: ReturnType<typeof setTimeout> | null = null
|
||||
let orientationLockRequest = 0
|
||||
|
||||
const currentTime = $derived(externalCurrentTime ?? internalTime)
|
||||
const duration = $derived(externalDuration ?? internalDuration)
|
||||
@@ -127,6 +138,60 @@
|
||||
}
|
||||
}
|
||||
|
||||
function isMobileDevice() {
|
||||
return (
|
||||
typeof window !== 'undefined' &&
|
||||
(window.matchMedia?.('(pointer: coarse)').matches === true || navigator.maxTouchPoints > 0)
|
||||
)
|
||||
}
|
||||
|
||||
function isWideVideo() {
|
||||
return Boolean(
|
||||
mediaElement &&
|
||||
!audio &&
|
||||
mediaElement instanceof HTMLVideoElement &&
|
||||
mediaElement.videoWidth > mediaElement.videoHeight,
|
||||
)
|
||||
}
|
||||
|
||||
async function syncScreenOrientation(nextFullscreen: boolean) {
|
||||
const request = ++orientationLockRequest
|
||||
const orientation =
|
||||
typeof window !== 'undefined'
|
||||
? (window.screen?.orientation as
|
||||
| (ScreenOrientation & {
|
||||
lock: (orientation: 'landscape') => Promise<void>
|
||||
})
|
||||
| undefined)
|
||||
: undefined
|
||||
const shouldLock = nextFullscreen && isMobileDevice() && isWideVideo()
|
||||
|
||||
if (!shouldLock) {
|
||||
if (orientationLocked) orientation?.unlock()
|
||||
orientationLocked = false
|
||||
return
|
||||
}
|
||||
|
||||
if (!orientation?.lock) return
|
||||
try {
|
||||
await orientation.lock('landscape')
|
||||
if (request !== orientationLockRequest) {
|
||||
orientation.unlock()
|
||||
return
|
||||
}
|
||||
orientationLocked = true
|
||||
} catch {
|
||||
// Orientation locking is not available in every mobile browser.
|
||||
}
|
||||
}
|
||||
|
||||
function handleVideoDoubleTap(event: MouseEvent) {
|
||||
if (audio || !isMobileDevice()) return
|
||||
const target = event.currentTarget as HTMLVideoElement
|
||||
const bounds = target.getBoundingClientRect()
|
||||
skip(event.clientX - bounds.left < bounds.width / 2 ? -10 : 10)
|
||||
}
|
||||
|
||||
function handlePlay(event: Event) {
|
||||
playing = true
|
||||
onPlay?.(event)
|
||||
@@ -148,6 +213,7 @@
|
||||
internalDuration = Number.isFinite(mediaElement.duration) ? mediaElement.duration : 0
|
||||
volume = mediaElement.volume
|
||||
muted = mediaElement.muted || mediaElement.volume === 0
|
||||
void syncScreenOrientation(fullscreen)
|
||||
onLoadedMetadata?.(event)
|
||||
}
|
||||
|
||||
@@ -219,6 +285,7 @@
|
||||
|
||||
function handleFullscreenChange() {
|
||||
fullscreen = Boolean(document.fullscreenElement)
|
||||
void syncScreenOrientation(fullscreen)
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
@@ -247,16 +314,18 @@
|
||||
|
||||
return () => {
|
||||
for (const eventName of activityEvents) playerFrame?.removeEventListener(eventName, handlePlayerActivity)
|
||||
orientationLockRequest += 1
|
||||
if (orientationLocked) {
|
||||
window.screen?.orientation?.unlock()
|
||||
orientationLocked = false
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<svelte:window onfullscreenchange={handleFullscreenChange} />
|
||||
|
||||
<div
|
||||
bind:this={playerFrame}
|
||||
class={`media-player ${className}`}
|
||||
>
|
||||
<div bind:this={playerFrame} class={`media-player ${className}`}>
|
||||
{#if audio}
|
||||
<div
|
||||
class="media-player-audio-surface"
|
||||
@@ -294,9 +363,11 @@
|
||||
{autoplay}
|
||||
preload="auto"
|
||||
playsinline
|
||||
tabindex="0"
|
||||
class={`media-player-video ${mediaClass}`}
|
||||
aria-label={title ? `Video player for ${title}` : 'Video player'}
|
||||
onclick={handleSurfaceClick}
|
||||
ondblclick={handleVideoDoubleTap}
|
||||
onkeydown={handleKeydown}
|
||||
onplay={handlePlay}
|
||||
onpause={handlePause}
|
||||
@@ -320,15 +391,17 @@
|
||||
{controlsVisible}
|
||||
onTogglePlayback={togglePlayback}
|
||||
onSeek={seek}
|
||||
onSkip={skip}
|
||||
onToggleMute={toggleMute}
|
||||
onVolumeChange={changeVolume}
|
||||
onToggleFullscreen={toggleFullscreen}
|
||||
{showSeekButtons}
|
||||
{showVolume}
|
||||
{showFullscreen}
|
||||
{showGraph}
|
||||
{graphVisible}
|
||||
{qualityOptions}
|
||||
{selectedQuality}
|
||||
{onQualityChange}
|
||||
{onToggleGraph}
|
||||
{endControls}
|
||||
overlay
|
||||
/>
|
||||
@@ -357,6 +430,7 @@
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
touch-action: manipulation;
|
||||
}
|
||||
|
||||
.media-player-audio-surface {
|
||||
|
||||
Reference in new issue
Block a user