mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 14:29:35 +00:00
更改跳过首尾方式
This commit is contained in:
parent
5abf938dcf
commit
66439018fb
9 changed files with 150 additions and 67 deletions
|
|
@ -38,6 +38,8 @@ abstract class AbsAudioPlayer {
|
|||
required String token,
|
||||
Duration? initialPosition,
|
||||
List<Uri>? downloadedUris,
|
||||
Duration? start,
|
||||
Duration? end,
|
||||
}) async {
|
||||
if (_bookStreamController.nvalue == book) {
|
||||
_logger.info('Book is the same, doing nothing');
|
||||
|
|
@ -69,17 +71,27 @@ abstract class AbsAudioPlayer {
|
|||
_mediaItemController.sink.add(item);
|
||||
final playlist = book.tracks
|
||||
.map(
|
||||
(track) =>
|
||||
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
|
||||
(track) => (
|
||||
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
|
||||
track.duration
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
await setPlayList(playlist, index: indexTrack, position: positionInTrack);
|
||||
await setPlayList(
|
||||
playlist,
|
||||
index: indexTrack,
|
||||
position: positionInTrack,
|
||||
start: start,
|
||||
end: end,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setPlayList(
|
||||
List<Uri> playlist, {
|
||||
List<(Uri, Duration)> playlist, {
|
||||
int? index,
|
||||
Duration? position,
|
||||
Duration? start,
|
||||
Duration? end,
|
||||
});
|
||||
Future<void> play();
|
||||
Future<void> pause();
|
||||
|
|
@ -151,18 +163,20 @@ abstract class AbsAudioPlayer {
|
|||
Duration get position;
|
||||
Stream<Duration> get positionStream;
|
||||
|
||||
Duration get positionInChapter {
|
||||
final globalPosition = positionInBook;
|
||||
Duration get positionInChapter => getPositionInChapter(position);
|
||||
Duration getPositionInChapter(position) {
|
||||
final globalPosition = getPositionInBook(position);
|
||||
return globalPosition -
|
||||
(book?.findChapterAtTime(globalPosition).start ?? Duration.zero);
|
||||
}
|
||||
|
||||
Duration get positionInBook =>
|
||||
Duration get positionInBook => getPositionInBook(position);
|
||||
Duration getPositionInBook(position) =>
|
||||
position + (book?.tracks[currentIndex].startOffset ?? Duration.zero);
|
||||
|
||||
Stream<Duration> get positionInChapterStream =>
|
||||
positionStream.map((position) {
|
||||
return positionInChapter;
|
||||
return getPositionInChapter(position);
|
||||
});
|
||||
|
||||
Stream<Duration> get positionInBookStream => positionStream.map((position) {
|
||||
|
|
@ -336,3 +350,17 @@ extension BookExpandedExtension on BookExpanded {
|
|||
return tracks[index].startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
class AudioMetadata {
|
||||
final String album;
|
||||
final String title;
|
||||
final String artist;
|
||||
final String artwork;
|
||||
|
||||
AudioMetadata({
|
||||
required this.album,
|
||||
required this.title,
|
||||
required this.artist,
|
||||
required this.artwork,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,13 +72,15 @@ class AbsMpvAudioPlayer extends AbsAudioPlayer {
|
|||
|
||||
@override
|
||||
Future<void> setPlayList(
|
||||
List<Uri> playlist, {
|
||||
List<(Uri, Duration)> playlist, {
|
||||
int? index,
|
||||
Duration? position,
|
||||
Duration? start,
|
||||
Duration? end,
|
||||
}) async {
|
||||
await player.open(
|
||||
Playlist(
|
||||
playlist.map((uri) => Media(uri.toString())).toList(),
|
||||
playlist.map((uri) => Media(uri.$1.toString())).toList(),
|
||||
index: index ?? 0,
|
||||
),
|
||||
play: false,
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ final _logger = Logger('AbsPlatformAudioPlayer');
|
|||
/// 音频播放器 平台ios,macos,android (just_audio)
|
||||
class AbsPlatformAudioPlayer extends AbsAudioPlayer {
|
||||
late final AudioPlayer _player;
|
||||
AbsPlatformAudioPlayer(AudioPlayer player) {
|
||||
_player = player;
|
||||
AbsPlatformAudioPlayer() {
|
||||
// 跳转到播放列表指定条目指定位置
|
||||
// prefetch-playlist=yes
|
||||
JustAudioMediaKit.prefetchPlaylist = true;
|
||||
|
|
@ -19,8 +18,8 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
|
|||
// cache-pause-wait=60
|
||||
|
||||
JustAudioMediaKit.ensureInitialized();
|
||||
player = AudioPlayer();
|
||||
player.playerStateStream.listen((state) {
|
||||
_player = AudioPlayer();
|
||||
_player.playerStateStream.listen((state) {
|
||||
playerStateSubject.add(
|
||||
playerState.copyWith(
|
||||
playing: state.playing,
|
||||
|
|
@ -34,15 +33,15 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
|
|||
),
|
||||
);
|
||||
});
|
||||
player.positionStream.distinct().listen((position) {
|
||||
positionStream.listen((position) {
|
||||
final chapter = currentChapter;
|
||||
if (positionInBook <= (chapter?.start ?? Duration.zero) ||
|
||||
positionInBook <= (chapter?.end ?? Duration.zero)) {
|
||||
positionInBook >= (chapter?.end ?? Duration.zero)) {
|
||||
final chapter = book?.findChapterAtTime(positionInBook);
|
||||
if (chapter != currentChapter) {
|
||||
print('当前章节时长: ${currentChapter?.duration}');
|
||||
print('切换章节时长: ${chapter?.duration}');
|
||||
print('当前播放音轨时长: ${player.duration}');
|
||||
print('当前播放音轨时长: ${_player.duration}');
|
||||
chapterStreamController.add(chapter);
|
||||
}
|
||||
}
|
||||
|
|
@ -52,7 +51,11 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
|
|||
Duration get bufferedPosition => _player.bufferedPosition;
|
||||
|
||||
@override
|
||||
Stream<Duration> get bufferedPositionStream => _player.bufferedPositionStream;
|
||||
Stream<Duration> get bufferedPositionStream => _player.bufferedPositionStream
|
||||
.where(
|
||||
(_) => _player.playerState.processingState == ProcessingState.buffering,
|
||||
)
|
||||
.asBroadcastStream();
|
||||
|
||||
@override
|
||||
int get currentIndex => _player.currentIndex ?? 0;
|
||||
|
|
@ -76,24 +79,53 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
|
|||
Stream<bool> get playingStream => _player.playingStream;
|
||||
|
||||
@override
|
||||
Duration get position => _player.position;
|
||||
Duration get position => _addClippingStart(_player.position);
|
||||
|
||||
Duration _addClippingStart(Duration position, {bool add = true}) {
|
||||
if (_player.sequenceState.currentSource != null &&
|
||||
_player.sequenceState.currentSource is ClippingAudioSource) {
|
||||
final currentSource =
|
||||
_player.sequenceState.currentSource as ClippingAudioSource;
|
||||
if (currentSource.start != null) {
|
||||
return add
|
||||
? position + currentSource.start!
|
||||
: position - currentSource.start!;
|
||||
}
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Duration> get positionStream => _player.positionStream;
|
||||
Stream<Duration> get positionStream =>
|
||||
_player.positionStream.where((_) => _player.playing).map((position) {
|
||||
return _addClippingStart(position);
|
||||
});
|
||||
|
||||
@override
|
||||
Future<void> seek(Duration position, {int? index}) async {
|
||||
await _player.seek(position, index: index);
|
||||
await _player.seek(_addClippingStart(position, add: false), index: index);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setPlayList(
|
||||
List<Uri> playlist, {
|
||||
List<(Uri, Duration)> playlist, {
|
||||
int? index,
|
||||
Duration? position,
|
||||
Duration? start,
|
||||
Duration? end,
|
||||
}) async {
|
||||
List<AudioSource> audioSources =
|
||||
playlist.map((uri) => AudioSource.uri(uri)).toList();
|
||||
List<AudioSource> audioSources = start != null && start > Duration.zero ||
|
||||
end != null && end > Duration.zero
|
||||
? playlist
|
||||
.map(
|
||||
(item) => ClippingAudioSource(
|
||||
child: AudioSource.uri(item.$1),
|
||||
start: start,
|
||||
end: end == null ? null : item.$2 - end,
|
||||
),
|
||||
)
|
||||
.toList()
|
||||
: playlist.map((item) => AudioSource.uri(item.$1)).toList();
|
||||
await _player
|
||||
.setAudioSources(
|
||||
audioSources,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue