This commit is contained in:
rang 2025-12-09 23:38:51 +08:00
parent 20a3b95edc
commit 4a7c592e41
9 changed files with 112 additions and 102 deletions

View file

@ -26,6 +26,7 @@ abstract class AbsAudioPlayer {
final chapterStreamController = BehaviorSubject<BookChapter?>.seeded(null);
BookExpanded? get book => _bookStreamController.nvalue;
AudioTrack? get currentTrack => book?.tracks[currentIndex];
BookChapter? get currentChapter => chapterStreamController.nvalue;
AbsPlayerState get playerState => playerStateSubject.value;
Stream<MediaItem?> get mediaItemStream => _mediaItemController.stream;
@ -68,8 +69,8 @@ abstract class AbsAudioPlayer {
_mediaItemController.sink.add(item);
final playlist = book.tracks
.map(
(track) => _getUri(currentTrack, downloadedUris,
baseUrl: baseUrl, token: token),
(track) =>
_getUri(track, downloadedUris, baseUrl: baseUrl, token: token),
)
.toList();
await setPlayList(playlist, index: indexTrack, position: positionInTrack);

View file

@ -26,9 +26,13 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
);
});
player.positionStream.distinct().listen((position) {
final chapter = book?.findChapterAtTime(positionInBook);
if (chapter != currentChapter) {
chapterStreamController.add(chapter);
final chapter = currentChapter;
if (positionInBook <= (chapter?.start ?? Duration.zero) ||
positionInBook <= (chapter?.end ?? Duration.zero)) {
final chapter = book?.findChapterAtTime(positionInBook);
if (chapter != currentChapter) {
chapterStreamController.add(chapter);
}
}
});
}
@ -103,4 +107,10 @@ class AbsPlatformAudioPlayer extends AbsAudioPlayer {
@override
double get speed => player.speed;
@override
void dispose() {
super.dispose();
player.dispose();
}
}

View file

@ -1,32 +1,34 @@
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:vaani/features/player/core/abs_audio_handler.dart' as core;
import 'package:vaani/features/player/core/abs_audio_player.dart';
import 'package:vaani/globals.dart';
// import 'package:audio_service/audio_service.dart';
// import 'package:audio_session/audio_session.dart';
// import 'package:riverpod_annotation/riverpod_annotation.dart';
// import 'package:vaani/features/player/core/abs_audio_handler.dart' as core;
// import 'package:vaani/features/player/core/abs_audio_player.dart';
// import 'package:vaani/globals.dart';
///
Future<void> configurePlayer(AbsAudioPlayer player) async {
// for playing audio on windows, linux
// ///
// @Riverpod(keepAlive: true)
// Future<void> configurePlayer(AbsAudioPlayer player) async {
// // for playing audio on windows, linux
// for configuring how this app will interact with other audio apps
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration.speech());
// // for configuring how this app will interact with other audio apps
// final session = await AudioSession.instance;
// await session.configure(const AudioSessionConfiguration.speech());
await AudioService.init(
builder: () => core.AbsAudioHandler(player),
config: const AudioServiceConfig(
androidNotificationChannelId: 'dr.blank.vaani.channel.audio',
androidNotificationChannelName: 'ABSPlayback',
androidNotificationChannelDescription:
'Needed to control audio from lock screen',
androidNotificationOngoing: false,
androidStopForegroundOnPause: false,
androidNotificationIcon: 'drawable/ic_stat_logo',
preloadArtwork: true,
// fastForwardInterval: Duration(seconds: 20),
// rewindInterval: Duration(seconds: 20),
),
);
// await AudioService.init(
// builder: () => core.AbsAudioHandler(player),
// config: const AudioServiceConfig(
// androidNotificationChannelId: 'dr.blank.vaani.channel.audio',
// androidNotificationChannelName: 'ABSPlayback',
// androidNotificationChannelDescription:
// 'Needed to control audio from lock screen',
// androidNotificationOngoing: false,
// androidStopForegroundOnPause: false,
// androidNotificationIcon: 'drawable/ic_stat_logo',
// preloadArtwork: true,
// // fastForwardInterval: Duration(seconds: 20),
// // rewindInterval: Duration(seconds: 20),
// ),
// );
appLogger.finer('created simple player');
}
// appLogger.finer('created simple player');
// }

View file

@ -1,13 +1,48 @@
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shelfsdk/audiobookshelf_api.dart' as api;
import 'package:vaani/api/api_provider.dart';
import 'package:vaani/features/player/core/abs_audio_handler.dart';
import 'package:vaani/features/player/core/abs_audio_player.dart' as core;
import 'package:vaani/features/player/core/abs_audio_player_platform.dart';
part 'abs_provider.g.dart';
// final _logger = Logger('AbsPlayerProvider');
final _logger = Logger('AbsPlayerProvider');
///
@Riverpod(keepAlive: true)
Future<AudioHandler> configurePlayer(Ref ref) async {
final player = ref.read(audioPlayerProvider);
// for playing audio on windows, linux
// for configuring how this app will interact with other audio apps
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration.speech());
final audioService = await AudioService.init(
builder: () => AbsAudioHandler(player),
config: const AudioServiceConfig(
androidNotificationChannelId: 'dr.blank.vaani.channel.audio',
androidNotificationChannelName: 'ABSPlayback',
androidNotificationChannelDescription:
'Needed to control audio from lock screen',
androidNotificationOngoing: false,
androidStopForegroundOnPause: false,
androidNotificationIcon: 'drawable/ic_stat_logo',
preloadArtwork: true,
// fastForwardInterval: Duration(seconds: 20),
// rewindInterval: Duration(seconds: 20),
),
);
_logger.finer('created simple player');
return audioService;
}
/// riverpod状态
@Riverpod(keepAlive: true)
class AudioPlayer extends _$AudioPlayer {
@ -15,6 +50,7 @@ class AudioPlayer extends _$AudioPlayer {
core.AbsAudioPlayer build() {
// final player = AbsMpvAudioPlayer();
final player = AbsPlatformAudioPlayer();
ref.onDispose(player.dispose);
return player;
}

View file

@ -6,6 +6,25 @@ part of 'abs_provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$configurePlayerHash() => r'7e7003c815e7d240a67cd581931839cc60346707';
///
///
/// Copied from [configurePlayer].
@ProviderFor(configurePlayer)
final configurePlayerProvider = FutureProvider<AudioHandler>.internal(
configurePlayer,
name: r'configurePlayerProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$configurePlayerHash,
dependencies: null,
allTransitiveDependencies: null,
);
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef ConfigurePlayerRef = FutureProviderRef<AudioHandler>;
String _$isPlayerActiveHash() => r'71a24418ecf6c1a2d8160b0d0c8fc523d5679e76';
/// See also [isPlayerActive].
@ -58,7 +77,7 @@ final currentChaptersProvider =
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef CurrentChaptersRef = AutoDisposeProviderRef<List<api.BookChapter>>;
String _$audioPlayerHash() => r'26387ece7f0d0c0cc21dc7641853e643866726f6';
String _$audioPlayerHash() => r'e0efa20f340adf3312a76e5ca9e0c8ab0273a22a';
/// riverpod状态
///

View file

@ -23,10 +23,13 @@ class SkipStartEnd {
player.chapterStream.listen((chapter) {
if (chapter != null &&
player.positionInChapter < Duration(seconds: 1)) {
Future.microtask(
() => throttlerStart
.call(() => player.seekInBook(chapter.start + start)),
);
// player.pause();
player.seekInBook(chapter.start + start);
// player.play();
// Future.microtask(
// () => throttlerStart
// .call(() => player.seekInBook(chapter.start + start)),
// );
}
}),
);