Skip to content
Open
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
9 changes: 8 additions & 1 deletion lib/src/material/material_desktop_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MaterialDesktopControls extends StatefulWidget {
class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
with SingleTickerProviderStateMixin {
late PlayerNotifier notifier;
late VideoPlayerValue _latestValue;
late VideoPlayerValue _latestValue = controller.value;
double? _latestVolume;
Timer? _hideTimer;
Timer? _initTimer;
Expand Down Expand Up @@ -568,6 +568,13 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
_displayBufferingIndicator = buffering;
}

// Play/pause can also come from outside these controls (hardware media
// keys handled by the browser, MediaSession): reveal the controls so the
// state change is visible.
if (_latestValue.isPlaying != controller.value.isPlaying) {
_cancelAndRestartTimer();
}

setState(() {
_latestValue = controller.value;
_subtitlesPosition = controller.value.position;
Expand Down
100 changes: 100 additions & 0 deletions test/external_playpause_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'package:chewie/chewie.dart';
import 'package:chewie/src/center_play_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:video_player/video_player.dart';

const _src =
'https://assets.mixkit.co/videos/preview/mixkit-spinning-around-the-earth-29351-large.mp4';

/// Lets tests flip the playing state the way an external actor would
/// (hardware media keys handled by the browser, MediaSession): the value
/// changes without going through the controls' own play/pause path.
class _FakeVideoPlayerController extends VideoPlayerController {
_FakeVideoPlayerController() : super.networkUrl(Uri.parse(_src));

void setPlaying(bool playing) {
value = value.copyWith(isPlaying: playing);
}

void setPosition(Duration position) {
value = value.copyWith(position: position);
}
}

ChewieController _controller(_FakeVideoPlayerController videoController) {
return ChewieController(
videoPlayerController: videoController,
autoPlay: false,
looping: false,
showControlsOnInitialize: false,
customControls: const MaterialDesktopControls(),
);
}

Future<void> _pumpPlayer(
WidgetTester tester,
ChewieController controller,
) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(body: Chewie(controller: controller)),
),
);
await tester.pump();
}

bool _controlsVisible(WidgetTester tester) =>
tester.widget<CenterPlayButton>(find.byType(CenterPlayButton)).show;

void main() {
testWidgets('reveals the controls and auto-hides on an external play', (
tester,
) async {
final videoController = _FakeVideoPlayerController();
await _pumpPlayer(tester, _controller(videoController));
expect(_controlsVisible(tester), isFalse);

videoController.setPlaying(true);
await tester.pump();
expect(_controlsVisible(tester), isTrue);

await tester.pump(const Duration(seconds: 4));
expect(_controlsVisible(tester), isFalse);
});

testWidgets('reveals the controls and auto-hides on an external pause', (
tester,
) async {
final videoController = _FakeVideoPlayerController();
await _pumpPlayer(tester, _controller(videoController));

videoController.setPlaying(true);
await tester.pump();
await tester.pump(const Duration(seconds: 4));
expect(_controlsVisible(tester), isFalse);

videoController.setPlaying(false);
await tester.pump();
expect(_controlsVisible(tester), isTrue);

await tester.pump(const Duration(seconds: 4));
expect(_controlsVisible(tester), isFalse);
});

testWidgets('leaves hidden controls hidden when only the position changes', (
tester,
) async {
final videoController = _FakeVideoPlayerController();
await _pumpPlayer(tester, _controller(videoController));

videoController.setPlaying(true);
await tester.pump();
await tester.pump(const Duration(seconds: 4));
expect(_controlsVisible(tester), isFalse);

videoController.setPosition(const Duration(seconds: 42));
await tester.pump();
expect(_controlsVisible(tester), isFalse);
});
}