Skip to content
Merged
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
35 changes: 35 additions & 0 deletions packages/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
bool isDisposed = false;
Completer<Null> _creatingCompleter;
StreamSubscription<Map<String, dynamic>> _eventSubscription;
_VideoAppLifeCycleObserver _lifeCycleObserver;

VideoPlayerController(this.uri) : super(new VideoPlayerValue(duration: null));

Future<Null> initialize() async {
_lifeCycleObserver = new _VideoAppLifeCycleObserver(this);
_lifeCycleObserver.initialize();
_creatingCompleter = new Completer<Null>();
final Map<String, dynamic> response = await _channel.invokeMethod(
'create',
Expand Down Expand Up @@ -159,6 +162,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
}
}
isDisposed = true;
_lifeCycleObserver.dispose();
super.dispose();
}

Expand Down Expand Up @@ -261,6 +265,37 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
}
}

class _VideoAppLifeCycleObserver extends WidgetsBindingObserver {
bool _wasPlayingBeforePause = false;
final VideoPlayerController _controller;

_VideoAppLifeCycleObserver(this._controller);

void initialize() {
WidgetsBinding.instance.addObserver(this);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.paused:
_wasPlayingBeforePause = _controller.value.isPlaying;
_controller.pause();
break;
case AppLifecycleState.resumed:
if (_wasPlayingBeforePause) {
_controller.play();
}
break;
default:
}
}

void dispose() {
WidgetsBinding.instance.removeObserver(this);
}
}

/// Displays the video controlled by [controller].
class VideoPlayer extends StatelessWidget {
final VideoPlayerController controller;
Expand Down