Vaani/lib/features/player/core/audiobook_player.dart

406 lines
13 KiB
Dart
Raw Normal View History

2024-05-14 06:13:16 -04:00
/// a wrapper around the audioplayers package to manage the audio player instance
///
/// this is needed as audiobook can be a list of audio files instead of a single file
library;
2024-08-20 08:36:39 -04:00
import 'package:collection/collection.dart';
2024-05-17 11:04:20 -04:00
import 'package:just_audio/just_audio.dart';
2025-11-19 17:43:04 +08:00
// import 'package:just_audio_background/just_audio_background.dart';
2024-08-20 08:36:39 -04:00
import 'package:logging/logging.dart';
2024-05-14 06:13:16 -04:00
import 'package:shelfsdk/audiobookshelf_api.dart';
2025-11-13 17:53:23 +08:00
import 'package:vaani/settings/app_settings_provider.dart';
import 'package:vaani/settings/models/app_settings.dart';
import 'package:vaani/shared/extensions/model_conversions.dart';
2024-05-14 06:13:16 -04:00
2024-08-20 08:36:39 -04:00
final _logger = Logger('AudiobookPlayer');
2025-11-13 17:53:23 +08:00
// add a small offset so the display does not show the previous chapter for a split second
final offset = Duration(milliseconds: 10);
/// time into the current chapter to determine if we should go to the previous chapter or the start of the current chapter
final doNotSeekBackIfLessThan = Duration(seconds: 5);
2024-05-19 08:53:21 -04:00
/// returns the sum of the duration of all the previous tracks before the [index]
Duration sumOfTracks(BookExpanded book, int? index) {
_logger.fine('Calculating sum of tracks for index: $index');
2024-05-19 08:53:21 -04:00
// return 0 if index is less than 0
if (index == null || index < 0) {
_logger.warning('Index is null or less than 0, returning 0');
2024-05-19 08:53:21 -04:00
return Duration.zero;
}
final total = book.tracks.sublist(0, index).fold<Duration>(
Duration.zero,
(previousValue, element) => previousValue + element.duration,
);
_logger.fine('Sum of tracks for index: $index is $total');
return total;
2024-05-19 08:53:21 -04:00
}
2024-05-14 06:13:16 -04:00
/// will manage the audio player instance
class AudiobookPlayer extends AudioPlayer {
// constructor which takes in the BookExpanded object
2024-05-17 11:04:20 -04:00
AudiobookPlayer(this.token, this.baseUrl) : super() {
2024-05-14 06:13:16 -04:00
// set the source of the player to the first track in the book
_logger.config('Setting up audiobook player');
2025-11-13 17:53:23 +08:00
// playerStateStream.listen((playerState) {
// if (playerState.processingState == ProcessingState.completed) {
// Future.microtask(seekToNext);
// }
// });
2024-05-14 06:13:16 -04:00
}
/// the [BookExpanded] being played
BookExpanded? _book;
2024-05-19 08:53:21 -04:00
// /// the [BookExpanded] trying to be played
// BookExpanded? _intended_book;
2024-05-14 06:13:16 -04:00
/// the [BookExpanded] being played
///
2024-08-20 08:36:39 -04:00
/// to set the book, use [setSourceAudiobook]
2024-05-14 06:13:16 -04:00
BookExpanded? get book => _book;
/// the authentication token to access the [AudioTrack.contentUrl]
final String token;
/// the base url for the audio files
final Uri baseUrl;
// the current index of the audio file in the [book]
2025-11-13 17:53:23 +08:00
// int _currentIndex = 0;
2024-05-14 06:13:16 -04:00
// available audio tracks
int? get availableTracks => _book?.tracks.length;
2024-05-14 06:13:16 -04:00
/// sets the current [AudioTrack] as the source of the player
2024-08-20 08:36:39 -04:00
Future<void> setSourceAudiobook(
2024-05-19 08:53:21 -04:00
BookExpanded? book, {
bool preload = true,
2025-11-13 17:53:23 +08:00
int? initialIndex,
2024-05-19 08:53:21 -04:00
Duration? initialPosition,
2024-08-20 08:36:39 -04:00
List<Uri>? downloadedUris,
Uri? artworkUri,
2024-05-19 08:53:21 -04:00
}) async {
_logger.finer(
'Initial position: $initialPosition, Downloaded URIs: $downloadedUris',
);
2025-11-13 17:53:23 +08:00
final appSettings = loadOrCreateAppSettings();
2024-05-14 10:11:25 -04:00
if (book == null) {
_book = null;
2024-08-20 08:36:39 -04:00
_logger.info('Book is null, stopping player');
2024-05-14 10:11:25 -04:00
return stop();
}
2024-05-14 06:13:16 -04:00
if (_book == book) {
2024-08-20 08:36:39 -04:00
_logger.info('Book is the same, doing nothing');
2024-05-14 06:13:16 -04:00
return;
}
_logger.info('Setting source for book: $book');
_logger.fine('Stopping player');
2024-05-14 10:11:25 -04:00
await stop();
2024-05-14 06:13:16 -04:00
2024-05-19 08:53:21 -04:00
_book = book;
// some calculations to set the initial index and position
// initialPosition is of the entire book not just the current track
// hence first we need to calculate the current track which will be used to set the initial position
// then we set the initial index to the current track index and position as the remaining duration from the position
// after subtracting the duration of all the previous tracks
2024-05-19 09:45:41 -04:00
// initialPosition ;
2025-11-13 17:53:23 +08:00
final trackToPlay =
_book!.findTrackAtTime(initialPosition ?? Duration.zero);
2025-10-24 16:54:58 +08:00
2024-05-19 08:53:21 -04:00
final initialIndex = book.tracks.indexOf(trackToPlay);
2025-10-25 10:38:56 +08:00
final initialPositionInTrack = initialPosition != null
? initialPosition - trackToPlay.startOffset
: null;
2025-11-13 17:53:23 +08:00
_logger.finer('Setting audioSource');
final playlist = book.tracks.map((track) {
final retrievedUri =
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token);
// _logger.fine(
// 'Setting source for track: ${track.title}, URI: ${retrievedUri.obfuscate()}',
// );
return AudioSource.uri(
retrievedUri,
2025-11-19 17:43:04 +08:00
// tag: MediaItem(
// // Specify a unique ID for each media item:
// id: book.libraryItemId + track.index.toString(),
// // Metadata to display in the notification:
// title: appSettings.notificationSettings.primaryTitle
// .formatNotificationTitle(book),
// album: appSettings.notificationSettings.secondaryTitle
// .formatNotificationTitle(book),
// artUri: artworkUri ??
// Uri.parse(
// '$baseUrl/api/items/${book.libraryItemId}/cover?token=$token&width=800',
// ),
// ),
2025-11-13 17:53:23 +08:00
);
}).toList();
await setAudioSources(
playlist,
preload: preload,
initialIndex: initialIndex,
initialPosition: initialPositionInTrack,
).catchError((error) {
_logger.shout('Error in setting audio source: $error');
return null;
});
2024-05-14 06:13:16 -04:00
}
/// toggles the player between play and pause
Future<void> togglePlayPause() {
// check if book is set
if (_book == null) {
_logger.warning('No book is set, not toggling play/pause');
2024-05-14 06:13:16 -04:00
}
2024-05-19 09:45:41 -04:00
// TODO refactor this to cover all the states
2024-05-17 11:04:20 -04:00
return switch (playerState) {
PlayerState(playing: var isPlaying) => isPlaying ? pause() : play(),
};
2024-05-14 10:11:25 -04:00
}
/// need to override getDuration and getCurrentPosition to return according to the book instead of the current track
/// this is because the book can be a list of audio files and the player is only aware of the current track
/// so we need to calculate the duration and current position based on the book
2025-11-13 17:53:23 +08:00
Future<void> seekInBook(Duration globalPosition) async {
2024-05-19 08:53:21 -04:00
if (_book == null) {
_logger.warning('No book is set, not seeking');
2024-05-19 08:53:21 -04:00
return;
}
2025-11-13 17:53:23 +08:00
// 找到目标音轨和在音轨内的位置
final track = _book!.findTrackAtTime(globalPosition);
final index = _book!.tracks.indexOf(track);
Duration positionInTrack = globalPosition - track.startOffset;
if (positionInTrack <= Duration.zero) {
positionInTrack = offset;
2024-05-19 08:53:21 -04:00
}
2025-11-13 17:53:23 +08:00
// 切换到目标音轨具体位置
if (index != currentIndex) {
await seek(positionInTrack, index: index);
}
await seek(positionInTrack);
}
2025-11-13 17:53:23 +08:00
// 核心功能:跳转到指定章节
Future<void> skipToChapter(int chapterId, {Duration? position}) async {
if (_book == null) return;
2025-11-13 17:53:23 +08:00
final chapter = _book!.chapters.firstWhere(
(ch) => ch.id == chapterId,
orElse: () => throw Exception('Chapter not found'),
);
if (position != null) {
print('章节开头: ${chapter.start}');
print('章节开头: ${chapter.start + position}');
await seekInBook(chapter.start + position);
2025-10-31 17:21:27 +08:00
return;
}
2025-11-13 17:53:23 +08:00
await seekInBook(chapter.start + offset);
2024-05-19 08:53:21 -04:00
}
2025-10-31 17:21:27 +08:00
@override
2025-11-13 17:53:23 +08:00
Future<void> seekToNext() async {
if (_book == null) {
// 回退到默认行为
return super.seekToNext();
}
final chapter = currentChapter;
if (chapter == null) {
// 回退到默认行为
return super.seekToNext();
}
final currentIndex = _book!.chapters.indexOf(chapter);
if (currentIndex < _book!.chapters.length - 1) {
// 跳到下一章
final nextChapter = _book!.chapters[currentIndex + 1];
await skipToChapter(nextChapter.id);
2025-10-31 17:21:27 +08:00
}
}
@override
2025-11-13 17:53:23 +08:00
Future<void> seekToPrevious() async {
if (_book == null) {
return super.seekToPrevious();
}
final chapter = currentChapter;
if (chapter == null) {
return super.seekToPrevious();
}
final currentIndex = _book!.chapters.indexOf(chapter);
if (currentIndex > 0) {
// 跳到上一章
final prevChapter = _book!.chapters[currentIndex - 1];
await skipToChapter(prevChapter.id);
} else {
// 已经是第一章,回到开头
await seekInBook(Duration.zero);
2025-10-31 17:21:27 +08:00
}
}
2024-05-19 09:45:41 -04:00
/// a convenience method to get position in the book instead of the current track position
Duration get positionInBook {
2025-11-13 17:53:23 +08:00
if (_book == null || currentIndex == null) {
2024-05-19 09:45:41 -04:00
return Duration.zero;
}
2025-11-13 17:53:23 +08:00
return position + _book!.tracks[currentIndex!].startOffset;
2025-10-24 16:54:58 +08:00
// return position + _book!.tracks[sequenceState!.currentIndex].startOffset;
2024-05-19 09:45:41 -04:00
}
/// a convenience method to get the buffered position in the book instead of the current track position
Duration get bufferedPositionInBook {
2025-11-13 17:53:23 +08:00
if (_book == null || currentIndex == null) {
2024-05-19 09:45:41 -04:00
return Duration.zero;
}
2025-11-13 17:53:23 +08:00
return bufferedPosition + _book!.tracks[currentIndex!].startOffset;
2025-10-24 16:54:58 +08:00
// return bufferedPosition + _book!.tracks[sequenceState!.currentIndex].startOffset;
2024-05-19 09:45:41 -04:00
}
2025-11-13 17:53:23 +08:00
// 章节进度
Stream<Duration> get positionStreamInChapter {
return super.positionStream.map((position) {
if (_book == null || currentIndex == null) {
return Duration.zero;
}
final globalPosition =
position + _book!.tracks[currentIndex!].startOffset;
final chapter = _book!.findChapterAtTime(globalPosition);
return globalPosition - chapter.start;
});
}
2024-05-19 08:53:21 -04:00
/// streams to override to suit the book instead of the current track
// - positionStream
// - bufferedPositionStream
2025-10-24 15:42:33 +08:00
Stream<Duration> get positionStreamInBook {
// return the positionInBook stream
2024-05-19 08:53:21 -04:00
return super.positionStream.map((position) {
2025-11-13 17:53:23 +08:00
if (_book == null || currentIndex == null) {
2024-05-19 08:53:21 -04:00
return Duration.zero;
}
2025-11-13 17:53:23 +08:00
return position + _book!.tracks[currentIndex!].startOffset;
2025-10-24 16:54:58 +08:00
// return position + _book!.tracks[sequenceState!.currentIndex].startOffset;
2024-05-19 08:53:21 -04:00
});
}
2025-10-24 15:42:33 +08:00
Stream<Duration> get bufferedPositionStreamInBook {
2024-05-19 08:53:21 -04:00
return super.bufferedPositionStream.map((position) {
2025-11-13 17:53:23 +08:00
if (_book == null || currentIndex == null) {
2024-05-19 08:53:21 -04:00
return Duration.zero;
}
2025-11-13 17:53:23 +08:00
return position + _book!.tracks[currentIndex!].startOffset;
2025-10-24 16:54:58 +08:00
// return position + _book!.tracks[sequenceState!.currentIndex].startOffset;
2024-05-19 08:53:21 -04:00
});
}
/// a convenience getter for slow position stream
2025-10-24 15:42:33 +08:00
Stream<Duration> get slowPositionStreamInBook {
final superPositionStream = createPositionStream(
steps: 100,
minPeriod: const Duration(milliseconds: 500),
maxPeriod: const Duration(seconds: 1),
);
// now we need to map the position to the book instead of the current track
return superPositionStream.map((position) {
2025-11-13 17:53:23 +08:00
if (_book == null || currentIndex == null) {
return Duration.zero;
}
2025-11-13 17:53:23 +08:00
return position + _book!.tracks[currentIndex!].startOffset;
2025-10-24 16:54:58 +08:00
// return position + _book!.tracks[sequenceState!.currentIndex].startOffset;
});
}
2024-05-19 08:53:21 -04:00
/// get current chapter
BookChapter? get currentChapter {
if (_book == null) {
return null;
}
2025-11-13 17:53:23 +08:00
return _book!.findChapterAtTime(positionInBook);
2024-05-19 08:53:21 -04:00
}
}
2024-08-20 08:36:39 -04:00
Uri _getUri(
AudioTrack track,
List<Uri>? downloadedUris, {
required Uri baseUrl,
required String token,
}) {
// check if the track is in the downloadedUris
final uri = downloadedUris?.firstWhereOrNull(
(element) {
return element.pathSegments.last == track.metadata?.filename;
},
);
2025-10-25 10:38:56 +08:00
return uri ??
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
2024-08-20 08:36:39 -04:00
}
extension FormatNotificationTitle on String {
String formatNotificationTitle(BookExpanded book) {
return replaceAllMapped(
RegExp(r'\$(\w+)'),
(match) {
final type = match.group(1);
return NotificationTitleType.values
.firstWhere((element) => element.name == type)
.extractFrom(book) ??
match.group(0) ??
'';
},
);
}
}
extension NotificationTitleUtils on NotificationTitleType {
String? extractFrom(BookExpanded book) {
var bookMetadataExpanded = book.metadata.asBookMetadataExpanded;
switch (this) {
case NotificationTitleType.bookTitle:
return bookMetadataExpanded.title;
case NotificationTitleType.chapterTitle:
// TODO: implement chapter title; depends on https://github.com/Dr-Blank/Vaani/issues/2
return bookMetadataExpanded.title;
case NotificationTitleType.author:
return bookMetadataExpanded.authorName;
case NotificationTitleType.narrator:
return bookMetadataExpanded.narratorName;
case NotificationTitleType.series:
return bookMetadataExpanded.seriesName;
case NotificationTitleType.subtitle:
return bookMetadataExpanded.subtitle;
case NotificationTitleType.year:
return bookMetadataExpanded.publishedYear;
}
}
}
2025-11-13 17:53:23 +08:00
extension BookExpandedExtension on BookExpanded {
BookChapter findChapterAtTime(Duration position) {
return chapters.firstWhere(
(element) {
return element.start <= position && element.end >= position + offset;
},
orElse: () => chapters.first,
);
}
AudioTrack findTrackAtTime(Duration position) {
return tracks.firstWhere(
(element) {
return element.startOffset <= position &&
element.startOffset + element.duration >= position + offset;
},
orElse: () => tracks.first,
);
}
Duration getTrackStartOffset(int index) {
return tracks[index].startOffset;
}
}