22

I'm using a plugin called video_player on my Flutter project. I'm able to play and pause videos without a problem, but I want to make it fullscreen and horizontal. I couldn't find anything related to this.

This is the basic code I'm using:

playerController = VideoPlayerController.network(
          "<VIDEO_URL>")
        ..addListener(listener)
        ..setVolume(1.0)
        ..initialize()
        ..play();

Can I make it fullscreen?

Notheros
  • 2,137
  • 9
  • 23
  • 34

5 Answers5

26

As far as I understand, the VideoPlayer doesn't know anything about where it is, but rather just expands to fit within the given space the best it can.

I believe what you want to do is use a RotatedBox as a parent of the video and set the rotation value. Depending on how exactly your app works, you may want to have the video player start horizontal and small, and have a full screen button that switches to landscape mode. However, if the app itself is set up to rotate you'll have to take that into account and un-rotate the video once the phone has been rotated horizontally, which will probably result in uglyness in the UI as the flutter rotation happens and you un-rotate the video.

You probably also want to use a dialog to show the video full-screen so that you can dismiss it and get back to the screen you were at.

I could probably provide a bit more guidance but it does depend on which way you go with that (either locking the app to portrait mode and doing the rotation manually) vs using flutter's built-in rotation.

rmtmckenzie
  • 37,718
  • 9
  • 112
  • 99
16

I have another situation for this question, that's use Chewie plugin, you can install it right here: https://pub.dev/packages/chewie And this is the code that I implemented it:

VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;
  double _aspectRatio = 16 / 9;
  @override
  initState() {
    super.initState();
    print(widget.videoUrl);
    _videoPlayerController = VideoPlayerController.network(widget.videoUrl);
    _chewieController = ChewieController(
      allowedScreenSleep: false,
      allowFullScreen: true,
      deviceOrientationsAfterFullScreen: [
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ],
      videoPlayerController: _videoPlayerController,
      aspectRatio: _aspectRatio,
      autoInitialize: true,
      autoPlay: true,
      showControls: true,
    );
    _chewieController.addListener(() {
      if (_chewieController.isFullScreen) {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.landscapeRight,
          DeviceOrientation.landscapeLeft,
        ]);
      } else {
         SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ]);
      }
    });
  }

Remember restore orientation of the device after exit page:

@override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    super.dispose();
  }


    @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          child: Chewie(
            controller: _chewieController,
          ),
        ),
      ),
    );
  }
Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
Nhật Trần
  • 2,522
  • 1
  • 20
  • 20
  • I tried this solution Its not working for me in iOS – Shangari C Jan 23 '20 at 12:24
  • This was the perfect solution for me, as going into fullscreen mode was making my Android app allow the app to be rotated after coming out of fullscreen mode, even though this was set to Portrait only in the Manifest file. Following this solution fixed this problem for me. Thanks – Munes Jan 15 '21 at 01:41
  • If you have any problem, just ask me! – Nhật Trần Jan 15 '21 at 02:49
  • Glad, that the deviceOrientation before and after FullScreen helped you guys :D I will improve everything in chewie! It will be much better! – Rebar Jun 10 '21 at 23:13
  • This doesn't seem to be working anymore, at least in Chewie 1.2.2 when the device is rotated to landscape, full screen mode is not activated. – schneida Oct 16 '21 at 17:30
2

Even tho this is a late answer figured it might help someone,

I have found flick_video_player to be one of the best video players out there.

You can simply enter to full screen using the predefined method, using the controller.

 _activeManager!.flickControlManager!.enterFullscreen();

and to exit from the fullscreen player

 _activeManager!.flickControlManager!.exitFullscreen();

You can get examples here

flick video player examples

Abel Tilahun
  • 1,705
  • 1
  • 16
  • 25
0

I have created a function which will play video in fullscreen

import 'package:flutter/services.dart';
.
.

void pushFullScreenVideo() {

//This will help to hide the status bar and bottom bar of Mobile 
//also helps you to set preferred device orientations like landscape

SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations(
  [
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ],
);

//This will help you to push fullscreen view of video player on top of current page

Navigator.of(navigatorKey.currentContext)
    .push(
  PageRouteBuilder(
    opaque: false,
    settings: RouteSettings(),
    pageBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
    ) {
      return Scaffold(
        backgroundColor: Colors.transparent,
        resizeToAvoidBottomPadding: false,
        body: Dismissible(
          key: const Key('key'),
          direction: DismissDirection.vertical,
          onDismissed: (_) => Navigator.of(context).pop(),
          child: OrientationBuilder(
          builder: (context, orientation) {
            isPortrait = orientation == Orientation.portrait;
            return Center(
              child: Stack(
              //This will help to expand video in Horizontal mode till last pixel of screen
                fit: isPortrait ? StackFit.loose : StackFit.expand,
                  children: [
                     AspectRatio(
                       aspectRatio: controller.value.aspectRatio,
                       child: VideoPlayer(controller),
                     ),
                  ],
               ),
            );
         },
       ); 
    },
  ),
)
    .then(
  (value) {

//This will help you to set previous Device orientations of screen so App will continue for portrait mode

    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    SystemChrome.setPreferredOrientations(
      [
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ],
    );
  },
);
}
-2

In order for it to work in iOS you should add the following change in ios/Runner/Info.plist:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
Dharman
  • 30,962
  • 25
  • 85
  • 135