mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 22:39:34 +00:00
恢复
This commit is contained in:
parent
1ca8e4889a
commit
aad510ea45
31 changed files with 777 additions and 239 deletions
|
|
@ -6,21 +6,28 @@ import 'package:collection/collection.dart';
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart';
|
||||
import 'package:vaani/features/player/core/player_status.dart' as core;
|
||||
import 'package:vaani/features/player/providers/player_status_provider.dart';
|
||||
import 'package:vaani/features/settings/app_settings_provider.dart';
|
||||
import 'package:vaani/features/settings/models/app_settings.dart';
|
||||
import 'package:vaani/shared/extensions/chapter.dart';
|
||||
import 'package:vaani/shared/extensions/model_conversions.dart';
|
||||
|
||||
// add a small offset so the display does not show the previous chapter for a split second
|
||||
final offset = Duration(milliseconds: 10);
|
||||
|
||||
final _logger = Logger('AudiobookPlayer');
|
||||
|
||||
class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
||||
final AudioPlayer _player = AudioPlayer();
|
||||
// final List<AudioSource> _playlist = [];
|
||||
final Ref ref;
|
||||
|
||||
PlaybackSessionExpanded? _session;
|
||||
BookExpanded? _book;
|
||||
BookExpanded? get book => _book;
|
||||
|
||||
final _currentChapterObject = BehaviorSubject<BookChapter?>.seeded(null);
|
||||
AbsAudioHandler(this.ref) {
|
||||
|
|
@ -40,7 +47,7 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
}
|
||||
});
|
||||
_player.positionStream.distinct().listen((position) {
|
||||
final chapter = _session?.findChapterAtTime(positionInBook);
|
||||
final chapter = _book?.findChapterAtTime(positionInBook);
|
||||
if (chapter != currentChapter) {
|
||||
_currentChapterObject.sink.add(chapter);
|
||||
}
|
||||
|
|
@ -49,46 +56,68 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
|
||||
// 加载有声书
|
||||
Future<void> setSourceAudiobook(
|
||||
PlaybackSessionExpanded playbackSession, {
|
||||
BookExpanded book, {
|
||||
bool preload = true,
|
||||
required Uri baseUrl,
|
||||
required String token,
|
||||
Duration? initialPosition,
|
||||
List<Uri>? downloadedUris,
|
||||
}) async {
|
||||
_session = playbackSession;
|
||||
final appSettings = loadOrCreateAppSettings();
|
||||
// if (book == null) {
|
||||
// _book = null;
|
||||
// _logger.info('Book is null, stopping player');
|
||||
// return stop();
|
||||
// }
|
||||
if (_book == book) {
|
||||
_logger.info('Book is the same, doing nothing');
|
||||
return;
|
||||
}
|
||||
_book = book;
|
||||
|
||||
// 添加所有音轨
|
||||
List<AudioSource> audioSources = [];
|
||||
for (final track in playbackSession.audioTracks) {
|
||||
audioSources.add(
|
||||
AudioSource.uri(
|
||||
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
|
||||
),
|
||||
);
|
||||
}
|
||||
List<AudioSource> audioSources = book.tracks
|
||||
.map(
|
||||
(track) => AudioSource.uri(
|
||||
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
final title = appSettings.notificationSettings.primaryTitle
|
||||
.formatNotificationTitle(book);
|
||||
final album = appSettings.notificationSettings.secondaryTitle
|
||||
.formatNotificationTitle(book);
|
||||
playMediaItem(
|
||||
MediaItem(
|
||||
id: playbackSession.libraryItemId,
|
||||
album: playbackSession.mediaMetadata.title,
|
||||
title: playbackSession.displayTitle,
|
||||
displaySubtitle: playbackSession.mediaType == MediaType.book
|
||||
? (playbackSession.mediaMetadata as BookMetadata).subtitle
|
||||
: null,
|
||||
duration: playbackSession.duration,
|
||||
id: book.libraryItemId,
|
||||
title: title,
|
||||
album: album,
|
||||
displayTitle: title,
|
||||
displaySubtitle: album,
|
||||
duration: book.duration,
|
||||
artUri: Uri.parse(
|
||||
'$baseUrl/api/items/${playbackSession.libraryItemId}/cover?token=$token',
|
||||
'$baseUrl/api/items/${book.libraryItemId}/cover?token=$token',
|
||||
),
|
||||
),
|
||||
);
|
||||
final track = playbackSession.findTrackAtTime(playbackSession.currentTime);
|
||||
final index = playbackSession.audioTracks.indexOf(track);
|
||||
|
||||
await _player.setAudioSources(
|
||||
final trackToPlay = book.findTrackAtTime(initialPosition ?? Duration.zero);
|
||||
final initialIndex = book.tracks.indexOf(trackToPlay);
|
||||
final initialPositionInTrack = initialPosition != null
|
||||
? initialPosition - trackToPlay.startOffset
|
||||
: null;
|
||||
await _player
|
||||
.setAudioSources(
|
||||
audioSources,
|
||||
initialIndex: index,
|
||||
initialPosition: playbackSession.currentTime - track.startOffset,
|
||||
);
|
||||
_player.seek(playbackSession.currentTime - track.startOffset, index: index);
|
||||
preload: preload,
|
||||
initialIndex: initialIndex,
|
||||
initialPosition: initialPositionInTrack,
|
||||
)
|
||||
.catchError((error) {
|
||||
_logger.shout('Error in setting audio source: $error');
|
||||
return null;
|
||||
});
|
||||
// _player.seek(initialPositionInTrack, index: initialIndex);
|
||||
await play();
|
||||
// 恢复上次播放位置(如果有)
|
||||
// if (initialPosition != null) {
|
||||
|
|
@ -106,23 +135,23 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
|
||||
// 核心功能:跳转到指定章节
|
||||
Future<void> skipToChapter(int chapterId) async {
|
||||
if (_session == null) return;
|
||||
if (_book == null) return;
|
||||
|
||||
final chapter = _session!.chapters.firstWhere(
|
||||
final chapter = _book!.chapters.firstWhere(
|
||||
(ch) => ch.id == chapterId,
|
||||
orElse: () => throw Exception('Chapter not found'),
|
||||
);
|
||||
await seekInBook(chapter.start + offset);
|
||||
}
|
||||
|
||||
PlaybackSessionExpanded? get session => _session;
|
||||
BookExpanded? get Book => _book;
|
||||
|
||||
// 当前音轨
|
||||
AudioTrack? get currentTrack {
|
||||
if (_session == null || _player.currentIndex == null) {
|
||||
if (_book == null || _player.currentIndex == null) {
|
||||
return null;
|
||||
}
|
||||
return _session!.audioTracks[_player.currentIndex!];
|
||||
return _book!.tracks[_player.currentIndex!];
|
||||
}
|
||||
|
||||
// 当前章节
|
||||
|
|
@ -187,10 +216,12 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
|
||||
Future<void> togglePlayPause() async {
|
||||
// check if book is set
|
||||
if (_session == null) {
|
||||
return Future.value();
|
||||
if (_book == null) {
|
||||
_logger.warning('No book is set, not toggling play/pause');
|
||||
}
|
||||
_player.playerState.playing ? await pause() : await play();
|
||||
return switch (_player.playerState) {
|
||||
PlayerState(playing: var isPlaying) => isPlaying ? pause() : play(),
|
||||
};
|
||||
}
|
||||
|
||||
// 播放控制方法
|
||||
|
|
@ -207,7 +238,7 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
// 重写上一曲/下一曲为章节导航
|
||||
@override
|
||||
Future<void> skipToNext() async {
|
||||
if (_session == null) {
|
||||
if (_book == null) {
|
||||
// 回退到默认行为
|
||||
return _player.seekToNext();
|
||||
}
|
||||
|
|
@ -216,10 +247,10 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
// 回退到默认行为
|
||||
return _player.seekToNext();
|
||||
}
|
||||
final chapterIndex = _session!.chapters.indexOf(chapter);
|
||||
if (chapterIndex < _session!.chapters.length - 1) {
|
||||
final chapterIndex = _book!.chapters.indexOf(chapter);
|
||||
if (chapterIndex < _book!.chapters.length - 1) {
|
||||
// 跳到下一章
|
||||
final nextChapter = _session!.chapters[chapterIndex + 1];
|
||||
final nextChapter = _book!.chapters[chapterIndex + 1];
|
||||
await skipToChapter(nextChapter.id);
|
||||
}
|
||||
}
|
||||
|
|
@ -227,13 +258,13 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
@override
|
||||
Future<void> skipToPrevious() async {
|
||||
final chapter = currentChapter;
|
||||
if (_session == null || chapter == null) {
|
||||
if (_book == null || chapter == null) {
|
||||
return _player.seekToPrevious();
|
||||
}
|
||||
final currentIndex = _session!.chapters.indexOf(chapter);
|
||||
final currentIndex = _book!.chapters.indexOf(chapter);
|
||||
if (currentIndex > 0) {
|
||||
// 跳到上一章
|
||||
final prevChapter = _session!.chapters[currentIndex - 1];
|
||||
final prevChapter = _book!.chapters[currentIndex - 1];
|
||||
await skipToChapter(prevChapter.id);
|
||||
} else {
|
||||
// 已经是第一章,回到开头
|
||||
|
|
@ -264,10 +295,10 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
|
||||
// 核心功能:跳转到全局时间位置
|
||||
Future<void> seekInBook(Duration globalPosition) async {
|
||||
if (_session == null) return;
|
||||
if (_book == null) return;
|
||||
// 找到目标音轨和在音轨内的位置
|
||||
final track = _session!.findTrackAtTime(globalPosition);
|
||||
final index = _session!.audioTracks.indexOf(track);
|
||||
final track = _book!.findTrackAtTime(globalPosition);
|
||||
final index = _book!.tracks.indexOf(track);
|
||||
Duration positionInTrack = globalPosition - track.startOffset;
|
||||
if (positionInTrack < Duration.zero) {
|
||||
positionInTrack = Duration.zero;
|
||||
|
|
@ -332,7 +363,46 @@ Uri _getUri(
|
|||
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
|
||||
}
|
||||
|
||||
extension PlaybackSessionExpandedExtension on PlaybackSessionExpanded {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BookExpandedExtension on BookExpanded {
|
||||
BookChapter findChapterAtTime(Duration position) {
|
||||
return chapters.firstWhere(
|
||||
(element) {
|
||||
|
|
@ -343,23 +413,23 @@ extension PlaybackSessionExpandedExtension on PlaybackSessionExpanded {
|
|||
}
|
||||
|
||||
AudioTrack findTrackAtTime(Duration position) {
|
||||
return audioTracks.firstWhere(
|
||||
return tracks.firstWhere(
|
||||
(element) {
|
||||
return element.startOffset <= position &&
|
||||
element.startOffset + element.duration >= position + offset;
|
||||
},
|
||||
orElse: () => audioTracks.first,
|
||||
orElse: () => tracks.first,
|
||||
);
|
||||
}
|
||||
|
||||
int findTrackIndexAtTime(Duration position) {
|
||||
return audioTracks.indexWhere((element) {
|
||||
return tracks.indexWhere((element) {
|
||||
return element.startOffset <= position &&
|
||||
element.startOffset + element.duration >= position + offset;
|
||||
});
|
||||
}
|
||||
|
||||
Duration getTrackStartOffset(int index) {
|
||||
return audioTracks[index].startOffset;
|
||||
return tracks[index].startOffset;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,15 +2,7 @@ import 'package:audio_service/audio_service.dart';
|
|||
import 'package:just_audio_media_kit/just_audio_media_kit.dart';
|
||||
import 'package:riverpod/riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart' as core;
|
||||
import 'package:vaani/api/api_provider.dart';
|
||||
import 'package:vaani/api/library_item_provider.dart';
|
||||
import 'package:vaani/features/downloads/providers/download_manager.dart';
|
||||
import 'package:vaani/features/per_book_settings/providers/book_settings_provider.dart';
|
||||
import 'package:vaani/features/player/core/audiobook_player.dart';
|
||||
import 'package:vaani/features/player/providers/player_status_provider.dart';
|
||||
import 'package:vaani/features/settings/app_settings_provider.dart';
|
||||
import 'package:vaani/shared/extensions/model_conversions.dart';
|
||||
import 'package:vaani/shared/utils/helper.dart';
|
||||
|
||||
part 'audiobook_player.g.dart';
|
||||
|
|
@ -46,59 +38,59 @@ class Player extends _$Player {
|
|||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class Session extends _$Session {
|
||||
@override
|
||||
core.PlaybackSessionExpanded? build() {
|
||||
return null;
|
||||
}
|
||||
// @Riverpod(keepAlive: true)
|
||||
// class Session extends _$Session {
|
||||
// @override
|
||||
// core.PlaybackSessionExpanded? build() {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
Future<void> load(String id, String? episodeId) async {
|
||||
final audioService = ref.read(playerProvider);
|
||||
await audioService.pause();
|
||||
ref.read(playerStatusProvider.notifier).setLoading(id);
|
||||
final api = ref.read(authenticatedApiProvider);
|
||||
final playBack = await ref.watch(playBackSessionProvider(id).future);
|
||||
if (playBack == null) {
|
||||
return;
|
||||
}
|
||||
state = playBack.asExpanded;
|
||||
final downloadManager = ref.read(simpleDownloadManagerProvider);
|
||||
final libItem =
|
||||
await ref.read(libraryItemProvider(state!.libraryItemId).future);
|
||||
final downloadedUris = await downloadManager.getDownloadedFilesUri(libItem);
|
||||
// Future<void> load(String id, String? episodeId) async {
|
||||
// final audioService = ref.read(playerProvider);
|
||||
// await audioService.pause();
|
||||
// ref.read(playerStatusProvider.notifier).setLoading(id);
|
||||
// final api = ref.read(authenticatedApiProvider);
|
||||
// final playBack = await ref.watch(playBackSessionProvider(id).future);
|
||||
// if (playBack == null) {
|
||||
// return;
|
||||
// }
|
||||
// state = playBack.asExpanded;
|
||||
// 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;
|
||||
// var configurePlayerForEveryBook =
|
||||
// appPlayerSettings.configurePlayerForEveryBook;
|
||||
|
||||
await Future.wait([
|
||||
audioService.setSourceAudiobook(
|
||||
state!.asExpanded,
|
||||
baseUrl: api.baseUrl,
|
||||
token: api.token!,
|
||||
downloadedUris: downloadedUris,
|
||||
),
|
||||
// set the volume
|
||||
audioService.setVolume(
|
||||
configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultVolume ??
|
||||
appPlayerSettings.preferredDefaultVolume
|
||||
: appPlayerSettings.preferredDefaultVolume,
|
||||
),
|
||||
// set the speed
|
||||
audioService.setSpeed(
|
||||
configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
appPlayerSettings.preferredDefaultSpeed
|
||||
: appPlayerSettings.preferredDefaultSpeed,
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
// await Future.wait([
|
||||
// audioService.setSourceAudiobook(
|
||||
// state!.asExpanded,
|
||||
// baseUrl: api.baseUrl,
|
||||
// token: api.token!,
|
||||
// downloadedUris: downloadedUris,
|
||||
// ),
|
||||
// // set the volume
|
||||
// audioService.setVolume(
|
||||
// configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultVolume ??
|
||||
// appPlayerSettings.preferredDefaultVolume
|
||||
// : appPlayerSettings.preferredDefaultVolume,
|
||||
// ),
|
||||
// // set the speed
|
||||
// audioService.setSpeed(
|
||||
// configurePlayerForEveryBook
|
||||
// ? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
// appPlayerSettings.preferredDefaultSpeed
|
||||
// : appPlayerSettings.preferredDefaultSpeed,
|
||||
// ),
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
|
||||
class PlaybackSyncError implements Exception {
|
||||
String message;
|
||||
|
|
|
|||
|
|
@ -37,20 +37,5 @@ final playerProvider = NotifierProvider<Player, AbsAudioHandler>.internal(
|
|||
);
|
||||
|
||||
typedef _$Player = Notifier<AbsAudioHandler>;
|
||||
String _$sessionHash() => r'c171809249c3021dc445dc1ba90fe8626a3d3b54';
|
||||
|
||||
/// See also [Session].
|
||||
@ProviderFor(Session)
|
||||
final sessionProvider =
|
||||
NotifierProvider<Session, core.PlaybackSessionExpanded?>.internal(
|
||||
Session.new,
|
||||
name: r'sessionProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$sessionHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$Session = Notifier<core.PlaybackSessionExpanded?>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
|
|
|
|||
|
|
@ -1,10 +1,74 @@
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart' as core;
|
||||
import 'package:vaani/api/api_provider.dart';
|
||||
import 'package:vaani/api/library_item_provider.dart';
|
||||
import 'package:vaani/features/downloads/providers/download_manager.dart';
|
||||
import 'package:vaani/features/per_book_settings/providers/book_settings_provider.dart';
|
||||
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';
|
||||
|
||||
@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);
|
||||
|
||||
var bookPlayerSettings =
|
||||
ref.read(bookSettingsProvider(state!.libraryItemId)).playerSettings;
|
||||
var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
|
||||
|
||||
var configurePlayerForEveryBook =
|
||||
appPlayerSettings.configurePlayerForEveryBook;
|
||||
await Future.wait([
|
||||
audioService.setSourceAudiobook(
|
||||
state!,
|
||||
baseUrl: api.baseUrl,
|
||||
token: api.token!,
|
||||
initialPosition: currentTime,
|
||||
downloadedUris: downloadedUris,
|
||||
),
|
||||
// set the volume
|
||||
audioService.setVolume(
|
||||
configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultVolume ??
|
||||
appPlayerSettings.preferredDefaultVolume
|
||||
: appPlayerSettings.preferredDefaultVolume,
|
||||
),
|
||||
// set the speed
|
||||
audioService.setSpeed(
|
||||
configurePlayerForEveryBook
|
||||
? bookPlayerSettings.preferredDefaultSpeed ??
|
||||
appPlayerSettings.preferredDefaultSpeed
|
||||
: appPlayerSettings.preferredDefaultSpeed,
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class CurrentChapter extends _$CurrentChapter {
|
||||
@override
|
||||
|
|
@ -25,16 +89,16 @@ class CurrentChapter extends _$CurrentChapter {
|
|||
|
||||
@riverpod
|
||||
List<core.BookChapter> currentChapters(Ref ref) {
|
||||
final session = ref.watch(sessionProvider);
|
||||
if (session == null) {
|
||||
final book = ref.watch(currentBookProvider);
|
||||
if (book == null) {
|
||||
return [];
|
||||
}
|
||||
final currentChapter = ref.watch(currentChapterProvider);
|
||||
if (currentChapter == null) {
|
||||
return [];
|
||||
}
|
||||
final index = session.chapters.indexOf(currentChapter);
|
||||
final total = session.chapters.length;
|
||||
return session.chapters
|
||||
final index = book.chapters.indexOf(currentChapter);
|
||||
final total = book.chapters.length;
|
||||
return book.chapters
|
||||
.sublist(index - 3, (total - 3) <= (index + 17) ? total : index + 17);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ part of 'currently_playing_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$currentChaptersHash() => r'f2cc6ec31b5a3a9471775b1c96b2bfc3a91f1c90';
|
||||
String _$currentChaptersHash() => r'a25733d8085a2ce7dbc16fa2bf14f00ab8e2a623';
|
||||
|
||||
/// See also [currentChapters].
|
||||
@ProviderFor(currentChapters)
|
||||
|
|
@ -24,6 +24,21 @@ final currentChaptersProvider =
|
|||
@Deprecated('Will be removed in 3.0. Use Ref instead')
|
||||
// ignore: unused_element
|
||||
typedef CurrentChaptersRef = AutoDisposeProviderRef<List<core.BookChapter>>;
|
||||
String _$currentBookHash() => r'8dd534821b2b02a0259c6e6bde58012b880225c5';
|
||||
|
||||
/// See also [CurrentBook].
|
||||
@ProviderFor(CurrentBook)
|
||||
final currentBookProvider =
|
||||
AutoDisposeNotifierProvider<CurrentBook, core.BookExpanded?>.internal(
|
||||
CurrentBook.new,
|
||||
name: r'currentBookProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$currentBookHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CurrentBook = AutoDisposeNotifier<core.BookExpanded?>;
|
||||
String _$currentChapterHash() => r'f5f6d9e49cb7e455d032f7370f364d9ce30b8eb1';
|
||||
|
||||
/// See also [CurrentChapter].
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ import 'dart:math';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:vaani/constants/sizes.dart';
|
||||
import 'package:vaani/features/player/providers/audiobook_player.dart';
|
||||
import 'package:vaani/features/player/providers/currently_playing_provider.dart';
|
||||
import 'package:vaani/features/player/view/widgets/player_player_pause_button.dart';
|
||||
import 'package:vaani/features/player/view/widgets/player_progress_bar.dart';
|
||||
import 'package:vaani/features/skip_start_end/view/skip_start_end_button.dart';
|
||||
import 'package:vaani/features/sleep_timer/view/sleep_timer_button.dart';
|
||||
import 'package:vaani/shared/extensions/model_conversions.dart';
|
||||
import 'package:vaani/shared/widgets/shelves/book_shelf.dart';
|
||||
|
||||
import 'widgets/audiobook_player_seek_button.dart';
|
||||
|
|
@ -25,8 +25,8 @@ class PlayerExpanded extends HookConsumerWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final session = ref.watch(sessionProvider);
|
||||
if (session == null) {
|
||||
final currentBook = ref.watch(currentBookProvider);
|
||||
if (currentBook == null) {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
|
||||
|
|
@ -77,8 +77,8 @@ class PlayerExpanded extends HookConsumerWidget {
|
|||
padding: EdgeInsets.only(bottom: AppElementSizes.paddingRegular),
|
||||
child: Text(
|
||||
[
|
||||
session.displayTitle,
|
||||
session.displayAuthor,
|
||||
currentBook.metadata.title ?? '',
|
||||
currentBook.metadata.asBookMetadataExpanded.authorName ?? '',
|
||||
].join(' - '),
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
color: Theme.of(context)
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ class PlayerExpandedDesktop extends HookConsumerWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final session = ref.watch(sessionProvider);
|
||||
if (session == null) {
|
||||
final book = ref.watch(currentBookProvider);
|
||||
if (book == null) {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
|
||||
|
|
@ -49,6 +49,7 @@ class PlayerExpandedDesktop extends HookConsumerWidget {
|
|||
body: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: AppElementSizes.paddingLarge,
|
||||
right: AppElementSizes.paddingRegular,
|
||||
bottom: playerMinHeight + 40,
|
||||
),
|
||||
child: Row(
|
||||
|
|
@ -108,7 +109,18 @@ class PlayerExpandedDesktop extends HookConsumerWidget {
|
|||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
Expanded(
|
||||
child: ChapterSelection(),
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
right: BorderSide(
|
||||
color: Theme.of(context).focusColor,
|
||||
width: 1.0,
|
||||
style: BorderStyle.solid, // 可以设置为 dashed 虚线
|
||||
),
|
||||
),
|
||||
),
|
||||
child: ChapterSelection(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import 'package:vaani/features/player/providers/audiobook_player.dart';
|
|||
import 'package:vaani/features/player/providers/currently_playing_provider.dart';
|
||||
import 'package:vaani/features/player/view/widgets/player_player_pause_button.dart';
|
||||
import 'package:vaani/router/router.dart';
|
||||
import 'package:vaani/shared/extensions/model_conversions.dart';
|
||||
import 'package:vaani/shared/widgets/shelves/book_shelf.dart';
|
||||
|
||||
/// The height of the player when it is minimized
|
||||
|
|
@ -18,8 +19,8 @@ class PlayerMinimized extends HookConsumerWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final session = ref.watch(sessionProvider);
|
||||
if (session == null) {
|
||||
final currentBook = ref.watch(currentBookProvider);
|
||||
if (currentBook == null) {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
final currentChapter = ref.watch(currentChapterProvider);
|
||||
|
|
@ -35,7 +36,7 @@ class PlayerMinimized extends HookConsumerWidget {
|
|||
context.pushNamed(
|
||||
Routes.libraryItem.name,
|
||||
pathParameters: {
|
||||
Routes.libraryItem.pathParamName!: session.libraryItemId,
|
||||
Routes.libraryItem.pathParamName!: currentBook.libraryItemId,
|
||||
},
|
||||
);
|
||||
},
|
||||
|
|
@ -60,14 +61,14 @@ class PlayerMinimized extends HookConsumerWidget {
|
|||
children: [
|
||||
// AutoScrollText(
|
||||
PlatformText(
|
||||
'${session.displayTitle} - ${currentChapter?.title ?? ''}',
|
||||
'${currentBook.metadata.title ?? ''} - ${currentChapter?.title ?? ''}',
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis,
|
||||
// velocity:
|
||||
// const Velocity(pixelsPerSecond: Offset(16, 0)),
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
PlatformText(
|
||||
session.displayAuthor,
|
||||
currentBook.metadata.asBookMetadataExpanded.authorName ?? '',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
||||
|
|
|
|||
|
|
@ -21,13 +21,12 @@ class AudiobookPlayerSeekChapterButton extends HookConsumerWidget {
|
|||
size: AppElementSizes.iconSizeSmall,
|
||||
),
|
||||
onPressed: () {
|
||||
if (player.session == null) {
|
||||
if (player.book == null) {
|
||||
return;
|
||||
}
|
||||
// if chapter does not exist, go to the start or end of the book
|
||||
if (player.currentChapter == null) {
|
||||
player
|
||||
.seekInBook(isForward ? player.session!.duration : Duration.zero);
|
||||
player.seekInBook(isForward ? player.book!.duration : Duration.zero);
|
||||
return;
|
||||
}
|
||||
if (isForward) {
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class ChapterSelectionModal extends HookConsumerWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final session = ref.watch(sessionProvider);
|
||||
final session = ref.watch(currentBookProvider);
|
||||
final currentChapter = ref.watch(currentChapterProvider);
|
||||
|
||||
final currentChapterIndex = currentChapter?.id;
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class AudiobookChapterProgressBar extends HookConsumerWidget {
|
|||
progress:
|
||||
currentChapterProgress ?? position.data ?? const Duration(seconds: 0),
|
||||
total: currentChapter == null
|
||||
? player.session?.duration ?? const Duration(seconds: 0)
|
||||
? player.book?.duration ?? const Duration(seconds: 0)
|
||||
: currentChapter.end - currentChapter.start,
|
||||
// ! TODO add onSeek
|
||||
onSeek: (duration) {
|
||||
|
|
@ -74,7 +74,7 @@ class AudiobookProgressBar extends HookConsumerWidget {
|
|||
height: AppElementSizes.barHeightLarge,
|
||||
child: LinearProgressIndicator(
|
||||
value: (position.data ?? const Duration(seconds: 0)).inSeconds /
|
||||
(player.session?.duration ?? const Duration(seconds: 0)).inSeconds,
|
||||
(player.book?.duration ?? const Duration(seconds: 0)).inSeconds,
|
||||
borderRadius: BorderRadiusGeometry.all(Radius.circular(10)),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
|
|||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final player = ref.watch(playerProvider);
|
||||
final bookId = player.session?.libraryItemId ?? '_';
|
||||
final bookId = player.book?.libraryItemId ?? '_';
|
||||
final bookSettings = ref.watch(bookSettingsProvider(bookId));
|
||||
final appSettings = ref.watch(appSettingsProvider);
|
||||
return TextButton(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue