优化一下通知栏显示

This commit is contained in:
rang 2025-12-07 17:55:07 +08:00
parent d6894c3191
commit f4f860f3ec
12 changed files with 133 additions and 69 deletions

View file

@ -1,18 +1,45 @@
import 'package:audio_service/audio_service.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:media_kit/media_kit.dart';
import 'package:vaani/features/player/providers/abs_provider.dart';
class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
final Player player = Player();
AbsAudioHandler() {
AbsAudioHandler(Ref ref) {
playbackState.add(
playbackState.value.copyWith(
controls: [
MediaControl.skipToPrevious,
if (player.state.playing) MediaControl.pause else MediaControl.play,
// MediaControl.rewind,
// MediaControl.fastForward,
MediaControl.skipToNext,
MediaControl.stop,
],
systemActions: {
MediaAction.play,
MediaAction.pause,
MediaAction.seek,
MediaAction.seekForward,
MediaAction.seekBackward,
},
),
);
final absState = ref.read(absStateProvider.notifier);
// 1. /
player.stream.playing.listen((bool playing) {
playbackState.add(playbackState.value.copyWith(
playing: playing,
// playing processingState
processingState:
playing ? AudioProcessingState.ready : AudioProcessingState.idle,
processingState: player.state.completed
? AudioProcessingState.completed
: player.state.buffering
? AudioProcessingState.buffering
: AudioProcessingState.ready,
));
absState.updataPlaying(playing);
});
// 2.
player.stream.position.listen((Duration position) {
@ -76,6 +103,10 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
}
Future<void> setVolume(double volume) async {
final state = player.state;
await player.setVolume(volume);
}
PlayerStream get stream => player.stream;
PlayerState get state => player.state;
}

View file

@ -8,22 +8,26 @@ class AbsPlayerState {
final api.BookChapter? currentChapter;
//
final int currentIndex;
final bool playing;
AbsPlayerState({
this.book,
this.currentChapter,
this.currentIndex = 0,
this.playing = false,
});
AbsPlayerState copyWith({
api.BookExpanded? book,
api.BookChapter? currentChapter,
int? currentIndex,
bool? playing,
}) {
return AbsPlayerState(
book: book ?? this.book,
currentChapter: currentChapter ?? this.currentChapter,
currentIndex: currentIndex ?? this.currentIndex,
playing: playing ?? this.playing,
);
}
}

View file

@ -1,30 +0,0 @@
import 'package:audio_service/audio_service.dart';
import 'package:audio_session/audio_session.dart';
import 'package:media_kit/media_kit.dart';
import 'package:vaani/features/player/core/abs_audio_handler.dart' as core;
Future<core.AbsAudioHandler> absAudioHandlerInit() async {
// for playing audio on windows, linux
MediaKit.ensureInitialized();
// 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: () => core.AbsAudioHandler(),
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),
),
);
return audioService;
}