mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 14:29:35 +00:00
优化一下通知栏显示
This commit is contained in:
parent
d6894c3191
commit
f4f860f3ec
12 changed files with 133 additions and 69 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue