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 23, 2024
1 parent 89f6686 commit 8eef7c1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 15 deletions.
15 changes: 15 additions & 0 deletions lib/pages/video/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ abstract class _VideoController with Store {
@observable
double brightness = 0;

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

// 安卓全屏状态
@observable
bool androidFullscreen = false;
Expand All @@ -72,6 +76,16 @@ abstract class _VideoController with Store {
String title = '';
String from = '/tab/popular/';

Future setPlaybackSpeed(double playerSpeed) async {
final PlayerController playerController = Modular.get<PlayerController>();
try {
playerController.mediaPlayer.setRate(playerSpeed);
} catch(e) {
debugPrint(e.toString());
}
this.playerSpeed = playerSpeed;
}

Future changeEpisode(int episode) async {
final PlayerController playerController = Modular.get<PlayerController>();
final PopularController popularController =
Expand All @@ -92,6 +106,7 @@ abstract class _VideoController with Store {
} catch (e) {
debugPrint(e.toString());
}
playerSpeed = 1.0;
await playerController.init();
}

Expand Down
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.

115 changes: 100 additions & 15 deletions lib/pages/video/video_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _VideoPageState extends State<VideoPage> with WindowListener {
@override
void initState() {
super.initState();
// videoController.episode = 1;
videoController.playerSpeed = 1.0;
playerController.videoUrl = videoController.videoUrl;
playerController.videoCookie = videoController.videoCookie;
playerController.init();
Expand All @@ -73,6 +73,11 @@ class _VideoPageState extends State<VideoPage> with WindowListener {
debugPrint(e.toString());
}
playerTimer = getPlayerTimer();
try {
playerController.mediaPlayer.setRate(videoController.playerSpeed);
} catch(e) {
debugPrint(e.toString());
}
}

@override
Expand Down Expand Up @@ -146,26 +151,79 @@ class _VideoPageState extends State<VideoPage> with WindowListener {
} catch (_) {}
}

Future<void> raiseVolume(double value) async {
try {
FlutterVolumeController.updateShowSystemUI(false);
await FlutterVolumeController.raiseVolume(value);
} catch (_) {}
}

Future<void> lowerVolume(double value) async {
try {
FlutterVolumeController.updateShowSystemUI(false);
await FlutterVolumeController.lowerVolume(value);
} catch (_) {}
}

Future<void> setBrightness(double value) async {
try {
await ScreenBrightness().setScreenBrightness(value);
} 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
];
showDialog(
context: context,
builder: (BuildContext 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);
Modular.to.pop();
},
child: Text(i.toString()),
),
] else ...[
FilledButton.tonal(
onPressed: () async {
await videoController.setPlaybackSpeed(i);
Modular.to.pop();
},
child: Text(i.toString()),
),
]
]
],
);
}),
actions: <Widget>[
TextButton(
onPressed: () => Modular.to.pop(),
child: Text(
'取消',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
TextButton(
onPressed: () async {
await videoController.setPlaybackSpeed(1.0);
Modular.to.pop();
},
child: const Text('默认速度'),
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
navigationBarState = Platform.isWindows
Expand Down Expand Up @@ -449,6 +507,33 @@ class _VideoPageState extends State<VideoPage> with WindowListener {
)
: Container(),

// 倍速条
(videoController.showPositioned ||
!playerController.mediaPlayer.state.playing)
? Positioned(
top: 0,
right: 0,
child: 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(),

// 自定义播放器底部组件
(videoController.showPositioned ||
!playerController.mediaPlayer.state.playing)
Expand Down

0 comments on commit 8eef7c1

Please sign in to comment.