@@ -14,7 +14,7 @@ class VlcStream extends StatefulWidget {
14
14
_VlcStreamState createState () => _VlcStreamState ();
15
15
}
16
16
17
- class _VlcStreamState extends State <VlcStream > {
17
+ class _VlcStreamState extends State <VlcStream > with WidgetsBindingObserver {
18
18
// state of media player while playing
19
19
bool isPlaying = true ;
20
20
@@ -27,6 +27,8 @@ class _VlcStreamState extends State<VlcStream> {
27
27
// hide control buttons {play/pause and seek slider} while playing media
28
28
bool showControls = true ;
29
29
30
+ bool isPausedDueToLifecycle = false ;
31
+
30
32
_initVlcPlayer () async {
31
33
_videoViewController = new VlcPlayerController (onInit: () {
32
34
_videoViewController.play ();
@@ -62,6 +64,7 @@ class _VlcStreamState extends State<VlcStream> {
62
64
if (state == PlayingState .PLAYING ) {
63
65
_videoViewController.pause ();
64
66
setState (() {
67
+ isPausedDueToLifecycle = false ;
65
68
isPlaying = false ;
66
69
});
67
70
} else {
@@ -72,9 +75,38 @@ class _VlcStreamState extends State<VlcStream> {
72
75
}
73
76
}
74
77
78
+ @override
79
+ void didChangeAppLifecycleState (AppLifecycleState state) {
80
+ if (state == AppLifecycleState .detached ||
81
+ state == AppLifecycleState .inactive ||
82
+ state == AppLifecycleState .paused) {
83
+ //The app is either in bg or the phone has been turned off
84
+ PlayingState state = _videoViewController.playingState;
85
+ if (state == PlayingState .PLAYING ) {
86
+ //Check if the video is playing and only then execute pause operation
87
+ _videoViewController.pause ();
88
+ setState (() {
89
+ //Keeping track if the video is paused due to lifecycle change
90
+ isPausedDueToLifecycle = true ;
91
+ isPlaying = false ;
92
+ });
93
+ }
94
+ }
95
+ //Only is the video was paused due to lifecycle changes
96
+ if (state == AppLifecycleState .resumed && isPausedDueToLifecycle) {
97
+ //The app is bought back into the view or the display is turned back on
98
+ _videoViewController.play ();
99
+ setState (() {
100
+ isPlaying = true ;
101
+ });
102
+ }
103
+ super .didChangeAppLifecycleState (state);
104
+ }
105
+
75
106
@override
76
107
void initState () {
77
108
super .initState ();
109
+ WidgetsBinding .instance.addObserver (this );
78
110
_initVlcPlayer ();
79
111
SystemChrome .setEnabledSystemUIOverlays ([]);
80
112
Wakelock .enable ();
@@ -196,6 +228,7 @@ class _VlcStreamState extends State<VlcStream> {
196
228
197
229
@override
198
230
void dispose () {
231
+ WidgetsBinding .instance.removeObserver (this );
199
232
Wakelock .disable ();
200
233
SystemChrome .setEnabledSystemUIOverlays (SystemUiOverlay .values);
201
234
super .dispose ();
0 commit comments