This commit is contained in:
1 parent
23cdd88e11
commit
efda65ae5f
2 files changed
+71
-2
No files matched your search
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { Music2 } from '@lucide/svelte'
|
||||
import { onMount } from 'svelte'
|
||||
import type { Snippet } from 'svelte'
|
||||
import MediaPlayerControls, {
|
||||
type MediaPlayerKind,
|
||||
@@ -64,10 +65,34 @@
|
||||
let volume = $state(1)
|
||||
let muted = $state(false)
|
||||
let fullscreen = $state(false)
|
||||
let controlsVisible = $state(true)
|
||||
let controlsHideTimeout: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
const currentTime = $derived(externalCurrentTime ?? internalTime)
|
||||
const duration = $derived(externalDuration ?? internalDuration)
|
||||
const audio = $derived(kind === 'audio')
|
||||
const controlsHideDelay = 2500
|
||||
|
||||
function clearControlsHideTimer() {
|
||||
if (controlsHideTimeout === null) return
|
||||
clearTimeout(controlsHideTimeout)
|
||||
controlsHideTimeout = null
|
||||
}
|
||||
|
||||
function scheduleControlsHide() {
|
||||
clearControlsHideTimer()
|
||||
if (audio || !playing) return
|
||||
controlsHideTimeout = setTimeout(() => {
|
||||
controlsVisible = false
|
||||
controlsHideTimeout = null
|
||||
}, controlsHideDelay)
|
||||
}
|
||||
|
||||
function handlePlayerActivity() {
|
||||
if (audio) return
|
||||
controlsVisible = true
|
||||
scheduleControlsHide()
|
||||
}
|
||||
|
||||
function togglePlayback() {
|
||||
if (!mediaElement) return
|
||||
@@ -109,6 +134,7 @@
|
||||
|
||||
function handlePause(event: Event) {
|
||||
playing = false
|
||||
controlsVisible = true
|
||||
onPause?.(event)
|
||||
}
|
||||
|
||||
@@ -127,6 +153,7 @@
|
||||
|
||||
function handleEnded(event: Event) {
|
||||
playing = false
|
||||
controlsVisible = true
|
||||
onEnded?.(event)
|
||||
}
|
||||
|
||||
@@ -200,11 +227,36 @@
|
||||
mediaElement.load()
|
||||
if (autoplay) void mediaElement.play().catch(() => undefined)
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
if (audio || !playing) {
|
||||
clearControlsHideTimer()
|
||||
controlsVisible = true
|
||||
return
|
||||
}
|
||||
|
||||
scheduleControlsHide()
|
||||
return clearControlsHideTimer
|
||||
})
|
||||
|
||||
onMount(() => {
|
||||
if (!playerFrame) return
|
||||
|
||||
const activityEvents = ['mousemove', 'click', 'keydown', 'focusin', 'touchstart'] as const
|
||||
for (const eventName of activityEvents) playerFrame.addEventListener(eventName, handlePlayerActivity)
|
||||
|
||||
return () => {
|
||||
for (const eventName of activityEvents) playerFrame?.removeEventListener(eventName, handlePlayerActivity)
|
||||
}
|
||||
})
|
||||
</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"
|
||||
@@ -265,6 +317,7 @@
|
||||
{volume}
|
||||
{muted}
|
||||
{fullscreen}
|
||||
{controlsVisible}
|
||||
onTogglePlayback={togglePlayback}
|
||||
onSeek={seek}
|
||||
onSkip={skip}
|
||||
|
||||
Reference in new issue
Block a user