2025-11-28 17:05:35 +08:00
|
|
|
// my_audio_handler.dart
|
|
|
|
|
import 'dart:io';
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
2024-08-20 08:36:39 -04:00
|
|
|
import 'package:collection/collection.dart';
|
2025-11-28 17:05:35 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2024-05-17 11:04:20 -04:00
|
|
|
import 'package:just_audio/just_audio.dart';
|
2025-11-28 17:05:35 +08:00
|
|
|
import 'package:rxdart/rxdart.dart';
|
2024-05-14 06:13:16 -04:00
|
|
|
import 'package:shelfsdk/audiobookshelf_api.dart';
|
2025-11-28 17:05:35 +08:00
|
|
|
import 'package:vaani/features/player/core/player_status.dart' as core;
|
|
|
|
|
import 'package:vaani/features/player/providers/player_status_provider.dart';
|
|
|
|
|
import 'package:vaani/shared/extensions/chapter.dart';
|
2025-11-30 00:56:16 +08:00
|
|
|
import 'package:vaani/shared/extensions/model_conversions.dart';
|
2024-08-20 08:36:39 -04:00
|
|
|
|
2025-11-13 17:53:23 +08:00
|
|
|
// add a small offset so the display does not show the previous chapter for a split second
|
|
|
|
|
final offset = Duration(milliseconds: 10);
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|
|
|
|
final AudioPlayer _player = AudioPlayer();
|
|
|
|
|
// final List<AudioSource> _playlist = [];
|
|
|
|
|
final Ref ref;
|
2025-11-13 17:53:23 +08:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
PlaybackSessionExpanded? _session;
|
2024-05-19 08:53:21 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
final _currentChapterObject = BehaviorSubject<BookChapter?>.seeded(null);
|
|
|
|
|
AbsAudioHandler(this.ref) {
|
|
|
|
|
_setupAudioPlayer();
|
2024-05-14 06:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
void _setupAudioPlayer() {
|
|
|
|
|
final statusNotifier = ref.read(playerStatusProvider.notifier);
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// 转发播放状态
|
|
|
|
|
_player.playbackEventStream.map(_transformEvent).pipe(playbackState);
|
|
|
|
|
_player.playerStateStream.listen((event) {
|
|
|
|
|
if (event.playing) {
|
|
|
|
|
statusNotifier.setPlayStatusVerify(core.PlayStatus.playing);
|
|
|
|
|
} else {
|
|
|
|
|
statusNotifier.setPlayStatusVerify(core.PlayStatus.paused);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
_player.positionStream.distinct().listen((position) {
|
|
|
|
|
final chapter = _session?.findChapterAtTime(positionInBook);
|
|
|
|
|
if (chapter != currentChapter) {
|
|
|
|
|
_currentChapterObject.sink.add(chapter);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-05-19 08:53:21 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// 加载有声书
|
|
|
|
|
Future<void> setSourceAudiobook(
|
|
|
|
|
PlaybackSessionExpanded playbackSession, {
|
|
|
|
|
required Uri baseUrl,
|
|
|
|
|
required String token,
|
|
|
|
|
List<Uri>? downloadedUris,
|
|
|
|
|
}) async {
|
|
|
|
|
_session = playbackSession;
|
|
|
|
|
|
|
|
|
|
// 添加所有音轨
|
|
|
|
|
List<AudioSource> audioSources = [];
|
|
|
|
|
for (final track in playbackSession.audioTracks) {
|
|
|
|
|
audioSources.add(
|
|
|
|
|
AudioSource.uri(
|
|
|
|
|
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-30 00:56:16 +08:00
|
|
|
final track = playbackSession.findTrackAtTime(playbackSession.currentTime);
|
|
|
|
|
|
|
|
|
|
final item = MediaItem(
|
|
|
|
|
id: playbackSession.libraryItemId,
|
|
|
|
|
album: playbackSession.mediaMetadata.title,
|
|
|
|
|
artist: playbackSession.displayAuthor,
|
|
|
|
|
title: playbackSession.displayTitle,
|
|
|
|
|
displayTitle: playbackSession.displayTitle,
|
|
|
|
|
displaySubtitle: playbackSession.mediaType == MediaType.book
|
|
|
|
|
? playbackSession.mediaMetadata.asBookMetadata.subtitle
|
|
|
|
|
: null,
|
|
|
|
|
displayDescription: "测试描述",
|
|
|
|
|
duration: track.duration,
|
|
|
|
|
artUri: Uri.parse(
|
|
|
|
|
'$baseUrl/api/items/${playbackSession.libraryItemId}/cover?token=$token',
|
2025-11-28 17:05:35 +08:00
|
|
|
),
|
|
|
|
|
);
|
2025-11-30 00:56:16 +08:00
|
|
|
await playMediaItem(item);
|
2025-11-28 17:05:35 +08:00
|
|
|
final index = playbackSession.audioTracks.indexOf(track);
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
await _player.setAudioSources(
|
|
|
|
|
audioSources,
|
|
|
|
|
initialIndex: index,
|
|
|
|
|
initialPosition: playbackSession.currentTime - track.startOffset,
|
|
|
|
|
);
|
|
|
|
|
_player.seek(playbackSession.currentTime - track.startOffset, index: index);
|
|
|
|
|
await play();
|
|
|
|
|
// 恢复上次播放位置(如果有)
|
|
|
|
|
// if (initialPosition != null) {
|
|
|
|
|
// await seekInBook(initialPosition);
|
|
|
|
|
// }
|
|
|
|
|
}
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// // 音轨切换处理
|
|
|
|
|
// void _onTrackChanged(int trackIndex) {
|
|
|
|
|
// if (_book == null) return;
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// // 可以在这里处理音轨切换逻辑,比如预加载下一音轨
|
|
|
|
|
// // print('切换到音轨: ${_book!.tracks[trackIndex].title}');
|
|
|
|
|
// }
|
2024-05-15 02:27:05 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// 核心功能:跳转到指定章节
|
|
|
|
|
Future<void> skipToChapter(int chapterId) async {
|
|
|
|
|
if (_session == null) return;
|
|
|
|
|
|
|
|
|
|
final chapter = _session!.chapters.firstWhere(
|
|
|
|
|
(ch) => ch.id == chapterId,
|
|
|
|
|
orElse: () => throw Exception('Chapter not found'),
|
2024-10-03 05:54:29 -04:00
|
|
|
);
|
2025-11-28 17:05:35 +08:00
|
|
|
await seekInBook(chapter.start + offset);
|
|
|
|
|
}
|
2024-05-14 10:11:25 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
PlaybackSessionExpanded? get session => _session;
|
|
|
|
|
|
|
|
|
|
// 当前音轨
|
|
|
|
|
AudioTrack? get currentTrack {
|
|
|
|
|
if (_session == null || _player.currentIndex == null) {
|
2025-11-13 17:53:23 +08:00
|
|
|
return null;
|
2025-11-28 17:05:35 +08:00
|
|
|
}
|
|
|
|
|
return _session!.audioTracks[_player.currentIndex!];
|
2024-05-14 06:13:16 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// 当前章节
|
|
|
|
|
BookChapter? get currentChapter {
|
|
|
|
|
return _currentChapterObject.value;
|
|
|
|
|
}
|
2024-05-14 06:13:16 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
Duration get position => _player.position;
|
|
|
|
|
Duration get positionInChapter {
|
|
|
|
|
return _player.position +
|
|
|
|
|
(currentTrack?.startOffset ?? Duration.zero) -
|
|
|
|
|
(currentChapter?.start ?? Duration.zero);
|
2024-05-14 10:11:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
Duration get positionInBook {
|
|
|
|
|
return _player.position + (currentTrack?.startOffset ?? Duration.zero);
|
2025-10-24 11:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
Duration get bufferedPositionInBook {
|
|
|
|
|
return _player.bufferedPosition +
|
|
|
|
|
(currentTrack?.startOffset ?? Duration.zero);
|
|
|
|
|
}
|
2025-10-24 11:47:50 +08:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
Duration? get chapterDuration => currentChapter?.duration;
|
|
|
|
|
|
|
|
|
|
Stream<PlayerState> get playerStateStream => _player.playerStateStream;
|
|
|
|
|
|
|
|
|
|
Stream<Duration> get positionStream => _player.positionStream;
|
|
|
|
|
|
|
|
|
|
Stream<Duration> get positionStreamInBook {
|
|
|
|
|
return _player.positionStream.map((position) {
|
|
|
|
|
return position + (currentTrack?.startOffset ?? Duration.zero);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<Duration> get slowPositionStreamInBook {
|
|
|
|
|
final superPositionStream = _player.createPositionStream(
|
|
|
|
|
steps: 100,
|
|
|
|
|
minPeriod: const Duration(milliseconds: 500),
|
|
|
|
|
maxPeriod: const Duration(seconds: 1),
|
2025-11-13 17:53:23 +08:00
|
|
|
);
|
2025-11-28 17:05:35 +08:00
|
|
|
return superPositionStream.map((position) {
|
|
|
|
|
return position + (currentTrack?.startOffset ?? Duration.zero);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<Duration> get bufferedPositionStreamInBook {
|
|
|
|
|
return _player.bufferedPositionStream.map((position) {
|
|
|
|
|
return position + (currentTrack?.startOffset ?? Duration.zero);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<Duration> get positionStreamInChapter {
|
|
|
|
|
return _player.positionStream.distinct().map((position) {
|
|
|
|
|
return position +
|
|
|
|
|
(currentTrack?.startOffset ?? Duration.zero) -
|
|
|
|
|
(currentChapter?.start ?? Duration.zero);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<BookChapter?> get chapterStream => _currentChapterObject.stream;
|
|
|
|
|
|
|
|
|
|
Future<void> togglePlayPause() async {
|
|
|
|
|
// check if book is set
|
|
|
|
|
if (_session == null) {
|
|
|
|
|
return Future.value();
|
2025-10-31 17:21:27 +08:00
|
|
|
}
|
2025-11-28 17:05:35 +08:00
|
|
|
_player.playerState.playing ? await pause() : await play();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 播放控制方法
|
|
|
|
|
@override
|
|
|
|
|
Future<void> play() async {
|
|
|
|
|
await _player.play();
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-31 17:21:27 +08:00
|
|
|
@override
|
2025-11-28 17:05:35 +08:00
|
|
|
Future<void> pause() async {
|
|
|
|
|
await _player.pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写上一曲/下一曲为章节导航
|
|
|
|
|
@override
|
|
|
|
|
Future<void> skipToNext() async {
|
|
|
|
|
if (_session == null) {
|
2025-11-13 17:53:23 +08:00
|
|
|
// 回退到默认行为
|
2025-11-28 17:05:35 +08:00
|
|
|
return _player.seekToNext();
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
final chapter = currentChapter;
|
|
|
|
|
if (chapter == null) {
|
|
|
|
|
// 回退到默认行为
|
2025-11-28 17:05:35 +08:00
|
|
|
return _player.seekToNext();
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
2025-11-28 17:05:35 +08:00
|
|
|
final chapterIndex = _session!.chapters.indexOf(chapter);
|
|
|
|
|
if (chapterIndex < _session!.chapters.length - 1) {
|
2025-11-13 17:53:23 +08:00
|
|
|
// 跳到下一章
|
2025-11-28 17:05:35 +08:00
|
|
|
final nextChapter = _session!.chapters[chapterIndex + 1];
|
2025-11-13 17:53:23 +08:00
|
|
|
await skipToChapter(nextChapter.id);
|
2025-10-31 17:21:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
2025-11-28 17:05:35 +08:00
|
|
|
Future<void> skipToPrevious() async {
|
2025-11-13 17:53:23 +08:00
|
|
|
final chapter = currentChapter;
|
2025-11-28 17:05:35 +08:00
|
|
|
if (_session == null || chapter == null) {
|
|
|
|
|
return _player.seekToPrevious();
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
2025-11-28 17:05:35 +08:00
|
|
|
final currentIndex = _session!.chapters.indexOf(chapter);
|
2025-11-13 17:53:23 +08:00
|
|
|
if (currentIndex > 0) {
|
|
|
|
|
// 跳到上一章
|
2025-11-28 17:05:35 +08:00
|
|
|
final prevChapter = _session!.chapters[currentIndex - 1];
|
2025-11-13 17:53:23 +08:00
|
|
|
await skipToChapter(prevChapter.id);
|
|
|
|
|
} else {
|
|
|
|
|
// 已经是第一章,回到开头
|
|
|
|
|
await seekInBook(Duration.zero);
|
2025-10-31 17:21:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
@override
|
|
|
|
|
Future<void> seek(Duration position) async {
|
|
|
|
|
// 这个 position 是当前音轨内的位置,我们不直接使用
|
|
|
|
|
// 而是通过全局位置来控制
|
|
|
|
|
final track = currentTrack;
|
|
|
|
|
Duration startOffset = Duration.zero;
|
|
|
|
|
if (track != null) {
|
|
|
|
|
startOffset = track.startOffset;
|
2024-05-19 09:45:41 -04:00
|
|
|
}
|
2025-11-28 17:05:35 +08:00
|
|
|
await seekInBook(startOffset + position);
|
2024-05-19 09:45:41 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
Future<void> setVolume(double volume) async {
|
|
|
|
|
await _player.setVolume(volume);
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
@override
|
|
|
|
|
Future<void> setSpeed(double speed) async {
|
|
|
|
|
await _player.setSpeed(speed);
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
// 核心功能:跳转到全局时间位置
|
|
|
|
|
Future<void> seekInBook(Duration globalPosition) async {
|
|
|
|
|
if (_session == null) return;
|
|
|
|
|
// 找到目标音轨和在音轨内的位置
|
|
|
|
|
final track = _session!.findTrackAtTime(globalPosition);
|
|
|
|
|
final index = _session!.audioTracks.indexOf(track);
|
|
|
|
|
Duration positionInTrack = globalPosition - track.startOffset;
|
|
|
|
|
if (positionInTrack < Duration.zero) {
|
|
|
|
|
positionInTrack = Duration.zero;
|
|
|
|
|
}
|
|
|
|
|
// 切换到目标音轨具体位置
|
|
|
|
|
await _player.seek(positionInTrack, index: index);
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
AudioPlayer get player => _player;
|
|
|
|
|
PlaybackState _transformEvent(PlaybackEvent event) {
|
|
|
|
|
return PlaybackState(
|
|
|
|
|
controls: [
|
|
|
|
|
if (kIsWeb || !Platform.isAndroid) MediaControl.skipToPrevious,
|
|
|
|
|
MediaControl.rewind,
|
|
|
|
|
if (_player.playing) MediaControl.pause else MediaControl.play,
|
|
|
|
|
MediaControl.stop,
|
|
|
|
|
MediaControl.fastForward,
|
|
|
|
|
if (kIsWeb || !Platform.isAndroid) MediaControl.skipToNext,
|
|
|
|
|
],
|
|
|
|
|
systemActions: {
|
|
|
|
|
if (kIsWeb || !Platform.isAndroid) MediaAction.skipToPrevious,
|
|
|
|
|
MediaAction.rewind,
|
|
|
|
|
MediaAction.seek,
|
|
|
|
|
MediaAction.fastForward,
|
|
|
|
|
MediaAction.stop,
|
|
|
|
|
MediaAction.setSpeed,
|
|
|
|
|
if (kIsWeb || !Platform.isAndroid) MediaAction.skipToNext,
|
|
|
|
|
},
|
|
|
|
|
androidCompactActionIndices: const [1, 2, 3],
|
|
|
|
|
processingState: const {
|
|
|
|
|
ProcessingState.idle: AudioProcessingState.idle,
|
|
|
|
|
ProcessingState.loading: AudioProcessingState.loading,
|
|
|
|
|
ProcessingState.buffering: AudioProcessingState.buffering,
|
|
|
|
|
ProcessingState.ready: AudioProcessingState.ready,
|
|
|
|
|
ProcessingState.completed: AudioProcessingState.completed,
|
|
|
|
|
}[_player.processingState] ??
|
|
|
|
|
AudioProcessingState.idle,
|
|
|
|
|
playing: _player.playing,
|
|
|
|
|
updatePosition: _player.position,
|
|
|
|
|
bufferedPosition: event.bufferedPosition,
|
|
|
|
|
speed: _player.speed,
|
|
|
|
|
queueIndex: event.currentIndex,
|
|
|
|
|
captioningEnabled: false,
|
2024-05-20 07:45:06 -04:00
|
|
|
);
|
2024-05-19 08:53:21 -04:00
|
|
|
}
|
2024-05-15 02:27:05 -04:00
|
|
|
}
|
2024-08-20 08:36:39 -04:00
|
|
|
|
|
|
|
|
Uri _getUri(
|
|
|
|
|
AudioTrack track,
|
|
|
|
|
List<Uri>? downloadedUris, {
|
|
|
|
|
required Uri baseUrl,
|
|
|
|
|
required String token,
|
|
|
|
|
}) {
|
|
|
|
|
// check if the track is in the downloadedUris
|
|
|
|
|
final uri = downloadedUris?.firstWhereOrNull(
|
|
|
|
|
(element) {
|
|
|
|
|
return element.pathSegments.last == track.metadata?.filename;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-25 10:38:56 +08:00
|
|
|
return uri ??
|
|
|
|
|
Uri.parse('${baseUrl.toString()}${track.contentUrl}?token=$token');
|
2024-08-20 08:36:39 -04:00
|
|
|
}
|
2024-09-25 03:13:42 -04:00
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
extension PlaybackSessionExpandedExtension on PlaybackSessionExpanded {
|
2025-11-13 17:53:23 +08:00
|
|
|
BookChapter findChapterAtTime(Duration position) {
|
|
|
|
|
return chapters.firstWhere(
|
|
|
|
|
(element) {
|
|
|
|
|
return element.start <= position && element.end >= position + offset;
|
|
|
|
|
},
|
|
|
|
|
orElse: () => chapters.first,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioTrack findTrackAtTime(Duration position) {
|
2025-11-28 17:05:35 +08:00
|
|
|
return audioTracks.firstWhere(
|
2025-11-13 17:53:23 +08:00
|
|
|
(element) {
|
|
|
|
|
return element.startOffset <= position &&
|
|
|
|
|
element.startOffset + element.duration >= position + offset;
|
|
|
|
|
},
|
2025-11-28 17:05:35 +08:00
|
|
|
orElse: () => audioTracks.first,
|
2025-11-13 17:53:23 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 17:05:35 +08:00
|
|
|
int findTrackIndexAtTime(Duration position) {
|
|
|
|
|
return audioTracks.indexWhere((element) {
|
|
|
|
|
return element.startOffset <= position &&
|
|
|
|
|
element.startOffset + element.duration >= position + offset;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 17:53:23 +08:00
|
|
|
Duration getTrackStartOffset(int index) {
|
2025-11-28 17:05:35 +08:00
|
|
|
return audioTracks[index].startOffset;
|
2025-11-13 17:53:23 +08:00
|
|
|
}
|
|
|
|
|
}
|