Disable autoplay (#51)

This commit is contained in:
Ollie Taylor
2023-07-29 20:11:22 +01:00
committed by GitHub
parent 528457fe59
commit f42a00f04c
9 changed files with 19 additions and 31 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'svelte-podcast': minor
---
removes autoplay as it doesn't behave as users would expect
+1 -3
View File
@@ -35,7 +35,6 @@ export const audio_element: Readable<HTMLAudioElement | null> = derived(
el.currentTime = $audio_state.start_at; el.currentTime = $audio_state.start_at;
el.playbackRate = $audio_state.playback_rate || 1; el.playbackRate = $audio_state.playback_rate || 1;
el.volume = $audio_state.volume || 1; el.volume = $audio_state.volume || 1;
el.autoplay = $audio_state.autoplay || false;
} }
// If a new HTMLAudioElement was created, append it to the body. // If a new HTMLAudioElement was created, append it to the body.
@@ -44,11 +43,10 @@ export const audio_element: Readable<HTMLAudioElement | null> = derived(
// Define a function to set the HTMLAudioElement and call it. // Define a function to set the HTMLAudioElement and call it.
const handle_update = () => { const handle_update = () => {
set(el); set(el);
$audio_state?.autoplay && el.play();
announce.info('Updating audio element');
}; };
handle_update(); handle_update();
announce.info('Setting audio element');
// Add event listeners to the HTMLAudioElement to update the store when it changes. // Add event listeners to the HTMLAudioElement to update the store when it changes.
el.addEventListener('loadeddata', handle_update); el.addEventListener('loadeddata', handle_update);
-3
View File
@@ -1,7 +1,5 @@
import { writable, type Writable } from 'svelte/store'; import { writable, type Writable } from 'svelte/store';
// TODO: implement autoplay
/** /**
* An object representing the state of an audio player. * An object representing the state of an audio player.
*/ */
@@ -10,7 +8,6 @@ interface AudioState {
start_at: number; start_at: number;
playback_rate?: number; playback_rate?: number;
volume?: number; volume?: number;
autoplay?: boolean;
} }
/** A writable Svelte store containing the audio state. */ /** A writable Svelte store containing the audio state. */
+4 -8
View File
@@ -15,7 +15,7 @@ import { user_progress } from './user-progress';
* @param src - Audio source * @param src - Audio source
* @param metadata - Audio metadata * @param metadata - Audio metadata
*/ */
const load_src = (src: string, metadata: AudioMetadata, autoplay = false) => { const load_src = (src: string, metadata: AudioMetadata) => {
if (!BROWSER) return; if (!BROWSER) return;
// Save the current progress if audio is already loaded // Save the current progress if audio is already loaded
@@ -30,7 +30,6 @@ const load_src = (src: string, metadata: AudioMetadata, autoplay = false) => {
start_at: user_progress.get(src) || 0, start_at: user_progress.get(src) || 0,
playback_rate: preferences.playback_rate, playback_rate: preferences.playback_rate,
volume: preferences.volume, volume: preferences.volume,
autoplay: autoplay,
}); });
// Set metadata for current audio // Set metadata for current audio
@@ -59,12 +58,11 @@ const unload_src = () => {
*/ */
const play = (playing: boolean | 'toggle' = 'toggle') => { const play = (playing: boolean | 'toggle' = 'toggle') => {
const el = use_audio_element('play'); const el = use_audio_element('play');
console.log(el, el.paused);
if (typeof playing === 'boolean') { if (typeof playing === 'boolean') {
playing ? el.play() : el.pause(); playing ? el.play() : el.pause();
} } else {
if (playing === 'toggle') {
el.paused ? el.play() : el.pause(); el.paused ? el.play() : el.pause();
} }
}; };
@@ -78,9 +76,7 @@ const mute = (muted: boolean | 'toggle' = 'toggle') => {
if (typeof muted === 'boolean') { if (typeof muted === 'boolean') {
el.muted = muted; el.muted = muted;
} } else {
if (muted === 'toggle') {
el.muted = !el.muted; el.muted = !el.muted;
} }
}; };
+8 -5
View File
@@ -7,15 +7,16 @@ import { format_seconds } from './utility/format-seconds';
/** /**
* An object representing the attributes of an audio element. * An object representing the attributes of an audio element.
*/ */
export interface AudioAttributes { export type AudioAttributes = {
is_loaded: boolean;
is_paused: boolean;
current_time: number; current_time: number;
duration: number; duration: number;
is_loaded: boolean;
is_paused: boolean;
metadata: AudioMetadata | null;
src: string | null;
timestamp_current: string; timestamp_current: string;
timestamp_end: string; timestamp_end: string;
metadata: AudioMetadata | null; };
}
/** A readable Svelte store containing the audio attributes. */ /** A readable Svelte store containing the audio attributes. */
export const audio_attributes: Readable<AudioAttributes> = derived( export const audio_attributes: Readable<AudioAttributes> = derived(
@@ -23,6 +24,7 @@ export const audio_attributes: Readable<AudioAttributes> = derived(
([$el, $meta], set) => { ([$el, $meta], set) => {
if (!$el) { if (!$el) {
return set({ return set({
src: null,
is_loaded: false, is_loaded: false,
is_paused: true, is_paused: true,
current_time: 0, current_time: 0,
@@ -34,6 +36,7 @@ export const audio_attributes: Readable<AudioAttributes> = derived(
} }
set({ set({
src: $el.src,
is_loaded: Boolean($el.src), is_loaded: Boolean($el.src),
is_paused: $el.paused, is_paused: $el.paused,
current_time: $el.currentTime, current_time: $el.currentTime,
-4
View File
@@ -1,5 +1,3 @@
import { announce } from '../_internal_/announce';
/** /**
* Returns a string timestamp in the format of "hh:mm:ss" from a given number of seconds. * Returns a string timestamp in the format of "hh:mm:ss" from a given number of seconds.
* @param seconds - The number of seconds to convert to a timestamp. * @param seconds - The number of seconds to convert to a timestamp.
@@ -14,8 +12,6 @@ function seconds_to_timestamp(
): string { ): string {
const safe_seconds = Number(seconds) || 0; const safe_seconds = Number(seconds) || 0;
announce.info({ seconds, force_hours, seperator, safe_seconds });
const hours = Math.floor(safe_seconds / 3600); const hours = Math.floor(safe_seconds / 3600);
const hours_remainder = safe_seconds % 3600; const hours_remainder = safe_seconds % 3600;
-3
View File
@@ -13,9 +13,6 @@ audio.src.load(
guests: ['Rich Harris'], guests: ['Rich Harris'],
hosts: ['Wes Bos', 'Scott Tolinski'], hosts: ['Wes Bos', 'Scott Tolinski'],
}, },
// autoplay
false,
); );
// unload the current audio source // unload the current audio source
+1 -2
View File
@@ -68,8 +68,7 @@
<p> <p>
Along side your audio source, you can optionally define arbitrary Along side your audio source, you can optionally define arbitrary
metadata for you to use in your player, and determine if the episode metadata for you to use in your player. Learn more in the <a
should autoplay once loaded. Learn more in the <a
href="/api/#audio.src">api docs</a href="/api/#audio.src">api docs</a
> >
</p> </p>
@@ -10,7 +10,4 @@ audio.src.load(
artwork: '/artwork.png', artwork: '/artwork.png',
guest_name: 'OllieJT', guest_name: 'OllieJT',
}, },
// Autoplay once loaded
false,
); );