mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-17 14:59:35 +00:00
更改播放逻辑
This commit is contained in:
parent
290b68336f
commit
420438c0df
29 changed files with 810 additions and 514 deletions
|
|
@ -1,17 +1,22 @@
|
|||
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';
|
||||
import 'package:vaani/features/player/providers/abs_provider.dart'
|
||||
hide AbsAudioPlayer;
|
||||
import 'package:vaani/shared/audio_player.dart';
|
||||
|
||||
class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
||||
final Player player = Player();
|
||||
final AbsAudioPlayer player;
|
||||
|
||||
AbsAudioHandler(Ref ref) {
|
||||
AbsAudioHandler(this.player) {
|
||||
player.mediaItemStream.listen((item) {
|
||||
mediaItem.add(item);
|
||||
});
|
||||
playbackState.add(
|
||||
playbackState.value.copyWith(
|
||||
controls: [
|
||||
MediaControl.skipToPrevious,
|
||||
if (player.state.playing) MediaControl.pause else MediaControl.play,
|
||||
// if (player.state.playing) MediaControl.pause else MediaControl.play,
|
||||
// MediaControl.rewind,
|
||||
// MediaControl.fastForward,
|
||||
MediaControl.skipToNext,
|
||||
|
|
@ -27,48 +32,40 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
),
|
||||
);
|
||||
|
||||
final absState = ref.read(absStateProvider.notifier);
|
||||
// 1. 转发播放/暂停状态
|
||||
player.stream.playing.listen((bool playing) {
|
||||
playbackState.add(playbackState.value.copyWith(
|
||||
playing: playing,
|
||||
// 根据 playing 和实际情况更新 processingState
|
||||
processingState: player.state.completed
|
||||
? AudioProcessingState.completed
|
||||
: player.state.buffering
|
||||
? AudioProcessingState.buffering
|
||||
: AudioProcessingState.ready,
|
||||
));
|
||||
absState.updataPlaying(playing);
|
||||
player.playerStateStream.listen((playerState) {
|
||||
playbackState.add(
|
||||
playbackState.value.copyWith(
|
||||
playing: playerState.playing,
|
||||
// 根据 playing 和实际情况更新 processingState
|
||||
processingState: const {
|
||||
ProcessingState.idle: AudioProcessingState.idle,
|
||||
ProcessingState.loading: AudioProcessingState.loading,
|
||||
ProcessingState.buffering: AudioProcessingState.buffering,
|
||||
ProcessingState.ready: AudioProcessingState.ready,
|
||||
ProcessingState.completed: AudioProcessingState.completed,
|
||||
}[playerState.processingState] ??
|
||||
AudioProcessingState.idle,
|
||||
),
|
||||
);
|
||||
});
|
||||
// 2. 转发播放位置
|
||||
player.stream.position.listen((Duration position) {
|
||||
player.positionStream.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);
|
||||
// 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('播放完成');
|
||||
// });
|
||||
}
|
||||
|
||||
// 播放控制方法重写
|
||||
|
|
@ -99,14 +96,10 @@ class AbsAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
|
|||
|
||||
@override
|
||||
Future<void> setSpeed(double speed) async {
|
||||
await player.setRate(speed);
|
||||
await player.setSpeed(speed);
|
||||
}
|
||||
|
||||
Future<void> setVolume(double volume) async {
|
||||
final state = player.state;
|
||||
await player.setVolume(volume);
|
||||
}
|
||||
|
||||
PlayerStream get stream => player.stream;
|
||||
PlayerState get state => player.state;
|
||||
}
|
||||
|
|
|
|||
33
lib/features/player/core/init.dart
Normal file
33
lib/features/player/core/init.dart
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:audio_session/audio_session.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:vaani/features/player/core/abs_audio_handler.dart' as core;
|
||||
import 'package:vaani/features/player/providers/abs_provider.dart';
|
||||
import 'package:vaani/globals.dart';
|
||||
|
||||
Future<void> configurePlayer(ProviderContainer container) 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());
|
||||
|
||||
await AudioService.init(
|
||||
builder: () => core.AbsAudioHandler(container.read(absAudioPlayerProvider)),
|
||||
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');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue