更改播放逻辑

This commit is contained in:
rang 2025-12-08 17:54:08 +08:00
parent 290b68336f
commit 420438c0df
29 changed files with 810 additions and 514 deletions

View file

@ -1,9 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:vaani/features/player/core/player_status.dart';
import 'package:vaani/features/player/providers/player_status_provider.dart';
import 'package:vaani/features/player/providers/audiobook_player.dart';
import 'package:vaani/features/player/providers/abs_provider.dart'
hide PlayerState;
import 'package:vaani/shared/audio_player.dart';
class AudiobookPlayerPlayPauseButton extends HookConsumerWidget {
const AudiobookPlayerPlayPauseButton({
@ -14,42 +13,39 @@ class AudiobookPlayerPlayPauseButton extends HookConsumerWidget {
final double iconSize;
@override
Widget build(BuildContext context, WidgetRef ref) {
final playerStatus =
ref.watch(playerStatusProvider.select((v) => v.playStatus));
final playerState = ref.watch(playerStateProvider);
return PlatformIconButton(
icon: _getIcon(playerStatus, context),
onPressed: () => _actionButtonPressed(playerStatus, ref),
return IconButton(
icon: _getIcon(playerState, context),
onPressed: () => _actionButtonPressed(playerState, ref),
);
}
Widget _getIcon(PlayStatus playerStatus, BuildContext context) {
switch (playerStatus) {
case PlayStatus.playing:
return Icon(size: iconSize, PlatformIcons(context).pause);
case PlayStatus.paused:
return Icon(size: iconSize, PlatformIcons(context).playArrow);
case PlayStatus.loading:
return PlatformCircularProgressIndicator();
default:
return Icon(size: iconSize, PlatformIcons(context).playArrow);
Widget _getIcon(PlayerState playerState, BuildContext context) {
if (playerState.playing) {
return Icon(size: iconSize, Icons.pause);
} else {
switch (playerState.processingState) {
case ProcessingState.loading || ProcessingState.buffering:
return CircularProgressIndicator();
default:
return Icon(size: iconSize, Icons.play_arrow);
}
}
}
void _actionButtonPressed(PlayStatus playerStatus, WidgetRef ref) async {
final player = ref.read(playerProvider);
switch (playerStatus) {
case PlayStatus.loading:
break;
case PlayStatus.playing:
await player.pause();
break;
case PlayStatus.completed:
await player.seekInBook(const Duration(seconds: 0));
await player.play();
break;
default:
await player.play();
void _actionButtonPressed(PlayerState playerState, WidgetRef ref) async {
final player = ref.read(absAudioPlayerProvider);
if (playerState.playing) {
await player.pause();
} else {
switch (playerState.processingState) {
case ProcessingState.completed:
await player.seekInBook(const Duration(seconds: 0));
await player.play();
default:
await player.play();
}
}
}
}