From d1c1481a3b086a129928771c11977f9eb3516dc6 Mon Sep 17 00:00:00 2001 From: William Chong Date: Wed, 25 Mar 2026 14:40:05 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20client-side=20TTS=20pre?= =?UTF-8?q?fetch=20in=20favour=20of=20native=20app=20control?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move segment prefetching responsibility to the native app layer, which has better visibility into playback state and buffer needs. --- composables/use-native-audio-player.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/composables/use-native-audio-player.ts b/composables/use-native-audio-player.ts index 00d86b4e..b60a1c47 100644 --- a/composables/use-native-audio-player.ts +++ b/composables/use-native-audio-player.ts @@ -3,17 +3,6 @@ import { useEventListener } from '@vueuse/core' export function useNativeAudioPlayer(isActive: Ref): TTSAudioPlayer { const handlers: Partial<{ [K in keyof TTSAudioPlayerEvents]: TTSAudioPlayerEvents[K] }> = {} let loaded = false - let cachedSegments: TTSSegment[] = [] - let cachedGetAudioSrc: ((segment: TTSSegment) => string) | null = null - - function prefetchSegment(index: number) { - if (!cachedGetAudioSrc || index < 0 || index >= cachedSegments.length) return - const url = cachedGetAudioSrc(cachedSegments[index]!) - // Fire-and-forget: warm server-side cache with minimal transfer. - // blocking=1 makes the server generate & cache the full buffer; Range - // header limits the response to 1 byte so we don't download the audio. - fetch(url, { headers: { Range: 'bytes=0-0' } }).catch(() => {}) - } function on(event: K, handler: TTSAudioPlayerEvents[K]) { handlers[event] = handler @@ -43,7 +32,6 @@ export function useNativeAudioPlayer(isActive: Ref): TTSAud case 'trackChanged': if (typeof detail.index === 'number') { handlers.trackChanged?.(detail.index) - prefetchSegment(detail.index + 1) } break case 'queueEnded': @@ -53,7 +41,6 @@ export function useNativeAudioPlayer(isActive: Ref): TTSAud case 'remotePrevious': if (typeof detail.index === 'number') { handlers.trackChanged?.(detail.index) - prefetchSegment(detail.index + 1) } break case 'error': @@ -69,9 +56,6 @@ export function useNativeAudioPlayer(isActive: Ref): TTSAud rate: number metadata: { bookTitle: string, authorName: string, coverUrl: string } }) { - cachedSegments = options.segments - cachedGetAudioSrc = options.getAudioSrc - const origin = window.location.origin const tracks = options.segments.map((segment, i) => ({ index: i, @@ -87,7 +71,6 @@ export function useNativeAudioPlayer(isActive: Ref): TTSAud metadata: options.metadata, }) loaded = true - prefetchSegment(options.startIndex + 1) } function resume(): boolean { @@ -103,8 +86,6 @@ export function useNativeAudioPlayer(isActive: Ref): TTSAud function stop() { postToNative({ type: 'stop' }) loaded = false - cachedSegments = [] - cachedGetAudioSrc = null } function skipTo(index: number) {