Adds initial player component
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { createEventDispatcher } from 'svelte';
|
||||||
|
import { usePlayer, type PlayerElement } from './audio';
|
||||||
|
|
||||||
|
// TODO: Abstract to a store so we can add actions like reqind 20s
|
||||||
|
|
||||||
|
export let src: string;
|
||||||
|
export let autoplay: boolean;
|
||||||
|
export let playbackRate: number = 1;
|
||||||
|
export let currentTime: number = 0;
|
||||||
|
export let paused: boolean = true;
|
||||||
|
export let volume: number = 0;
|
||||||
|
export let muted: boolean = false;
|
||||||
|
|
||||||
|
let audioElement: PlayerElement;
|
||||||
|
|
||||||
|
$: usePlayer(audioElement, 'playbackRate', playbackRate);
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher<{ play: void; pause: void }>();
|
||||||
|
|
||||||
|
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"
|
||||||
|
controls={true}
|
||||||
|
on:canplay={(e) => console.log('canplay')}
|
||||||
|
on:canplaythrough={(e) => console.log('canplaythrough')}
|
||||||
|
on:durationchange={(e) => console.log('durationchange')}
|
||||||
|
on:ended={(e) => console.log('ended')}
|
||||||
|
on:loadeddata={(e) => console.log('loaded data')}
|
||||||
|
on:loadedmetadata={(e) => console.log('loadedmetadata')}
|
||||||
|
on:loadstart={(e) => console.log('load start')}
|
||||||
|
on:pause={(e) => console.log('pause')}
|
||||||
|
on:play={(e) => console.log('play')}
|
||||||
|
on:playing={(e) => console.log('playing')}
|
||||||
|
on:ratechange={(e) => console.log('ratechange')}
|
||||||
|
on:seeked={(e) => console.log('seeked')}
|
||||||
|
on:seeking={(e) => console.log('seeking')}
|
||||||
|
on:suspend={(e) => console.log('suspend')}
|
||||||
|
on:timeupdate={(e) => console.log('timeupdate')}
|
||||||
|
on:volumechange={(e) => console.log('volumechange')}
|
||||||
|
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)}
|
||||||
|
/>
|
||||||
|
{/key}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
export type PlayerElement = HTMLAudioElement | undefined;
|
||||||
|
|
||||||
|
export function usePlayer<K extends keyof HTMLAudioElement>(
|
||||||
|
el: PlayerElement,
|
||||||
|
key: K,
|
||||||
|
value: HTMLAudioElement[K]
|
||||||
|
) {
|
||||||
|
if (!el) return;
|
||||||
|
el[key] = value;
|
||||||
|
}
|
||||||
+41
-3
@@ -1,3 +1,41 @@
|
|||||||
<h1>Welcome to your library project</h1>
|
<script>
|
||||||
<p>Create your package using @sveltejs/package and preview/showcase your work with SvelteKit</p>
|
import Audio from '$lib/components/audio.svelte';
|
||||||
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
|
|
||||||
|
let playbackRate = 1;
|
||||||
|
let volume = 0.5;
|
||||||
|
let paused = true;
|
||||||
|
|
||||||
|
let currentTime = 0;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>Demo</h1>
|
||||||
|
|
||||||
|
<h5>Native audio controls</h5>
|
||||||
|
<Audio src="/example.mp3" autoplay={false} {volume} {playbackRate} {paused} bind:currentTime />
|
||||||
|
|
||||||
|
<h5>Custom audio controls</h5>
|
||||||
|
|
||||||
|
<h6>Audio Actions</h6>
|
||||||
|
|
||||||
|
<button type="button" on:click={() => (paused = false)}>Play</button>
|
||||||
|
<button type="button" on:click={() => (paused = true)}>Pause</button>
|
||||||
|
<button type="button" on:click={() => (paused = !paused)}>Toggle</button>
|
||||||
|
|
||||||
|
<h6>Seeking</h6>
|
||||||
|
|
||||||
|
<button type="button" on:click={() => (currentTime = currentTime + 5)}>Skip 5s</button>
|
||||||
|
<button type="button" on:click={() => (currentTime = currentTime - 5)}>Rewind 5s</button>
|
||||||
|
|
||||||
|
<button type="button" on:click={() => (currentTime = 30)}>Go to 30s</button>
|
||||||
|
|
||||||
|
<h6>Playback Rate</h6>
|
||||||
|
|
||||||
|
{#each [0.5, 1, 2, 3] as rate}
|
||||||
|
<button type="button" on:click={() => (playbackRate = rate)}>{rate}x</button>
|
||||||
|
{/each}
|
||||||
|
|
||||||
|
<h6>Volume</h6>
|
||||||
|
|
||||||
|
{#each [0, 0.25, 0.5, 0.75, 1] as v}
|
||||||
|
<button type="button" on:click={() => (volume = v)}>{v * 100}%</button>
|
||||||
|
{/each}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user