Merge branch 'experimental/fix-adb' into develop

* experimental/fix-adb:
  Fix audiobook player
This commit is contained in:
acx10
2026-01-31 15:56:27 -07:00
4 changed files with 310 additions and 131 deletions
@@ -8,13 +8,11 @@
<!-- Hidden audio element -->
<audio
#audioElement
[src]="audioSrc"
(loadedmetadata)="onAudioLoaded()"
(timeupdate)="onTimeUpdate()"
(progress)="onProgress()"
(ended)="onAudioEnded()"
(error)="onAudioError()"
preload="none"
></audio>
<!-- Header -->
@@ -55,7 +55,8 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
private pageTitle = inject(PageTitleService);
isLoading = true;
audioLoading = true;
audioLoading = false;
audioInitialized = false;
bookId!: number;
audiobookInfo!: AudiobookInfo;
@@ -145,29 +146,13 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
this.resetState();
this.isLoading = true;
const audiobookInfo$ = this.audiobookService.getAudiobookInfo(this.bookId);
const book$ = this.bookService.getBookByIdFromAPI(this.bookId, false);
forkJoin([audiobookInfo$, book$]).subscribe({
next: ([info, book]) => {
this.audiobookService.getAudiobookInfo(this.bookId).subscribe({
next: (info) => {
this.audiobookInfo = info;
this.pageTitle.setBookPageTitle(book);
const token = this.authService.getInternalAccessToken() || this.authService.getOidcAccessToken();
this.bookCoverUrl = `${API_CONFIG.BASE_URL}/api/v1/media/cover/${this.bookId}?token=${encodeURIComponent(token || '')}`;
this.coverUrl = this.audiobookService.getEmbeddedCoverUrl(this.bookId);
if (book.audiobookProgress) {
this.savedPosition = book.audiobookProgress.positionMs
? book.audiobookProgress.positionMs / 1000
: 0;
}
if (info.folderBased && info.tracks && info.tracks.length > 0) {
const trackIndex = book.audiobookProgress?.trackIndex ?? 0;
this.currentTrackIndex = trackIndex;
this.loadTrack(trackIndex, false);
const track = info.tracks[trackIndex];
// Prepare the source URL but don't load it yet
this.audioSrc = this.audiobookService.getTrackStreamUrl(this.bookId, 0);
const track = info.tracks[0];
if (track?.durationMs) {
this.duration = track.durationMs / 1000;
}
@@ -178,14 +163,15 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
}
}
// Load cover URLs
const token = this.authService.getInternalAccessToken() || this.authService.getOidcAccessToken();
this.bookCoverUrl = `${API_CONFIG.BASE_URL}/api/v1/media/cover/${this.bookId}?token=${encodeURIComponent(token || '')}`;
this.coverUrl = this.audiobookService.getEmbeddedCoverUrl(this.bookId);
this.isLoading = false;
this.audioLoading = false;
if (this.savedPosition > 0) {
this.currentTime = this.savedPosition;
}
this.loadBookmarks();
// Load book details in parallel (for progress, title, etc.) - non-blocking
this.loadBookDetails(info);
},
error: () => {
this.messageService.add({
@@ -194,22 +180,77 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
detail: 'Failed to load audiobook'
});
this.isLoading = false;
this.audioLoading = false;
}
});
}
private loadBookDetails(info: AudiobookInfo): void {
this.bookService.getBookByIdFromAPI(this.bookId, false)
.pipe(takeUntil(this.destroy$))
.subscribe({
next: (book) => {
this.pageTitle.setBookPageTitle(book);
if (book.audiobookProgress) {
this.savedPosition = book.audiobookProgress.positionMs
? book.audiobookProgress.positionMs / 1000
: 0;
// If audio is already loaded, seek to saved position
const audio = this.audioElement?.nativeElement;
if (audio && audio.readyState >= 1 && this.savedPosition > 0) {
audio.currentTime = this.savedPosition;
this.currentTime = this.savedPosition;
}
// Handle track index for folder-based audiobooks
if (info.folderBased && info.tracks && info.tracks.length > 0) {
const trackIndex = book.audiobookProgress?.trackIndex ?? 0;
if (trackIndex !== this.currentTrackIndex) {
this.currentTrackIndex = trackIndex;
this.loadTrack(trackIndex, false);
const track = info.tracks[trackIndex];
if (track?.durationMs) {
this.duration = track.durationMs / 1000;
}
}
}
}
if (this.savedPosition > 0) {
this.currentTime = this.savedPosition;
}
},
error: () => {
// Non-critical - audiobook can still play without book details
}
});
this.loadBookmarks();
}
private loadTrack(index: number, showLoading = true): void {
if (!this.audiobookInfo.tracks || index < 0 || index >= this.audiobookInfo.tracks.length) {
return;
}
this.currentTrackIndex = index;
this.audioSrc = this.audiobookService.getTrackStreamUrl(this.bookId, index);
this.audioLoading = showLoading;
this.buffered = 0;
const track = this.audiobookInfo.tracks[index];
if (track?.durationMs) {
this.duration = track.durationMs / 1000;
}
// Only set the audio source if already initialized
if (this.audioInitialized) {
this.audioLoading = showLoading;
const audio = this.audioElement?.nativeElement;
if (audio) {
audio.src = this.audioSrc;
audio.load();
}
}
}
private resetState(): void {
@@ -222,7 +263,8 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
this.savedPosition = 0;
this.currentTrackIndex = 0;
this.audioSrc = '';
this.audioLoading = true;
this.audioLoading = false;
this.audioInitialized = false;
this.showTrackList = false;
this.coverUrl = undefined;
@@ -380,6 +422,36 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
const audio = this.audioElement?.nativeElement;
if (!audio) return;
// Initialize audio on first play
if (!this.audioInitialized) {
this.audioLoading = true;
this.audioInitialized = true;
audio.src = this.audioSrc;
audio.load();
// Wait for enough data to play
const playWhenReady = () => {
audio.removeEventListener('canplay', playWhenReady);
audio.play();
this.isPlaying = true;
this.startProgressSaveInterval();
this.updateMediaSessionPlaybackState();
if (this.audiobookSessionService.isSessionActive()) {
this.audiobookSessionService.resumeSession(Math.round(this.currentTime * 1000));
} else {
this.audiobookSessionService.startSession(
this.bookId,
Math.round(this.currentTime * 1000),
this.playbackRate,
this.audiobookInfo?.bookFileId,
this.audiobookInfo?.folderBased ? this.currentTrackIndex : undefined
);
}
};
audio.addEventListener('canplay', playWhenReady);
return;
}
if (this.isPlaying) {
audio.pause();
this.stopProgressSaveInterval();
@@ -499,10 +571,25 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
}
selectTrack(track: AudiobookTrack): void {
this.loadTrack(track.index);
this.currentTrackIndex = track.index;
this.audioSrc = this.audiobookService.getTrackStreamUrl(this.bookId, track.index);
this.currentTime = 0;
this.savedPosition = 0;
this.buffered = 0;
const trackInfo = this.audiobookInfo.tracks?.[track.index];
if (trackInfo?.durationMs) {
this.duration = trackInfo.durationMs / 1000;
}
this.showTrackList = false;
const audio = this.audioElement?.nativeElement;
if (audio) {
this.audioLoading = true;
this.audioInitialized = true;
audio.src = this.audioSrc;
audio.load();
}
setTimeout(() => {
this.audioElement?.nativeElement?.play();
this.isPlaying = true;
@@ -522,15 +609,23 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
selectChapter(chapter: AudiobookChapter): void {
const audio = this.audioElement?.nativeElement;
if (audio) {
audio.currentTime = chapter.startTimeMs / 1000;
this.currentTime = chapter.startTimeMs / 1000;
this.showTrackList = false;
this.updateMediaSessionMetadata();
if (!this.isPlaying) {
if (!audio) return;
// Initialize audio if not yet done
if (!this.audioInitialized) {
this.audioLoading = true;
this.audioInitialized = true;
audio.src = this.audioSrc;
audio.load();
const seekAndPlay = () => {
audio.removeEventListener('canplay', seekAndPlay);
audio.currentTime = chapter.startTimeMs / 1000;
this.currentTime = chapter.startTimeMs / 1000;
audio.play();
this.isPlaying = true;
this.startProgressSaveInterval();
this.updateMediaSessionMetadata();
this.updateMediaSessionPlaybackState();
if (this.audiobookSessionService.isSessionActive()) {
this.audiobookSessionService.resumeSession(chapter.startTimeMs);
@@ -540,6 +635,28 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
this.audiobookInfo?.bookFileId
);
}
};
audio.addEventListener('canplay', seekAndPlay);
this.showTrackList = false;
return;
}
audio.currentTime = chapter.startTimeMs / 1000;
this.currentTime = chapter.startTimeMs / 1000;
this.showTrackList = false;
this.updateMediaSessionMetadata();
if (!this.isPlaying) {
audio.play();
this.isPlaying = true;
this.startProgressSaveInterval();
this.updateMediaSessionPlaybackState();
if (this.audiobookSessionService.isSessionActive()) {
this.audiobookSessionService.resumeSession(chapter.startTimeMs);
} else {
this.audiobookSessionService.startSession(
this.bookId, chapter.startTimeMs, this.playbackRate,
this.audiobookInfo?.bookFileId
);
}
}
}
@@ -876,23 +993,57 @@ export class AudiobookReaderComponent implements OnInit, OnDestroy {
}
goToBookmark(bookmark: BookMark): void {
const audio = this.audioElement?.nativeElement;
if (!audio) return;
const targetPosition = (bookmark.positionMs || 0) / 1000;
// Initialize audio if not yet done
if (!this.audioInitialized) {
if (this.audiobookInfo.folderBased && bookmark.trackIndex !== undefined && bookmark.trackIndex !== null) {
this.currentTrackIndex = bookmark.trackIndex;
this.audioSrc = this.audiobookService.getTrackStreamUrl(this.bookId, bookmark.trackIndex);
}
this.audioLoading = true;
this.audioInitialized = true;
audio.src = this.audioSrc;
audio.load();
const seekAndPlay = () => {
audio.removeEventListener('canplay', seekAndPlay);
audio.currentTime = targetPosition;
this.currentTime = targetPosition;
audio.play();
this.isPlaying = true;
this.startProgressSaveInterval();
const positionMs = bookmark.positionMs || 0;
if (this.audiobookSessionService.isSessionActive()) {
this.audiobookSessionService.resumeSession(positionMs);
} else {
this.audiobookSessionService.startSession(
this.bookId, positionMs, this.playbackRate,
this.audiobookInfo?.bookFileId,
this.audiobookInfo?.folderBased ? bookmark.trackIndex : undefined
);
}
};
audio.addEventListener('canplay', seekAndPlay);
this.showBookmarkList = false;
return;
}
if (this.audiobookInfo.folderBased && bookmark.trackIndex !== undefined && bookmark.trackIndex !== null) {
if (bookmark.trackIndex !== this.currentTrackIndex) {
this.loadTrack(bookmark.trackIndex);
this.savedPosition = (bookmark.positionMs || 0) / 1000;
this.savedPosition = targetPosition;
} else {
const audio = this.audioElement?.nativeElement;
if (audio && bookmark.positionMs) {
audio.currentTime = bookmark.positionMs / 1000;
this.currentTime = bookmark.positionMs / 1000;
}
audio.currentTime = targetPosition;
this.currentTime = targetPosition;
}
} else {
const audio = this.audioElement?.nativeElement;
if (audio && bookmark.positionMs) {
audio.currentTime = bookmark.positionMs / 1000;
this.currentTime = bookmark.positionMs / 1000;
}
audio.currentTime = targetPosition;
this.currentTime = targetPosition;
}
this.showBookmarkList = false;