mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 22:39:34 +00:00
更改播放器为media_kit
This commit is contained in:
parent
1a317308dd
commit
ac6158123f
14 changed files with 1092 additions and 878 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
lib/features/player/core/init.dart
Normal file
30
lib/features/player/core/init.dart
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue