2023-02-21 23:54:18 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
|
import { usePlayer, type PlayerElement } from './audio';
|
2023-02-22 00:36:40 +00:00
|
|
|
import AudioA11yProgress from '$lib/components/audio-a11y-progress.svelte';
|
2023-02-21 23:54:18 +00:00
|
|
|
|
|
|
|
|
// TODO: Abstract to a store so we can add actions like reqind 20s
|
|
|
|
|
|
|
|
|
|
export let src: string;
|
|
|
|
|
export let autoplay: boolean;
|
2023-02-22 00:25:18 +00:00
|
|
|
|
2023-02-21 23:54:18 +00:00
|
|
|
export let currentTime: number = 0;
|
2023-02-22 00:25:18 +00:00
|
|
|
export let muted: boolean = false;
|
2023-02-21 23:54:18 +00:00
|
|
|
export let paused: boolean = true;
|
2023-02-22 00:25:18 +00:00
|
|
|
export let playbackRate: number = 1;
|
2023-02-21 23:54:18 +00:00
|
|
|
export let volume: number = 0;
|
|
|
|
|
|
|
|
|
|
let audioElement: PlayerElement;
|
|
|
|
|
|
|
|
|
|
$: usePlayer(audioElement, 'playbackRate', playbackRate);
|
|
|
|
|
|
2023-02-22 00:25:18 +00:00
|
|
|
const dispatch = createEventDispatcher<{
|
|
|
|
|
playing: void;
|
|
|
|
|
finished: void;
|
|
|
|
|
paused: { currentTime: number };
|
|
|
|
|
progress: { currentTime: number };
|
|
|
|
|
seek: { currentTime: number };
|
|
|
|
|
rateChange: { playbackRate: number };
|
|
|
|
|
}>();
|
2023-02-21 23:54:18 +00:00
|
|
|
|
|
|
|
|
let duration: number = 0;
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
{#key src}
|
|
|
|
|
<audio
|
|
|
|
|
bind:this={audioElement}
|
|
|
|
|
{src}
|
|
|
|
|
{autoplay}
|
|
|
|
|
bind:currentTime
|
|
|
|
|
bind:duration
|
|
|
|
|
bind:paused
|
|
|
|
|
bind:muted
|
|
|
|
|
bind:volume
|
|
|
|
|
preload="metadata"
|
2023-02-22 00:25:18 +00:00
|
|
|
controls={false}
|
|
|
|
|
on:timeupdate={(e) => {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
const timeNow = e.target?.currentTime;
|
|
|
|
|
if (typeof timeNow != 'number') return;
|
|
|
|
|
dispatch('progress', { currentTime: timeNow });
|
|
|
|
|
}}
|
|
|
|
|
on:playing={(e) => dispatch('playing')}
|
|
|
|
|
on:pause={(e) => dispatch('paused', { currentTime })}
|
|
|
|
|
on:ratechange={(e) => dispatch('rateChange', { playbackRate })}
|
|
|
|
|
on:ended={(e) => dispatch('finished')}
|
|
|
|
|
on:seeked={(e) => dispatch('seek', { currentTime })}
|
2023-02-21 23:54:18 +00:00
|
|
|
on:durationchange={(e) => console.log('durationchange')}
|
|
|
|
|
on:loadeddata={(e) => console.log('loaded data')}
|
|
|
|
|
on:loadedmetadata={(e) => console.log('loadedmetadata')}
|
|
|
|
|
on:loadstart={(e) => console.log('load start')}
|
|
|
|
|
on:seeking={(e) => console.log('seeking')}
|
|
|
|
|
on:suspend={(e) => console.log('suspend')}
|
|
|
|
|
on:volumechange={(e) => console.log('volumechange')}
|
2023-02-22 00:25:18 +00:00
|
|
|
on:play={(e) => console.log('play')}
|
|
|
|
|
on:canplay={(e) => console.log('canplay')}
|
|
|
|
|
on:canplaythrough={(e) => console.log('canplaythrough')}
|
2023-02-21 23:54:18 +00:00
|
|
|
on:waiting={(e) => console.log('waiting')}
|
|
|
|
|
on:stalled={(e) => console.log('stalled', e)}
|
|
|
|
|
on:emptied={(e) => console.log('emptied', e)}
|
|
|
|
|
on:load={(e) => console.log('load', e)}
|
|
|
|
|
/>
|
2023-02-22 00:36:40 +00:00
|
|
|
<AudioA11yProgress {currentTime} {paused} {duration} />
|
2023-02-22 00:29:57 +00:00
|
|
|
<slot {duration} />
|
2023-02-21 23:54:18 +00:00
|
|
|
{/key}
|