Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playback speed controls #390

Merged
merged 8 commits into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/src/chewie_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,17 @@ class ChewieController extends ChangeNotifier {
this.isLive = false,
this.allowFullScreen = true,
this.allowMuting = true,
this.allowPlaybackSpeedChanging = true,
this.playbackSpeeds = const [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2],
this.systemOverlaysOnEnterFullScreen,
this.deviceOrientationsOnEnterFullScreen,
this.systemOverlaysAfterFullScreen = SystemUiOverlay.values,
this.deviceOrientationsAfterFullScreen = DeviceOrientation.values,
this.routePageBuilder,
}) : assert(videoPlayerController != null,
'You must provide a controller to play a video') {
}) : assert(videoPlayerController != null,
'You must provide a controller to play a video'),
assert(playbackSpeeds.every((speed) => speed > 0),
'The playbackSpeeds values must all be greater than 0') {
_initialize();
}

Expand Down Expand Up @@ -298,6 +302,12 @@ class ChewieController extends ChangeNotifier {
/// Defines if the mute control should be shown
final bool allowMuting;

/// Defines if the playback speed control should be shown
final bool allowPlaybackSpeedChanging;

/// Defines the set of allowed playback speeds user can change
final List<double> playbackSpeeds;

/// Defines the system overlays visible on entering fullscreen
final List<SystemUiOverlay> systemOverlaysOnEnterFullScreen;

Expand Down
97 changes: 96 additions & 1 deletion lib/src/cupertino_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ class _CupertinoControlsState extends State<CupertinoControls> {
_buildSkipForward(iconColor, barHeight),
_buildPosition(iconColor),
_buildProgressBar(),
_buildRemaining(iconColor)
_buildRemaining(iconColor),
chewieController.allowPlaybackSpeedChanging
? _buildSpeedButton(
controller, iconColor, barHeight)
: Container(),
],
),
),
Expand Down Expand Up @@ -371,6 +375,58 @@ class _CupertinoControlsState extends State<CupertinoControls> {
);
}

GestureDetector _buildSpeedButton(
VideoPlayerController controller,
Color iconColor,
double barHeight,
) {
return GestureDetector(
onTap: () async {
_hideTimer?.cancel();

final chosenSpeed = await showCupertinoModalPopup<double>(
context: context,
semanticsDismissible: true,
useRootNavigator: true,
builder: (context) => _PlaybackSpeedDialog(
speeds: chewieController.playbackSpeeds,
selected: _latestValue.playbackSpeed,
),
);

if (chosenSpeed != null) {
controller.setPlaybackSpeed(chosenSpeed);
}

if (_latestValue.isPlaying) {
_startHideTimer();
}
},
child: Container(
height: barHeight,
color: Colors.transparent,
padding: EdgeInsets.only(
left: 6.0,
right: 8.0,
),
margin: EdgeInsets.only(
right: 8.0,
),
child: Transform(
alignment: Alignment.center,
transform: Matrix4.skewY(0.0)
..rotateX(math.pi)
..rotateZ(math.pi * 0.8),
child: Icon(
Icons.speed,
color: iconColor,
size: 18.0,
),
),
),
);
}

Widget _buildTopBar(
Color backgroundColor,
Color iconColor,
Expand Down Expand Up @@ -545,3 +601,42 @@ class _CupertinoControlsState extends State<CupertinoControls> {
});
}
}

class _PlaybackSpeedDialog extends StatelessWidget {
const _PlaybackSpeedDialog({
Key key,
@required List<double> speeds,
@required double selected,
}) : _speeds = speeds,
_selected = selected,
super(key: key);

final List<double> _speeds;
final double _selected;

@override
Widget build(BuildContext context) {
final selectedColor = CupertinoTheme.of(context).primaryColor;

return CupertinoActionSheet(
actions: _speeds
.map(
(e) => CupertinoActionSheetAction(
onPressed: () {
Navigator.of(context).pop(e);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
e == _selected
? Icon(Icons.check, size: 20.0, color: selectedColor)
: Container(),
Text(e.toString()),
],
),
),
)
.toList(),
);
}
}
94 changes: 94 additions & 0 deletions lib/src/material_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class _MaterialControlsState extends State<MaterialControls> {
? Expanded(child: const Text('LIVE'))
: _buildPosition(iconColor),
chewieController.isLive ? const SizedBox() : _buildProgressBar(),
chewieController.allowPlaybackSpeedChanging
? _buildSpeedButton(controller)
: Container(),
chewieController.allowMuting
? _buildMuteButton(controller)
: Container(),
Expand Down Expand Up @@ -206,6 +209,50 @@ class _MaterialControlsState extends State<MaterialControls> {
);
}

Widget _buildSpeedButton(
VideoPlayerController controller,
) {
return GestureDetector(
onTap: () async {
_hideTimer?.cancel();

final chosenSpeed = await showModalBottomSheet<double>(
context: context,
isScrollControlled: true,
useRootNavigator: true,
builder: (context) => _PlaybackSpeedDialog(
speeds: chewieController.playbackSpeeds,
selected: _latestValue.playbackSpeed,
),
);

if (chosenSpeed != null) {
controller.setPlaybackSpeed(chosenSpeed);
}

if (_latestValue.isPlaying) {
_startHideTimer();
}
},
child: AnimatedOpacity(
opacity: _hideStuff ? 0.0 : 1.0,
duration: Duration(milliseconds: 300),
child: ClipRect(
child: Container(
child: Container(
height: barHeight,
padding: EdgeInsets.only(
left: 8.0,
right: 8.0,
),
child: Icon(Icons.speed),
),
),
),
),
);
}

GestureDetector _buildMuteButton(
VideoPlayerController controller,
) {
Expand Down Expand Up @@ -397,3 +444,50 @@ class _MaterialControlsState extends State<MaterialControls> {
);
}
}

class _PlaybackSpeedDialog extends StatelessWidget {
Ahmadre marked this conversation as resolved.
Show resolved Hide resolved
const _PlaybackSpeedDialog({
Key key,
@required List<double> speeds,
@required double selected,
}) : _speeds = speeds,
_selected = selected,
super(key: key);

final List<double> _speeds;
final double _selected;

@override
Widget build(BuildContext context) {
final Color selectedColor = Theme.of(context).primaryColor;

return ListView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
itemBuilder: (context, index) {
final _speed = _speeds[index];
return ListTile(
dense: true,
title: Row(
children: [
_speed == _selected
? Icon(
Icons.check,
size: 20.0,
color: selectedColor,
)
: Container(width: 20.0),
SizedBox(width: 16.0),
Text(_speed.toString()),
],
),
selected: _speed == _selected,
onTap: () {
Navigator.of(context).pop(_speed);
},
);
},
itemCount: _speeds.length,
);
}
}