更改播放器为media_kit

This commit is contained in:
rang 2025-12-06 17:50:37 +08:00
parent 1a317308dd
commit ac6158123f
14 changed files with 1092 additions and 878 deletions

View file

@ -1,10 +1,48 @@
import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';
import 'package:media_kit/media_kit.dart';
class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
final AudioPlayer player;
final Player player = Player();
AbsAudioHandler(this.player);
AbsAudioHandler() {
// 1. /
player.stream.playing.listen((bool playing) {
playbackState.add(playbackState.value.copyWith(
playing: playing,
// playing processingState
processingState:
playing ? AudioProcessingState.ready : AudioProcessingState.idle,
));
});
// 2.
player.stream.position.listen((Duration position) {
playbackState.add(playbackState.value.copyWith(
updatePosition: position,
));
});
// 3.
player.stream.duration.listen((Duration? duration) {
// mediaItem duration
final currentItem = mediaItem.value;
if (currentItem != null && duration != null) {
mediaItem.add(currentItem.copyWith(duration: duration));
}
});
player.stream.completed.listen((bool playing) {
print('播放完成');
});
}
Future<void> playOrPause() async {
await player.playOrPause();
}
Future<void> setMediaItem(MediaItem mediaItem) async {
this.mediaItem.add(mediaItem);
await player.open(Media(mediaItem.id), play: false);
// ignore: unnecessary_null_comparison
await player.stream.duration.firstWhere((d) => d != null);
}
//
@override
@ -19,12 +57,12 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
@override
Future<void> skipToNext() async {
await player.seekToNext();
await player.next();
}
@override
Future<void> skipToPrevious() async {
await player.seekToPrevious();
await player.previous();
}
@override
@ -34,6 +72,10 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
@override
Future<void> setSpeed(double speed) async {
await player.setSpeed(speed);
await player.setRate(speed);
}
Future<void> setVolume(double volume) async {
await player.setVolume(volume);
}
}

View file

@ -1,5 +1,29 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:just_audio/just_audio.dart';
import 'package:shelfsdk/audiobookshelf_api.dart';
// import 'package:just_audio/just_audio.dart';
import 'package:shelfsdk/audiobookshelf_api.dart' as api;
class AbsAudioPlayer extends AudioPlayer {}
class AbsPlayerState {
api.BookExpanded? book;
//
final api.BookChapter? currentChapter;
//
final int currentIndex;
AbsPlayerState({
this.book,
this.currentChapter,
this.currentIndex = 0,
});
AbsPlayerState copyWith({
api.BookExpanded? book,
api.BookChapter? currentChapter,
int? currentIndex,
}) {
return AbsPlayerState(
book: book ?? this.book,
currentChapter: currentChapter ?? this.currentChapter,
currentIndex: currentIndex ?? this.currentIndex,
);
}
}

View file

@ -0,0 +1,30 @@
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;
}