Skip to content

Commit 3da05b6

Browse files
committed
added allowBackgroundPlayback
1 parent fa216bf commit 3da05b6

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

flutter_vlc_player/lib/src/vlc_player_controller.dart

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
2828
/// The name of the asset is given by the [dataSource] argument and must not be
2929
/// null. The [package] argument must be non-null when the asset comes from a
3030
/// package and null otherwise.
31-
VlcPlayerController.asset(
32-
this.dataSource, {
31+
VlcPlayerController.asset(this.dataSource, {
3332
this.autoInitialize = true,
33+
this.allowBackgroundPlayback = false,
3434
this.package,
3535
this.hwAcc = HwAcc.auto,
3636
this.autoPlay = true,
3737
this.options,
38-
@Deprecated('Please, use the addOnInitListener method instead.')
39-
VoidCallback? onInit,
40-
@Deprecated('Please, use the addOnRendererEventListener method instead.')
41-
RendererCallback? onRendererHandler,
42-
}) : _dataSourceType = DataSourceType.asset,
38+
@Deprecated(
39+
'Please, use the addOnInitListener method instead.') VoidCallback? onInit,
40+
@Deprecated(
41+
'Please, use the addOnRendererEventListener method instead.') RendererCallback? onRendererHandler,
42+
})
43+
: _dataSourceType = DataSourceType.asset,
4344
_onInit = onInit,
4445
_onRendererHandler = onRendererHandler,
4546
super(VlcPlayerValue(duration: Duration.zero));
@@ -49,17 +50,18 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
4950
///
5051
/// The URI for the video is given by the [dataSource] argument and must not be
5152
/// null.
52-
VlcPlayerController.network(
53-
this.dataSource, {
53+
VlcPlayerController.network(this.dataSource, {
5454
this.autoInitialize = true,
55+
this.allowBackgroundPlayback = false,
5556
this.hwAcc = HwAcc.auto,
5657
this.autoPlay = true,
5758
this.options,
58-
@Deprecated('Please, use the addOnInitListener method instead.')
59-
VoidCallback? onInit,
60-
@Deprecated('Please, use the addOnRendererEventListener method instead.')
61-
RendererCallback? onRendererHandler,
62-
}) : package = null,
59+
@Deprecated(
60+
'Please, use the addOnInitListener method instead.') VoidCallback? onInit,
61+
@Deprecated(
62+
'Please, use the addOnRendererEventListener method instead.') RendererCallback? onRendererHandler,
63+
})
64+
: package = null,
6365
_dataSourceType = DataSourceType.network,
6466
_onInit = onInit,
6567
_onRendererHandler = onRendererHandler,
@@ -69,17 +71,18 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
6971
///
7072
/// This will load the file from the file-URI given by:
7173
/// `'file://${file.path}'`.
72-
VlcPlayerController.file(
73-
File file, {
74+
VlcPlayerController.file(File file, {
7475
this.autoInitialize = true,
76+
this.allowBackgroundPlayback = false,
7577
this.hwAcc = HwAcc.auto,
7678
this.autoPlay = true,
7779
this.options,
78-
@Deprecated('Please, use the addOnInitListener method instead.')
79-
VoidCallback? onInit,
80-
@Deprecated('Please, use the addOnRendererEventListener method instead.')
81-
RendererCallback? onRendererHandler,
82-
}) : dataSource = 'file://${file.path}',
80+
@Deprecated(
81+
'Please, use the addOnInitListener method instead.') VoidCallback? onInit,
82+
@Deprecated(
83+
'Please, use the addOnRendererEventListener method instead.') RendererCallback? onRendererHandler,
84+
})
85+
: dataSource = 'file://${file.path}',
8386
package = null,
8487
_dataSourceType = DataSourceType.file,
8588
_onInit = onInit,
@@ -103,6 +106,10 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
103106
/// Initialize vlc player when the platform is ready automatically
104107
final bool autoInitialize;
105108

109+
/// Set keep playing video in background, when app goes in background.
110+
/// The default value is false.
111+
final bool allowBackgroundPlayback;
112+
106113
/// This is a callback that will be executed once the platform view has been initialized.
107114
/// If you want the media to play as soon as the platform view has initialized, you could just call
108115
/// [VlcPlayerController.play] in this callback. (see the example).
@@ -176,8 +183,10 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
176183
throw Exception('Already Initialized');
177184
}
178185

179-
_lifeCycleObserver = VlcAppLifeCycleObserver(this);
180-
_lifeCycleObserver!.initialize();
186+
if (!allowBackgroundPlayback) {
187+
_lifeCycleObserver = VlcAppLifeCycleObserver(this);
188+
}
189+
_lifeCycleObserver?.initialize();
181190

182191
await vlcPlayerPlatform.create(
183192
viewId: _viewId,
@@ -375,11 +384,9 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
375384
}
376385

377386
/// Notify onRendererHandler callback & all registered listeners
378-
void _notifyOnRendererListeners(
379-
VlcRendererEventType type,
380-
String? id,
381-
String? name,
382-
) {
387+
void _notifyOnRendererListeners(VlcRendererEventType type,
388+
String? id,
389+
String? name,) {
383390
if (_onRendererHandler != null) {
384391
_onRendererHandler!(type, id!, name!);
385392
}
@@ -392,8 +399,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
392399
/// its state before the method was called. (i.e. if this method is called whilst media is playing, once the new
393400
/// data source has been loaded, the new stream will begin playing.)
394401
/// [dataSource] - the path of the asset file.
395-
Future<void> setMediaFromAsset(
396-
String dataSource, {
402+
Future<void> setMediaFromAsset(String dataSource, {
397403
String? package,
398404
bool? autoPlay,
399405
HwAcc? hwAcc,
@@ -413,8 +419,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
413419
/// its state before the method was called. (i.e. if this method is called whilst media is playing, once the new
414420
/// data source has been loaded, the new stream will begin playing.)
415421
/// [dataSource] - the URL of the stream to start playing.
416-
Future<void> setMediaFromNetwork(
417-
String dataSource, {
422+
Future<void> setMediaFromNetwork(String dataSource, {
418423
bool? autoPlay,
419424
HwAcc? hwAcc,
420425
}) async {
@@ -433,8 +438,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
433438
/// its state before the method was called. (i.e. if this method is called whilst media is playing, once the new
434439
/// data source has been loaded, the new stream will begin playing.)
435440
/// [file] - the File stream to start playing.
436-
Future<void> setMediaFromFile(
437-
File file, {
441+
Future<void> setMediaFromFile(File file, {
438442
bool? autoPlay,
439443
HwAcc? hwAcc,
440444
}) async {
@@ -455,8 +459,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
455459
/// data source has been loaded, the new stream will begin playing.)
456460
/// [dataSource] - the URL of the stream to start playing.
457461
/// [dataSourceType] - the source type of media.
458-
Future<void> _setStreamUrl(
459-
String dataSource, {
462+
Future<void> _setStreamUrl(String dataSource, {
460463
required DataSourceType dataSourceType,
461464
String? package,
462465
bool? autoPlay,
@@ -669,8 +672,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
669672
/// Add extra network subtitle to media.
670673
/// [dataSource] - Url of subtitle
671674
/// [isSelected] - Set true if you wanna force the added subtitle to start display on media.
672-
Future<void> addSubtitleFromNetwork(
673-
String dataSource, {
675+
Future<void> addSubtitleFromNetwork(String dataSource, {
674676
bool? isSelected,
675677
}) async {
676678
return await _addSubtitleTrack(
@@ -683,8 +685,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
683685
/// Add extra subtitle file to media.
684686
/// [file] - Subtitle file
685687
/// [isSelected] - Set true if you wanna force the added subtitle to start display on media.
686-
Future<void> addSubtitleFromFile(
687-
File file, {
688+
Future<void> addSubtitleFromFile(File file, {
688689
bool? isSelected,
689690
}) async {
690691
return await _addSubtitleTrack(
@@ -697,8 +698,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
697698
/// Add extra subtitle to media.
698699
/// [uri] - URI of subtitle
699700
/// [isSelected] - Set true if you wanna force the added subtitle to start display on media.
700-
Future<void> _addSubtitleTrack(
701-
String uri, {
701+
Future<void> _addSubtitleTrack(String uri, {
702702
required DataSourceType dataSourceType,
703703
bool? isSelected,
704704
}) async {
@@ -761,8 +761,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
761761
/// Add extra network audio to media.
762762
/// [dataSource] - Url of audio
763763
/// [isSelected] - Set true if you wanna force the added audio to start playing on media.
764-
Future<void> addAudioFromNetwork(
765-
String dataSource, {
764+
Future<void> addAudioFromNetwork(String dataSource, {
766765
bool? isSelected,
767766
}) async {
768767
return await _addAudioTrack(
@@ -775,8 +774,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
775774
/// Add extra audio file to media.
776775
/// [file] - Audio file
777776
/// [isSelected] - Set true if you wanna force the added audio to start playing on media.
778-
Future<void> addAudioFromFile(
779-
File file, {
777+
Future<void> addAudioFromFile(File file, {
780778
bool? isSelected,
781779
}) async {
782780
return await _addAudioTrack(
@@ -789,8 +787,7 @@ class VlcPlayerController extends ValueNotifier<VlcPlayerValue> {
789787
/// Add extra audio to media.
790788
/// [uri] - URI of audio
791789
/// [isSelected] - Set true if you wanna force the added audio to start playing on media.
792-
Future<void> _addAudioTrack(
793-
String uri, {
790+
Future<void> _addAudioTrack(String uri, {
794791
required DataSourceType dataSourceType,
795792
bool? isSelected,
796793
}) async {

0 commit comments

Comments
 (0)