修复播放问题

This commit is contained in:
rang 2025-10-25 10:11:50 +08:00
parent a9148f7a14
commit a28547685b
3 changed files with 41 additions and 35 deletions

View file

@ -555,7 +555,6 @@ Future<void> libraryItemPlayButtonOnPressed({
? bookPlayerSettings.preferredDefaultSpeed ?? appPlayerSettings.preferredDefaultSpeed ? bookPlayerSettings.preferredDefaultSpeed ?? appPlayerSettings.preferredDefaultSpeed
: appPlayerSettings.preferredDefaultSpeed, : appPlayerSettings.preferredDefaultSpeed,
), ),
// player.setClip(start: Duration(seconds: 10)),
]); ]);
// toggle play/pause // toggle play/pause

View file

@ -52,6 +52,11 @@ class AudiobookPlayer extends AudioPlayer {
AudiobookPlayer(this.token, this.baseUrl) : super() { AudiobookPlayer(this.token, this.baseUrl) : super() {
// set the source of the player to the first track in the book // set the source of the player to the first track in the book
_logger.config('Setting up audiobook player'); _logger.config('Setting up audiobook player');
playerStateStream.listen((playerState) {
if (playerState.processingState == ProcessingState.completed) {
Future.microtask(seekToNext);
}
});
} }
/// the [BookExpanded] being played /// the [BookExpanded] being played
@ -76,7 +81,7 @@ class AudiobookPlayer extends AudioPlayer {
// available audio tracks // available audio tracks
int? get availableTracks => _book?.tracks.length; int? get availableTracks => _book?.tracks.length;
List<Uri>? downloadedUris; List<Uri>? _downloadedUris;
/// sets the current [AudioTrack] as the source of the player /// sets the current [AudioTrack] as the source of the player
Future<void> setSourceAudiobook( Future<void> setSourceAudiobook(
@ -107,7 +112,7 @@ class AudiobookPlayer extends AudioPlayer {
await stop(); await stop();
_book = book; _book = book;
_downloadedUris = downloadedUris;
// some calculations to set the initial index and position // some calculations to set the initial index and position
// initialPosition is of the entire book not just the current track // 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 // hence first we need to calculate the current track which will be used to set the initial position
@ -157,14 +162,15 @@ class AudiobookPlayer extends AudioPlayer {
if (_book == null) { if (_book == null) {
return stop(); return stop();
} }
if (index == _currentIndex) { if (_currentIndex != 0 && index == _currentIndex) {
return; return;
} }
_currentIndex = index;
AudioTrack track = _book!.tracks[index]; AudioTrack track = _book!.tracks[index];
final appSettings = loadOrCreateAppSettings(); final appSettings = loadOrCreateAppSettings();
final playerSettings = readFromBoxOrCreate(_book!.libraryItemId).playerSettings; final playerSettings = readFromBoxOrCreate(_book!.libraryItemId).playerSettings;
final retrievedUri = _getUri(track, downloadedUris, baseUrl: baseUrl, token: token); final retrievedUri = _getUri(track, _downloadedUris, baseUrl: baseUrl, token: token);
await setAudioSource( await setAudioSource(
initialPosition: initialPosition == null || initialPosition <= Duration() initialPosition: initialPosition == null || initialPosition <= Duration()
@ -226,10 +232,7 @@ class AudiobookPlayer extends AudioPlayer {
/// this is because the book can be a list of audio files and the player is only aware 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 /// so we need to calculate the duration and current position based on the book
Future<void> seekInBook(Duration? positionInBook, {int? index, bool b = true}) async { Future<void> seekInBook(Duration? positionInBook, {int? index}) async {
if (!b) {
return super.seek(positionInBook, index: index);
}
if (_book == null) { if (_book == null) {
_logger.warning('No book is set, not seeking'); _logger.warning('No book is set, not seeking');
return; return;
@ -242,7 +245,8 @@ class AudiobookPlayer extends AudioPlayer {
final trackToPlay = getTrackToPlay(_book!, positionInBook); final trackToPlay = getTrackToPlay(_book!, positionInBook);
final i = tracks.indexOf(trackToPlay); final i = tracks.indexOf(trackToPlay);
final positionInTrack = positionInBook - trackToPlay.startOffset; final positionInTrack = positionInBook - trackToPlay.startOffset;
return super.seek(positionInTrack, index: i); return setAudioSourceTrack(i, initialPosition: positionInTrack);
// return super.seek(positionInTrack, index: i);
} }
// add a small offset so the display does not show the previous chapter for a split second // add a small offset so the display does not show the previous chapter for a split second
@ -253,14 +257,15 @@ class AudiobookPlayer extends AudioPlayer {
/// seek forward to the next chapter /// seek forward to the next chapter
void seekForward() { void seekForward() {
final index = _book!.chapters.indexOf(currentChapter!); seekToNext();
if (index < _book!.chapters.length - 1) { // final index = _book!.chapters.indexOf(currentChapter!);
super.seek( // if (index < _book!.chapters.length - 1) {
_book!.chapters[index + 1].start + offset, // super.seek(
); // _book!.chapters[index + 1].start + offset,
} else { // );
super.seek(currentChapter!.end); // } else {
} // super.seek(currentChapter!.end);
// }
} }
@override @override
@ -268,7 +273,7 @@ class AudiobookPlayer extends AudioPlayer {
if (_currentIndex >= availableTracks!) { if (_currentIndex >= availableTracks!) {
return super.seek(duration); return super.seek(duration);
} }
return setAudioSourceTrack(_currentIndex++); return setAudioSourceTrack(_currentIndex + 1);
} }
@override @override
@ -276,23 +281,24 @@ class AudiobookPlayer extends AudioPlayer {
if (_currentIndex == 0) { if (_currentIndex == 0) {
return super.seek(Duration()); return super.seek(Duration());
} }
return setAudioSourceTrack(_currentIndex--); return setAudioSourceTrack(_currentIndex - 1);
} }
/// seek backward to the previous chapter or the start of the current chapter /// seek backward to the previous chapter or the start of the current chapter
void seekBackward() { void seekBackward() {
final currentPlayingChapterIndex = _book!.chapters.indexOf(currentChapter!); seekToPrevious();
final chapterPosition = positionInBook - currentChapter!.start; // final currentPlayingChapterIndex = _book!.chapters.indexOf(currentChapter!);
BookChapter chapterToSeekTo; // final chapterPosition = positionInBook - currentChapter!.start;
// if player position is less than 5 seconds into the chapter, go to the previous chapter // BookChapter chapterToSeekTo;
if (chapterPosition < doNotSeekBackIfLessThan && currentPlayingChapterIndex > 0) { // // if player position is less than 5 seconds into the chapter, go to the previous chapter
chapterToSeekTo = _book!.chapters[currentPlayingChapterIndex - 1]; // if (chapterPosition < doNotSeekBackIfLessThan && currentPlayingChapterIndex > 0) {
} else { // chapterToSeekTo = _book!.chapters[currentPlayingChapterIndex - 1];
chapterToSeekTo = currentChapter!; // } else {
} // chapterToSeekTo = currentChapter!;
super.seek( // }
chapterToSeekTo.start + offset, // super.seek(
); // chapterToSeekTo.start + offset,
// );
} }
/// a convenience method to get position in the book instead of the current track position /// a convenience method to get position in the book instead of the current track position

View file

@ -220,9 +220,10 @@ class AudiobookChapterProgressBar extends HookConsumerWidget {
: currentChapter.end - currentChapter.start, : currentChapter.end - currentChapter.start,
// ! TODO add onSeek // ! TODO add onSeek
onSeek: (duration) { onSeek: (duration) {
player.seekInBook( // player.seekInBook(
duration + (currentChapter?.start ?? const Duration(seconds: 0)), // duration + (currentChapter?.start ?? const Duration(seconds: 0)),
); // );
player.seek(duration);
}, },
thumbRadius: 8, thumbRadius: 8,
buffered: currentChapterBuffered ?? buffered.data ?? const Duration(seconds: 0), buffered: currentChapterBuffered ?? buffered.data ?? const Duration(seconds: 0),