Flutter - video_player fullscreen

前端 未结 3 704
说谎
说谎 2021-02-13 21:28

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

3条回答
  •  粉色の甜心
    2021-02-13 22:03

    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,
              ),
            ),
          ),
        );
      }
    

提交回复
热议问题