mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-17 14:59:35 +00:00
完善新播放逻辑
This commit is contained in:
parent
eb1955e5e6
commit
114c9761fd
30 changed files with 658 additions and 683 deletions
|
|
@ -1,112 +1,48 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:shelfsdk/audiobookshelf_api.dart';
|
||||
import 'package:vaani/features/player/core/audiobook_player.dart';
|
||||
import 'package:vaani/features/player/core/audiobook_player_session.dart';
|
||||
import 'package:vaani/shared/extensions/chapter.dart';
|
||||
import 'package:vaani/shared/utils/throttler.dart';
|
||||
|
||||
class SkipStartEnd {
|
||||
final Duration start;
|
||||
final Duration end;
|
||||
final AudiobookPlayer player;
|
||||
// 当前章节的id
|
||||
int? chapterId;
|
||||
// int _index;
|
||||
final AbsAudioHandler player;
|
||||
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
final throttler = Throttler(delay: Duration(seconds: 3));
|
||||
// final StreamController<PlaybackEvent> _playbackController =
|
||||
// StreamController<PlaybackEvent>.broadcast();
|
||||
final throttlerStart = Throttler(delay: Duration(seconds: 3));
|
||||
final throttlerEnd = Throttler(delay: Duration(seconds: 3));
|
||||
|
||||
SkipStartEnd({
|
||||
required this.start,
|
||||
required this.end,
|
||||
required this.player,
|
||||
this.chapterId,
|
||||
}) {
|
||||
// if (start > Duration()) {
|
||||
// _subscriptions.add(
|
||||
// player.currentIndexStream.listen((index) {
|
||||
// if (_index != index && player.position.inMilliseconds < 500) {
|
||||
// Future.microtask(() {
|
||||
// player.seek(start);
|
||||
// });
|
||||
// _index = index!;
|
||||
// }
|
||||
// }),
|
||||
// );
|
||||
// }
|
||||
// if (end > Duration()) {
|
||||
// _subscriptions.add(
|
||||
// player.positionStream.distinct().listen((position) {
|
||||
// if (player.duration != null &&
|
||||
// player.duration!.inMilliseconds - player.position.inMilliseconds <
|
||||
// end.inMilliseconds) {
|
||||
// throttler.call(() {
|
||||
// print('跳过片尾');
|
||||
// Future.microtask(() async {
|
||||
// await player.stop();
|
||||
// player.seekToNext();
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// }),
|
||||
// );
|
||||
// }
|
||||
if (start > Duration.zero || end > Duration.zero) {
|
||||
if (start > Duration.zero) {
|
||||
_subscriptions.add(
|
||||
player.positionStream.listen((position) {
|
||||
final chapter = player.currentChapter;
|
||||
if (chapter == null) {
|
||||
return;
|
||||
}
|
||||
if (chapter.id == chapterId) {
|
||||
if (end > Duration.zero &&
|
||||
chapter.duration - (player.positionInBook - chapter.start) <
|
||||
end) {
|
||||
throttler.call(() {
|
||||
Future.microtask(() => skipEnd(chapter));
|
||||
});
|
||||
}
|
||||
}
|
||||
if (chapter.id != chapterId) {
|
||||
if (start > Duration.zero &&
|
||||
player.positionInBook - chapter.start < Duration(seconds: 1)) {
|
||||
throttler.call(() {
|
||||
Future.microtask(() => skipStart(chapter));
|
||||
});
|
||||
}
|
||||
|
||||
chapterId = chapter.id;
|
||||
player.chapterStream.listen((chapter) {
|
||||
if (chapter != null &&
|
||||
player.positionInChapter < Duration(seconds: 1)) {
|
||||
Future.microtask(
|
||||
() => throttlerStart
|
||||
.call(() => player.seekInBook(chapter.start + start)),
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void skipStart(BookChapter chapter) {
|
||||
print('跳过片头');
|
||||
final globalPosition = player.positionInBook;
|
||||
if (globalPosition - chapter.start < Duration(seconds: 1)) {
|
||||
player.seekInBook(chapter.start + start);
|
||||
}
|
||||
}
|
||||
|
||||
void skipEnd(chapter) {
|
||||
print('跳过片尾');
|
||||
final book = player.book;
|
||||
if (book == null) {
|
||||
return;
|
||||
}
|
||||
if (start > Duration.zero) {
|
||||
final currentIndex = book.chapters.indexOf(chapter);
|
||||
if (currentIndex < book.chapters.length - 1) {
|
||||
final nextChapter = book.chapters[currentIndex + 1];
|
||||
// 跳过片头+片尾
|
||||
print('跳过片头+片尾');
|
||||
player.skipToChapter(nextChapter.id, position: start);
|
||||
}
|
||||
} else {
|
||||
player.seekToPrevious();
|
||||
if (end > Duration.zero) {
|
||||
_subscriptions.add(
|
||||
player.positionStreamInChapter.listen((positionChapter) {
|
||||
if (end >
|
||||
(player.currentChapter?.duration ?? Duration.zero) -
|
||||
positionChapter) {
|
||||
Future.microtask(
|
||||
() => throttlerEnd.call(() => player.skipToNext()),
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -115,7 +51,8 @@ class SkipStartEnd {
|
|||
for (var sub in _subscriptions) {
|
||||
sub.cancel();
|
||||
}
|
||||
throttler.dispose();
|
||||
throttlerStart.dispose();
|
||||
throttlerEnd.dispose();
|
||||
// _playbackController.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue