Skip to content

Commit

Permalink
add player speed modify
Browse files Browse the repository at this point in the history
  • Loading branch information
Predidit committed Mar 28, 2024
1 parent a4e583f commit fc3fefb
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/pages/video/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ abstract class _VideoController with Store {
@observable
double brightness = 0;

// 播放器倍速
@observable
double playerSpeed = 1.0;

// 安卓全屏状态
@observable
bool androidFullscreen = false;
Expand Down Expand Up @@ -84,4 +88,14 @@ abstract class _VideoController with Store {
await playerController.init();
debugPrint('PlayController 重新初始化成功');
}

Future setPlaybackSpeed(double playerSpeed) async {
final PlayerController playerController = Modular.get<PlayerController>();
try {
playerController.mediaPlayer.setRate(playerSpeed);
} catch(e) {
debugPrint(e.toString());
}
this.playerSpeed = playerSpeed;
}
}
17 changes: 17 additions & 0 deletions lib/pages/video/video_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions lib/pages/video/video_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
import 'package:window_manager/window_manager.dart';
import 'package:screen_brightness/screen_brightness.dart';
import 'package:flutter_volume_controller/flutter_volume_controller.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class VideoPage extends StatefulWidget {
const VideoPage({super.key});
Expand All @@ -32,6 +33,7 @@ class _RatingPageState extends State<VideoPage> with WindowListener {
@override
void initState() {
super.initState();
videoController.playerSpeed = 1.0;
if (playerController.bvid == '') {
videoController.init(playerController).then((_) {
playerTimer = getPlayerTimer();
Expand Down Expand Up @@ -108,6 +110,70 @@ class _RatingPageState extends State<VideoPage> with WindowListener {
} catch (_) {}
}

// 选择倍速
void showSetSpeedSheet() {
final double currentSpeed = videoController.playerSpeed;
final List<double> speedsList = [
0.25,
0.5,
0.75,
1.0,
1.25,
1.5,
1.75,
2.0
];
SmartDialog.show(useAnimation: false, builder: (context) {
return AlertDialog(
title: const Text('播放速度'),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Wrap(
spacing: 8,
runSpacing: 2,
children: [
for (final double i in speedsList) ...<Widget>[
if (i == currentSpeed) ...<Widget>[
FilledButton(
onPressed: () async {
await videoController.setPlaybackSpeed(i);
SmartDialog.dismiss();
},
child: Text(i.toString()),
),
] else ...[
FilledButton.tonal(
onPressed: () async {
await videoController.setPlaybackSpeed(i);
SmartDialog.dismiss();
},
child: Text(i.toString()),
),
]
]
],
);
}),
actions: <Widget>[
TextButton(
onPressed: () => SmartDialog.dismiss(),
child: Text(
'取消',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
TextButton(
onPressed: () async {
await videoController.setPlaybackSpeed(1.0);
SmartDialog.dismiss();
},
child: const Text('默认速度'),
),
],
);
});
}

getPlayerTimer() {
return Timer.periodic(const Duration(seconds: 1), (timer) {
videoController.playing = playerController.mediaPlayer.state.playing;
Expand Down Expand Up @@ -192,6 +258,37 @@ class _RatingPageState extends State<VideoPage> with WindowListener {
),
),

// 倍速条
(videoController.showPositioned ||
!playerController.mediaPlayer.state.playing)
? Positioned(
top: 0,
right: 0,
child: Row(
children: [
SizedBox(
width: 45,
height: 34,
child: TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(
EdgeInsets.zero),
),
onPressed: () => showSetSpeedSheet(),
child: Text(
'${videoController.playerSpeed}X',
style: const TextStyle(
color: Colors.white,
fontSize: 12,
),
),
),
),
],
),
)
: Container(),

// 播放器手势控制
Positioned.fill(
left: 16,
Expand Down

0 comments on commit fc3fefb

Please sign in to comment.