Skip to content

Commit ddc24b5

Browse files
authored
fix: Fixed the background playback of video issue. (#81)
* fix: Fixed the background playback of video issue. * fix: Edge case fix when the video was originally paused and then we go out and come back into the view
1 parent 582235c commit ddc24b5

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

lib/screens/vlc_stream.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VlcStream extends StatefulWidget {
1414
_VlcStreamState createState() => _VlcStreamState();
1515
}
1616

17-
class _VlcStreamState extends State<VlcStream> {
17+
class _VlcStreamState extends State<VlcStream> with WidgetsBindingObserver {
1818
// state of media player while playing
1919
bool isPlaying = true;
2020

@@ -27,6 +27,8 @@ class _VlcStreamState extends State<VlcStream> {
2727
// hide control buttons {play/pause and seek slider} while playing media
2828
bool showControls = true;
2929

30+
bool isPausedDueToLifecycle = false;
31+
3032
_initVlcPlayer() async {
3133
_videoViewController = new VlcPlayerController(onInit: () {
3234
_videoViewController.play();
@@ -62,6 +64,7 @@ class _VlcStreamState extends State<VlcStream> {
6264
if (state == PlayingState.PLAYING) {
6365
_videoViewController.pause();
6466
setState(() {
67+
isPausedDueToLifecycle = false;
6568
isPlaying = false;
6669
});
6770
} else {
@@ -72,9 +75,38 @@ class _VlcStreamState extends State<VlcStream> {
7275
}
7376
}
7477

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+
75106
@override
76107
void initState() {
77108
super.initState();
109+
WidgetsBinding.instance.addObserver(this);
78110
_initVlcPlayer();
79111
SystemChrome.setEnabledSystemUIOverlays([]);
80112
Wakelock.enable();
@@ -196,6 +228,7 @@ class _VlcStreamState extends State<VlcStream> {
196228

197229
@override
198230
void dispose() {
231+
WidgetsBinding.instance.removeObserver(this);
199232
Wakelock.disable();
200233
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
201234
super.dispose();

0 commit comments

Comments
 (0)