2022-06-04 18:34:11 -05:00
|
|
|
<script lang="ts">
|
2024-05-31 13:44:04 -04:00
|
|
|
import LoadingSpinner from '$lib/components/shared-components/loading-spinner.svelte';
|
|
|
|
|
import { loopVideo as loopVideoPreference, videoViewerMuted, videoViewerVolume } from '$lib/stores/preferences.store';
|
|
|
|
|
import { getAssetPlaybackUrl, getAssetThumbnailUrl } from '$lib/utils';
|
2023-08-27 07:31:52 +03:00
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
2024-05-31 13:44:04 -04:00
|
|
|
import { AssetMediaSize } from '@immich/sdk';
|
2024-09-20 23:02:58 +02:00
|
|
|
import { tick } from 'svelte';
|
2024-08-29 17:40:17 +02:00
|
|
|
import { swipe } from 'svelte-gestures';
|
|
|
|
|
import type { SwipeCustomEvent } from 'svelte-gestures';
|
2024-02-14 08:09:49 -05:00
|
|
|
import { fade } from 'svelte/transition';
|
2024-06-04 21:53:00 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
2022-06-04 18:34:11 -05:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
export let assetId: string;
|
2024-05-14 21:31:47 +02:00
|
|
|
export let loopVideo: boolean;
|
2024-05-23 20:26:22 -04:00
|
|
|
export let checksum: string;
|
2024-09-20 23:02:58 +02:00
|
|
|
export let onPreviousAsset: () => void = () => {};
|
|
|
|
|
export let onNextAsset: () => void = () => {};
|
|
|
|
|
export let onVideoEnded: () => void = () => {};
|
|
|
|
|
export let onVideoStarted: () => void = () => {};
|
2022-06-04 18:34:11 -05:00
|
|
|
|
2024-03-21 21:01:08 +01:00
|
|
|
let element: HTMLVideoElement | undefined = undefined;
|
2023-07-01 00:50:47 -04:00
|
|
|
let isVideoLoading = true;
|
2024-05-23 20:26:22 -04:00
|
|
|
let assetFileUrl: string;
|
2024-06-16 17:37:25 +02:00
|
|
|
let forceMuted = false;
|
2024-05-23 20:26:22 -04:00
|
|
|
|
2024-06-16 17:37:25 +02:00
|
|
|
$: if (element) {
|
|
|
|
|
assetFileUrl = getAssetPlaybackUrl({ id: assetId, checksum });
|
|
|
|
|
forceMuted = false;
|
|
|
|
|
element.load();
|
2024-05-23 20:26:22 -04:00
|
|
|
}
|
2024-03-21 21:01:08 +01:00
|
|
|
|
2024-06-16 17:37:25 +02:00
|
|
|
const handleCanPlay = async (video: HTMLVideoElement) => {
|
2023-08-17 11:02:12 -04:00
|
|
|
try {
|
|
|
|
|
await video.play();
|
2024-09-20 23:02:58 +02:00
|
|
|
onVideoStarted();
|
2023-08-17 11:02:12 -04:00
|
|
|
} catch (error) {
|
2024-06-16 17:37:25 +02:00
|
|
|
if (error instanceof DOMException && error.name === 'NotAllowedError' && !forceMuted) {
|
|
|
|
|
await tryForceMutedPlay(video);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-04 21:53:00 +02:00
|
|
|
handleError(error, $t('errors.unable_to_play_video'));
|
2023-08-17 14:52:50 -04:00
|
|
|
} finally {
|
|
|
|
|
isVideoLoading = false;
|
2023-08-17 11:02:12 -04:00
|
|
|
}
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
2024-06-16 17:37:25 +02:00
|
|
|
|
|
|
|
|
const tryForceMutedPlay = async (video: HTMLVideoElement) => {
|
|
|
|
|
try {
|
|
|
|
|
forceMuted = true;
|
|
|
|
|
await tick();
|
|
|
|
|
await handleCanPlay(video);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleError(error, $t('errors.unable_to_play_video'));
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-08-29 17:40:17 +02:00
|
|
|
|
|
|
|
|
const onSwipe = (event: SwipeCustomEvent) => {
|
|
|
|
|
if (event.detail.direction === 'left') {
|
|
|
|
|
onNextAsset();
|
|
|
|
|
}
|
|
|
|
|
if (event.detail.direction === 'right') {
|
|
|
|
|
onPreviousAsset();
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-06-04 18:34:11 -05:00
|
|
|
</script>
|
|
|
|
|
|
2024-07-01 15:05:49 +01:00
|
|
|
<div transition:fade={{ duration: 150 }} class="flex h-full select-none place-content-center place-items-center">
|
2023-07-01 00:50:47 -04:00
|
|
|
<video
|
2024-03-21 21:01:08 +01:00
|
|
|
bind:this={element}
|
2024-05-14 21:31:47 +02:00
|
|
|
loop={$loopVideoPreference && loopVideo}
|
2023-08-17 14:52:50 -04:00
|
|
|
autoplay
|
|
|
|
|
playsinline
|
2023-07-01 00:50:47 -04:00
|
|
|
controls
|
|
|
|
|
class="h-full object-contain"
|
2024-08-29 17:40:17 +02:00
|
|
|
use:swipe
|
|
|
|
|
on:swipe={onSwipe}
|
2024-06-16 17:37:25 +02:00
|
|
|
on:canplay={(e) => handleCanPlay(e.currentTarget)}
|
2024-09-20 23:02:58 +02:00
|
|
|
on:ended={onVideoEnded}
|
2024-06-16 17:37:25 +02:00
|
|
|
on:volumechange={(e) => {
|
|
|
|
|
if (!forceMuted) {
|
|
|
|
|
$videoViewerMuted = e.currentTarget.muted;
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
muted={forceMuted || $videoViewerMuted}
|
2023-07-01 00:50:47 -04:00
|
|
|
bind:volume={$videoViewerVolume}
|
2024-05-31 13:44:04 -04:00
|
|
|
poster={getAssetThumbnailUrl({ id: assetId, size: AssetMediaSize.Preview, checksum })}
|
2024-10-08 11:42:19 +07:00
|
|
|
src={assetFileUrl}
|
2023-07-01 00:50:47 -04:00
|
|
|
>
|
|
|
|
|
</video>
|
2023-02-15 18:56:19 +01:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
{#if isVideoLoading}
|
2023-07-18 13:19:39 -05:00
|
|
|
<div class="absolute flex place-content-center place-items-center">
|
2023-07-01 00:50:47 -04:00
|
|
|
<LoadingSpinner />
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2022-06-04 18:34:11 -05:00
|
|
|
</div>
|