更改播放逻辑

This commit is contained in:
rang 2025-12-08 17:54:08 +08:00
parent 290b68336f
commit 420438c0df
29 changed files with 810 additions and 514 deletions

View file

@ -9,89 +9,72 @@ import 'package:vaani/features/player/providers/audiobook_player.dart';
import 'package:vaani/features/settings/app_settings_provider.dart';
import 'package:vaani/globals.dart';
part 'currently_playing_provider.g.dart';
// part 'currently_playing_provider.g.dart';
@riverpod
class CurrentBook extends _$CurrentBook {
@override
core.BookExpanded? build() {
return null;
}
// @riverpod
// class CurrentBook extends _$CurrentBook {
// @override
// core.BookExpanded? build() {
// return null;
// }
Future<void> update(core.BookExpanded book, Duration? currentTime) async {
final audioService = ref.read(playerProvider);
if (state == book) {
appLogger.info('Book was already set');
if (audioService.player.playing) {
appLogger.info('Pausing the book');
await audioService.pause();
return;
} else {
await audioService.play();
}
}
state = book;
final api = ref.read(authenticatedApiProvider);
final downloadManager = ref.read(simpleDownloadManagerProvider);
final libItem =
await ref.read(libraryItemProvider(state!.libraryItemId).future);
final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
// Future<void> update(core.BookExpanded book, Duration? currentTime) async {
// final audioService = ref.read(playerProvider);
// if (state == book) {
// appLogger.info('Book was already set');
// if (audioService.player.playing) {
// appLogger.info('Pausing the book');
// await audioService.pause();
// return;
// } else {
// await audioService.play();
// }
// }
// state = book;
// final api = ref.read(authenticatedApiProvider);
// final downloadManager = ref.read(simpleDownloadManagerProvider);
// final libItem =
// await ref.read(libraryItemProvider(state!.libraryItemId).future);
// final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
var bookPlayerSettings =
ref.read(bookSettingsProvider(state!.libraryItemId)).playerSettings;
var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
// var bookPlayerSettings =
// ref.read(bookSettingsProvider(state!.libraryItemId)).playerSettings;
// var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
var configurePlayerForEveryBook =
appPlayerSettings.configurePlayerForEveryBook;
audioService.setSourceAudiobook(
state!,
baseUrl: api.baseUrl,
token: api.token!,
initialPosition: currentTime,
downloadedUris: downloadedUris,
volume: configurePlayerForEveryBook
? bookPlayerSettings.preferredDefaultVolume ??
appPlayerSettings.preferredDefaultVolume
: appPlayerSettings.preferredDefaultVolume,
speed: configurePlayerForEveryBook
? bookPlayerSettings.preferredDefaultSpeed ??
appPlayerSettings.preferredDefaultSpeed
: appPlayerSettings.preferredDefaultSpeed,
);
}
}
// var configurePlayerForEveryBook =
// appPlayerSettings.configurePlayerForEveryBook;
// audioService.setSourceAudiobook(
// state!,
// baseUrl: api.baseUrl,
// token: api.token!,
// initialPosition: currentTime,
// downloadedUris: downloadedUris,
// volume: configurePlayerForEveryBook
// ? bookPlayerSettings.preferredDefaultVolume ??
// appPlayerSettings.preferredDefaultVolume
// : appPlayerSettings.preferredDefaultVolume,
// speed: configurePlayerForEveryBook
// ? bookPlayerSettings.preferredDefaultSpeed ??
// appPlayerSettings.preferredDefaultSpeed
// : appPlayerSettings.preferredDefaultSpeed,
// );
// }
// }
@riverpod
class CurrentChapter extends _$CurrentChapter {
@override
core.BookChapter? build() {
final player = ref.watch(playerProvider);
player.chapterStream.distinct().listen((chapter) {
update(chapter);
});
return player.currentChapter;
}
// @riverpod
// class CurrentChapter extends _$CurrentChapter {
// @override
// core.BookChapter? build() {
// final player = ref.watch(playerProvider);
// player.chapterStream.distinct().listen((chapter) {
// update(chapter);
// });
// return player.currentChapter;
// }
void update(core.BookChapter? chapter) {
if (state != chapter) {
state = chapter;
}
}
}
@riverpod
List<core.BookChapter> currentChapters(Ref ref) {
final book = ref.watch(currentBookProvider);
if (book == null) {
return [];
}
final currentChapter = ref.watch(currentChapterProvider);
if (currentChapter == null) {
return [];
}
final index = book.chapters.indexOf(currentChapter);
final total = book.chapters.length;
final start = index - 3 >= 0 ? index - 3 : 0;
final end = start + 20 <= total ? start + 20 : total;
return book.chapters.sublist(start, end);
}
// void update(core.BookChapter? chapter) {
// if (state != chapter) {
// state = chapter;
// }
// }
// }