fix: improve plyr render for youtube video
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
import type { MediaPlayerOptions, VideoSource } from './types';
|
import type { MediaPlayerOptions, VideoSource } from './types';
|
||||||
import PlyrPlayer from './players/plyr-player.svelte';
|
import PlyrPlayer from './players/plyr-player.svelte';
|
||||||
import MusePlayer from './players/muse-player.svelte';
|
import MusePlayer from './players/muse-player.svelte';
|
||||||
|
import { formatYoutubeEmbedUrl } from './utils';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
source: VideoSource;
|
source: VideoSource;
|
||||||
@@ -26,11 +27,13 @@
|
|||||||
const tracks = $derived(source.tracks ?? []);
|
const tracks = $derived(source.tracks ?? []);
|
||||||
const isMuse = $derived.by(() => source.type === 'muse' && source.metadata?.svid);
|
const isMuse = $derived.by(() => source.type === 'muse' && source.metadata?.svid);
|
||||||
const isGoogleDrive = $derived(source.type === 'google_drive');
|
const isGoogleDrive = $derived(source.type === 'google_drive');
|
||||||
|
const isYouTube = $derived(source.type === 'youtube');
|
||||||
const poster = $derived(source.type === 'upload' ? source.metadata?.thumbnailUrl : undefined);
|
const poster = $derived(source.type === 'upload' ? source.metadata?.thumbnailUrl : undefined);
|
||||||
|
|
||||||
const iframeTitle = $derived(source.metadata?.title?.trim() || 'Video');
|
const iframeTitle = $derived(source.metadata?.title?.trim() || 'Video');
|
||||||
const iframeMaxHeight = $derived(options.maxHeight ?? '400px');
|
const iframeMaxHeight = $derived(options.maxHeight ?? '400px');
|
||||||
const iframeWidth = $derived(options.width ?? '100%');
|
const iframeWidth = $derived(options.width ?? '100%');
|
||||||
|
const youtubeEmbedUrl = $derived(isYouTube ? formatYoutubeEmbedUrl(source.url) : '');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class={className}>
|
<div class={className}>
|
||||||
@@ -51,6 +54,23 @@
|
|||||||
allowfullscreen
|
allowfullscreen
|
||||||
></iframe>
|
></iframe>
|
||||||
</div>
|
</div>
|
||||||
|
{:else if isYouTube}
|
||||||
|
<!-- Native embed avoids Plyr's async noembed/setAspectRatio race on teardown. -->
|
||||||
|
<div
|
||||||
|
class="ui:relative ui:aspect-video ui:w-full ui:overflow-hidden ui:rounded-md"
|
||||||
|
style:max-height={iframeMaxHeight}
|
||||||
|
style:width={iframeWidth}
|
||||||
|
style:min-height={options.minHeight}
|
||||||
|
style:height={options.height}
|
||||||
|
>
|
||||||
|
<iframe
|
||||||
|
src={youtubeEmbedUrl}
|
||||||
|
title={iframeTitle}
|
||||||
|
class="ui:block ui:h-full ui:w-full ui:border-0"
|
||||||
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
||||||
|
allowfullscreen
|
||||||
|
></iframe>
|
||||||
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<PlyrPlayer bind:this={plyrRef} src={source.url} {poster} hls={source.hls === true} {options} {tracks} />
|
<PlyrPlayer bind:this={plyrRef} src={source.url} {poster} hls={source.hls === true} {options} {tracks} />
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@
|
|||||||
let hlsPlyrGeneration = 0;
|
let hlsPlyrGeneration = 0;
|
||||||
let plyrConstructInFlight = false;
|
let plyrConstructInFlight = false;
|
||||||
let hasFiredFirstPlay = false;
|
let hasFiredFirstPlay = false;
|
||||||
|
let isMounted = false;
|
||||||
|
let youtubeInitGeneration = 0;
|
||||||
|
|
||||||
interface HlsQualityConfig {
|
interface HlsQualityConfig {
|
||||||
default: number;
|
default: number;
|
||||||
@@ -193,7 +195,7 @@
|
|||||||
try {
|
try {
|
||||||
// Recheck after the dynamic import resolves — the component may have
|
// Recheck after the dynamic import resolves — the component may have
|
||||||
// unmounted (or the videoElement bind nulled out) while we awaited.
|
// unmounted (or the videoElement bind nulled out) while we awaited.
|
||||||
if (!videoElement || playerInstance) return;
|
if (!isMounted || !videoElement || playerInstance) return;
|
||||||
|
|
||||||
mediaErrorCleanup = attachMediaErrorHandler(videoElement);
|
mediaErrorCleanup = attachMediaErrorHandler(videoElement);
|
||||||
|
|
||||||
@@ -257,23 +259,43 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function constructPlyrForYouTubeEmbed(): Promise<void> {
|
||||||
|
if (!isMounted || !containerElement || !youtubeVideoId || playerInstance || plyrConstructInFlight) return;
|
||||||
|
|
||||||
|
const initGeneration = youtubeInitGeneration;
|
||||||
|
plyrConstructInFlight = true;
|
||||||
|
|
||||||
|
const PlyrModule = await import('plyr');
|
||||||
|
const PlyrConstructor = PlyrModule.default;
|
||||||
|
const controls = showControls ? PLYR_DEFAULT_CONTROLS : [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!isMounted || initGeneration !== youtubeInitGeneration || !containerElement || playerInstance) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
playerInstance = new PlyrConstructor(containerElement, {
|
||||||
|
controls,
|
||||||
|
autoplay,
|
||||||
|
ratio: '16:9',
|
||||||
|
iconUrl: '/plyr.svg',
|
||||||
|
iconPrefix: 'plyr'
|
||||||
|
});
|
||||||
|
timeupdateCleanup = attachTimeUpdates(playerInstance);
|
||||||
|
firstPlayCleanup = attachFirstPlay(playerInstance);
|
||||||
|
plyrErrorCleanup = attachPlyrErrorHandler(playerInstance);
|
||||||
|
options.onPlayerReady?.(playerInstance);
|
||||||
|
} finally {
|
||||||
|
plyrConstructInFlight = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
isMounted = true;
|
||||||
|
|
||||||
void (async () => {
|
void (async () => {
|
||||||
if (isYouTube && youtubeVideoId && containerElement) {
|
if (isYouTube && youtubeVideoId && containerElement) {
|
||||||
const PlyrModule = await import('plyr');
|
await constructPlyrForYouTubeEmbed();
|
||||||
const PlyrConstructor = PlyrModule.default;
|
|
||||||
const controls = showControls ? PLYR_DEFAULT_CONTROLS : [];
|
|
||||||
playerInstance = new PlyrConstructor(containerElement, {
|
|
||||||
controls,
|
|
||||||
autoplay,
|
|
||||||
ratio: '16:9',
|
|
||||||
iconUrl: '/plyr.svg',
|
|
||||||
iconPrefix: 'plyr'
|
|
||||||
});
|
|
||||||
timeupdateCleanup = attachTimeUpdates(playerInstance);
|
|
||||||
firstPlayCleanup = attachFirstPlay(playerInstance);
|
|
||||||
plyrErrorCleanup = attachPlyrErrorHandler(playerInstance);
|
|
||||||
options.onPlayerReady?.(playerInstance);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,6 +470,8 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
|
isMounted = false;
|
||||||
|
youtubeInitGeneration += 1;
|
||||||
hlsLoadGeneration += 1;
|
hlsLoadGeneration += 1;
|
||||||
hlsPlyrGeneration = 0;
|
hlsPlyrGeneration = 0;
|
||||||
destroyHlsInstance();
|
destroyHlsInstance();
|
||||||
@@ -463,6 +487,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
teardownPlyrInstance();
|
teardownPlyrInstance();
|
||||||
|
|
||||||
|
if (isYouTube && containerElement) {
|
||||||
|
containerElement.replaceChildren();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user