feat(ui): auto-next chapter navigation with auto-start audio

When autoNext is enabled, audio automatically advances to the next chapter
when a track ends — navigating the page and starting the new chapter's audio.

- audioStore: add autoNext (toggle flag), nextChapter (written by AudioPlayer),
  and autoStartPending (set before goto, cleared after auto-start fires)
- layout: update onended to call goto() and set autoStartPending when autoNext
  is on and nextChapter is available; add auto-next toggle button to mini-player
  bar (double-chevron icon, amber when active)
- AudioPlayer: accept nextChapter prop; write it to audioStore via $effect;
  auto-start playback when autoStartPending is set on component mount; show
  inline 'Auto' toggle button in ready controls when a next chapter exists
- Chapter page: pass data.next as nextChapter prop to AudioPlayer
This commit is contained in:
Admin
2026-03-04 16:01:26 +05:00
parent 901b18ee13
commit b87e758303
4 changed files with 85 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
<script lang="ts">
import '../app.css';
import { page } from '$app/state';
import { goto } from '$app/navigation';
import type { Snippet } from 'svelte';
import type { LayoutData } from './$types';
import { audioStore } from '$lib/audio.svelte';
@@ -122,7 +123,13 @@
bind:duration={audioStore.duration}
onplay={() => (audioStore.isPlaying = true)}
onpause={() => (audioStore.isPlaying = false)}
onended={() => (audioStore.isPlaying = false)}
onended={() => {
audioStore.isPlaying = false;
if (audioStore.autoNext && audioStore.nextChapter !== null && audioStore.slug) {
audioStore.autoStartPending = true;
goto(`/books/${audioStore.slug}/chapters/${audioStore.nextChapter}`);
}
}}
preload="metadata"
style="display:none"
></audio>
@@ -278,6 +285,22 @@
>
{audioStore.speed}×
</button>
<!-- Auto-next toggle -->
<button
onclick={() => (audioStore.autoNext = !audioStore.autoNext)}
class="p-1.5 rounded flex-shrink-0 transition-colors {audioStore.autoNext
? 'text-amber-400 bg-amber-400/15 hover:bg-amber-400/25'
: 'text-zinc-600 hover:text-zinc-300 hover:bg-zinc-800'}"
title={audioStore.autoNext ? 'Auto-next on' : 'Auto-next off'}
aria-label="Auto-next {audioStore.autoNext ? 'on' : 'off'}"
aria-pressed={audioStore.autoNext}
>
<!-- "skip to end" / auto-advance icon -->
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
<path d="M6 18l8.5-6L6 6v12zm8.5-6L23 6v12l-8.5-6z"/>
</svg>
</button>
{:else if audioStore.status === 'generating'}
<!-- Spinner during generation -->
<svg class="w-6 h-6 text-amber-400 animate-spin flex-shrink-0" fill="none" viewBox="0 0 24 24">

View File

@@ -72,6 +72,7 @@
chapter={data.chapter.number}
chapterTitle={data.chapter.title || `Chapter ${data.chapter.number}`}
bookTitle={data.book.title}
nextChapter={data.next}
/>
<!-- Chapter content -->