Explorar o código

Added options for live and screen sleep (#67)

* screen sleep and live options added

* minor fix for LIVE label

* cupertino controls updated with new options
Roman Matroskin %!s(int64=7) %!d(string=hai) anos
pai
achega
b135a46662

+ 26 - 3
lib/src/chewie_player.dart

@@ -5,6 +5,7 @@ import 'package:chewie/src/player_with_controls.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter/widgets.dart';
+import 'package:screen/screen.dart';
 import 'package:video_player/video_player.dart';
 
 /// A Video Player with Material and Cupertino skins.
@@ -51,6 +52,12 @@ class Chewie extends StatefulWidget {
   /// Defines if the player will start in fullscreen when play is pressed
   final bool fullScreenByDefault;
 
+  /// Defines if the player will sleep in fullscreen or not
+  final bool allowedScreenSleep;
+
+  /// Defines if the controls should be for live stream video
+  final bool isLive;
+
   Chewie(
     this.controller, {
     Key key,
@@ -64,7 +71,10 @@ class Chewie extends StatefulWidget {
     this.materialProgressColors,
     this.placeholder,
     this.showControls = true,
-  })  : assert(controller != null, 'You must provide a controller to play a video'),
+    this.allowedScreenSleep = true,
+    this.isLive = false,
+  })  : assert(controller != null,
+            'You must provide a controller to play a video'),
         super(key: key);
 
   @override
@@ -88,6 +98,7 @@ class _ChewiePlayerState extends State<Chewie> {
       placeholder: widget.placeholder,
       autoPlay: widget.autoPlay,
       showControls: widget.showControls,
+      isLive: widget.isLive,
     );
   }
 
@@ -98,7 +109,8 @@ class _ChewiePlayerState extends State<Chewie> {
     _initialize();
   }
 
-  Widget _buildFullScreenVideo(BuildContext context, Animation<double> animation) {
+  Widget _buildFullScreenVideo(
+      BuildContext context, Animation<double> animation) {
     return new Scaffold(
       resizeToAvoidBottomPadding: false,
       body: new Container(
@@ -106,9 +118,11 @@ class _ChewiePlayerState extends State<Chewie> {
         color: Colors.black,
         child: new PlayerWithControls(
           controller: _controller,
-          onExpandCollapse: () => new Future<dynamic>.value(Navigator.of(context).pop()),
+          onExpandCollapse: () =>
+              new Future<dynamic>.value(Navigator.of(context).pop()),
           aspectRatio: widget.aspectRatio ?? _calculateAspectRatio(context),
           fullScreen: true,
+          isLive: widget.isLive,
           cupertinoProgressColors: widget.cupertinoProgressColors,
           materialProgressColors: widget.materialProgressColors,
         ),
@@ -186,8 +200,17 @@ class _ChewiePlayerState extends State<Chewie> {
       ]);
     }
 
+    if (!widget.allowedScreenSleep) {
+      Screen.keepOn(true);
+    }
+
     await Navigator.of(context).push(route);
 
+    bool isKeptOn = await Screen.isKeptOn;
+    if (isKeptOn) {
+      Screen.keepOn(false);
+    }
+
     SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
     SystemChrome.setPreferredOrientations([
       DeviceOrientation.portraitUp,

+ 30 - 10
lib/src/cupertino_controls.dart

@@ -18,6 +18,7 @@ class CupertinoControls extends StatefulWidget {
   final bool fullScreen;
   final ChewieProgressColors progressColors;
   final bool autoPlay;
+  final bool isLive;
 
   CupertinoControls({
     @required this.backgroundColor,
@@ -27,6 +28,7 @@ class CupertinoControls extends StatefulWidget {
     @required this.fullScreen,
     @required this.progressColors,
     @required this.autoPlay,
+    @required this.isLive,
   });
 
   @override
@@ -120,16 +122,24 @@ class _CupertinoControlsState extends State<CupertinoControls> {
                   new Radius.circular(10.0),
                 ),
               ),
-              child: new Row(
-                children: <Widget>[
-                  _buildSkipBack(iconColor, barHeight),
-                  _buildPlayPause(controller, iconColor, barHeight),
-                  _buildSkipForward(iconColor, barHeight),
-                  _buildPosition(iconColor),
-                  _buildProgressBar(),
-                  _buildRemaining(iconColor)
-                ],
-              ),
+              child: widget.isLive
+                  ? Row(
+                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
+                      children: <Widget>[
+                        _buildPlayPause(controller, iconColor, barHeight),
+                        _buildLive(iconColor),
+                      ],
+                    )
+                  : new Row(
+                      children: <Widget>[
+                        _buildSkipBack(iconColor, barHeight),
+                        _buildPlayPause(controller, iconColor, barHeight),
+                        _buildSkipForward(iconColor, barHeight),
+                        _buildPosition(iconColor),
+                        _buildProgressBar(),
+                        _buildRemaining(iconColor)
+                      ],
+                    ),
             ),
           ),
         ),
@@ -137,6 +147,16 @@ class _CupertinoControlsState extends State<CupertinoControls> {
     );
   }
 
+  Widget _buildLive(Color iconColor) {
+    return new Padding(
+      padding: new EdgeInsets.only(right: 12.0),
+      child: new Text(
+        'LIVE',
+        style: new TextStyle(color: iconColor, fontSize: 12.0),
+      ),
+    );
+  }
+
   GestureDetector _buildExpandButton(
     Color backgroundColor,
     Color iconColor,

+ 6 - 2
lib/src/material_controls.dart

@@ -13,6 +13,7 @@ class MaterialControls extends StatefulWidget {
   final Future<dynamic> Function() onExpandCollapse;
   final ChewieProgressColors progressColors;
   final bool autoPlay;
+  final bool isLive;
 
   MaterialControls({
     @required this.controller,
@@ -20,6 +21,7 @@ class MaterialControls extends StatefulWidget {
     @required this.onExpandCollapse,
     @required this.progressColors,
     @required this.autoPlay,
+    @required this.isLive,
   });
 
   @override
@@ -103,8 +105,10 @@ class _MaterialControlsState extends State<MaterialControls> {
         child: new Row(
           children: <Widget>[
             _buildPlayPause(controller),
-            _buildPosition(iconColor),
-            _buildProgressBar(),
+            widget.isLive
+                ? Expanded(child: const Text('LIVE'))
+                : _buildPosition(iconColor),
+            widget.isLive ? const SizedBox() : _buildProgressBar(),
             _buildMuteButton(controller),
             _buildExpandButton(),
           ],

+ 10 - 6
lib/src/player_with_controls.dart

@@ -18,6 +18,7 @@ class PlayerWithControls extends StatefulWidget {
   final double aspectRatio;
   final bool autoPlay;
   final bool showControls;
+  final bool isLive;
 
   PlayerWithControls({
     Key key,
@@ -30,6 +31,7 @@ class PlayerWithControls extends StatefulWidget {
     this.materialProgressColors,
     this.placeholder,
     this.autoPlay,
+    this.isLive = false,
   }) : super(key: key);
 
   @override
@@ -47,9 +49,9 @@ class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
       child: new Container(
         width: MediaQuery.of(context).size.width,
         child: new AspectRatio(
-                aspectRatio: widget.aspectRatio,
-                child: _buildPlayerWithControls(controller, context),
-              ),
+          aspectRatio: widget.aspectRatio,
+          child: _buildPlayerWithControls(controller, context),
+        ),
       ),
     );
   }
@@ -64,9 +66,9 @@ class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
             child: new Hero(
               tag: controller,
               child: new AspectRatio(
-                      aspectRatio: widget.aspectRatio,
-                      child: new VideoPlayer(controller),
-                    ),
+                aspectRatio: widget.aspectRatio,
+                child: new VideoPlayer(controller),
+              ),
             ),
           ),
           _buildControls(context, controller),
@@ -87,6 +89,7 @@ class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
                 fullScreen: widget.fullScreen,
                 progressColors: widget.materialProgressColors,
                 autoPlay: widget.autoPlay,
+                isLive: widget.isLive,
               )
             : new CupertinoControls(
                 backgroundColor: new Color.fromRGBO(41, 41, 41, 0.7),
@@ -96,6 +99,7 @@ class _VideoPlayerWithControlsState extends State<PlayerWithControls> {
                 fullScreen: widget.fullScreen,
                 progressColors: widget.cupertinoProgressColors,
                 autoPlay: widget.autoPlay,
+                isLive: widget.isLive,
               )
         : new Container();
   }

+ 1 - 0
pubspec.yaml

@@ -10,6 +10,7 @@ environment:
 dependencies:
   open_iconic_flutter: ">=0.3.0 <0.4.0"
   video_player: ">=0.7.0 <0.8.0"
+  screen: ">=0.0.4 <0.1.0"
   flutter:
     sdk: flutter