flutter video_player视频播放器的横竖屏切换

2023-11-10

参考

需要使用到的库:

import 'package:flutter/services.dart';
用到的方法:

//实现全屏功能代码,一般写在按钮或者初始化函数里
SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeLeft, //全屏时旋转方向,左边
]);

//还原为竖屏功能代码,一般写在 dispose()方法中,页面返回时执行
 SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
]);
示例代码(部份):

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

//跳转后的页面
class NewPlays extends StatefulWidget {
  //参数传递定义
  final VideoPlayerController controller;
  const NewPlays({Key key, this.controller}) : super(key: key);
  @override
  MyNewPlays createState() => MyNewPlays();
}

class MyNewPlays extends State<NewPlays> {

//初始化时直接向左边旋转全屏
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft, //全屏时旋转方向,左边
    ]);
  }
  
   /*
             中间部份省略
   */

//页面销毁时执行还原操作
  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }
}

我自己的

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';

import '../../router.dart';

//横屏界面
class MovieBigLandScape extends StatefulWidget {
  //参数传递定义
  final VideoPlayerController videoController;

  const MovieBigLandScape({Key? key, required this.videoController})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => MovieBigLandScapeState();
}

class MovieBigLandScapeState extends State<MovieBigLandScape> {
  bool isPress = false;
  late Timer? isPressTimer;

  //获取当前视频播放的信息
  late VideoPlayerValue videoPlayerValue;

  //当前播放视频的总时长
  late Duration totalDuration;

  //当前播放视频的位置
  late Duration currentDuration;

  String tDuration = "0:00:00";
  String cDuration = "0:00:00";

  //当前视频是否缓存
  bool isBuffer = false;

  @override
  void initState() {
    isPressTimer = Timer(const Duration(), () {});
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft, //全屏时旋转方向,左边
    ]);
    widget.videoController.addListener(() {
      mySetState(() {
        videoPlayerValue = widget.videoController.value;
        totalDuration = videoPlayerValue.duration;
        currentDuration = videoPlayerValue.position;
        tDuration = totalDuration.toString().substring(0, 7);
        cDuration = currentDuration.toString().substring(0, 7);
        isBuffer = videoPlayerValue.isBuffering;
      });
    });
    super.initState();
  }

  mySetState(callBack) {
    if (mounted) {
      setState(() {
        callBack();
      });
    }
  }

  @override
  void dispose() {
    if (isPressTimer != null) {
      isPressTimer?.cancel();
      isPressTimer = null;
    }
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Stack(
        alignment: Alignment.center,
        children: [
          Stack(
            alignment: Alignment.bottomCenter,
            children: [
              Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        isPress = !isPress;
                      });
                      if (isPressTimer != null) {
                        isPressTimer?.cancel();
                        isPressTimer = null;
                      }
                      isPressTimer =
                          Timer(const Duration(milliseconds: 5000), () {
                        setState(() {
                          isPress = false;
                        });
                      });
                    },
                    child: Container(
                        color: Colors.black,
                        alignment: Alignment.topCenter,
                        child: AspectRatio(
                            aspectRatio:
                                widget.videoController.value.aspectRatio,
                            child: VideoPlayer(widget.videoController))),
                  ),
                  if (isPress)
                    Row(
                      children: [
                        GestureDetector(
                          onDoubleTap: () {
                            widget.videoController.value.isPlaying
                                ? widget.videoController.pause()
                                : widget.videoController.play();
                          },
                          onTap: () {
                            widget.videoController.value.isPlaying
                                ? widget.videoController.pause()
                                : widget.videoController.play();
                          },
                          child: Container(
                            padding: const EdgeInsets.only(bottom: 5),
                            child: Icon(
                              widget.videoController.value.isPlaying
                                  ? Icons.pause
                                  : Icons.play_arrow,
                              color: Colors.white,
                              size: 30,
                            ),
                          ),
                        ),
                        Expanded(
                            child: Container(
                          padding: const EdgeInsets.only(bottom: 5),
                          child: VideoProgressIndicator(
                            widget.videoController,
                            allowScrubbing: true,
                            colors: const VideoProgressColors(
                                backgroundColor: Colors.white24,
                                bufferedColor: Colors.white,
                                playedColor: Colors.pinkAccent),
                          ),
                        )),
                        Container(
                          margin: const EdgeInsets.only(left: 5),
                          child: Text(
                            "$cDuration/$tDuration",
                            style: const TextStyle(
                                color: Colors.white, fontSize: 9),
                          ),
                        ),
                        GestureDetector(
                          onTap: () {
                            MyRouter.pop(context);
                          },
                          child: Container(
                            padding: const EdgeInsets.only(bottom: 5),
                            child: const Icon(
                              Icons.play_arrow,
                              color: Colors.white,
                              size: 30,
                            ),
                          ),
                        ),
                      ],
                    ),
                ],
              ),
              if (!isPress)
                VideoProgressIndicator(
                  widget.videoController,
                  allowScrubbing: true,
                  colors: const VideoProgressColors(
                      backgroundColor: Colors.white24,
                      bufferedColor: Colors.white,
                      playedColor: Colors.pinkAccent),
                )
            ],
          ),
          if (isBuffer)
            const SizedBox(
              height: 30,
              width: 30,
              child: CircularProgressIndicator(
                backgroundColor: Colors.white,
              ),
            )
        ],
      ),
    );
  }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

flutter video_player视频播放器的横竖屏切换 的相关文章

随机推荐

  • 【医学图像处理】用于肝血管分割的平均教师辅助置信学习

    标题 Noisy Labels are Treasure Mean Teacher Assisted Confident Learning for Hepatic Vessel Segmentation 作者 Donghuan Lu 腾讯天
  • django配置随时执行的脚本,使用运行时的manage环境执行程序

    有时候需要在启动django后执行一些数据的实时采集或更新操作 这篇博客记录脚本的配置与运行 安装依赖 首先安装依赖 pip install django extensions 然后在setting的INSTALLED APPS配置中添加d
  • OpenCV图像处理——轮廓检测

    目录 图像的轮廓 查找轮廓 绘制轮廓 轮廓的特征 轮廓面积 轮廓周长 轮廓近似 凸包 边界矩形 最小外接圆 椭圆拟合 直线拟合 图像的矩特征 矩的概念 图像中的矩特征 图像的轮廓 查找轮廓 binary contours hierarchy
  • 手机串口终端

    手机串口终端 做嵌入式开发的小伙伴永远离不开串口 想想一下 假设你需要进行现场调试 但是身边没有电脑 或者其他特殊环境不方便用电脑 或者就是单纯的懒得用电脑 该怎么办 这就是我碰到的场景 而且不止一次 说多了都是泪 别问 这种情况你就只能把
  • 多线程小记

    创建线程的几种方法 C 多线程之 beginthread https blog csdn net u013043408 article details 83830181 C 多线程之CreateThread https blog csdn
  • ping: www.baidu.com: Temporary failure in name resolution

    虚拟机突然访问不了百度了 查看发现网络都是好的 root zk02 ping www baidu com ping www baidu com Temporary failure in name resolution 经查修改下如下配置文件
  • 统计软件与数据分析Lesson9----爬虫解析库Beautiful Soup

    统计软件与数据分析Lesson9 爬虫解析库Beautiful Soup知识点总结 1 requests 模块 1 1 查看requests功能函数 1 2 发送请求 1 3 传递URL参数 1 4 获取响应内容 2 Beautiful S
  • Chrome NET::ERR_CERT_AUTHORITY_INVALID 错误分析及处理

    Chrome NET ERR CERT AUTHORITY INVALID 错误分析及处理 1 问题描述 今天用Apache搭建好PC端服务器后 用Chrome测试 打开http localhost 时出现了如下的错误 错误代码 NET E
  • js实现前端分页页码管理

    用JS实现前端分页页码管理 可以很美观的区分页码显示 这也是参考大多数网站的分页页码展示 能够有很好的用户体验 这也是有业务需要就写了一下 还是新手 经验不足 欢迎指出批评 首先先看效果图 这是初始页面 也就是第一页 的界面 如果为第一页时
  • Python怎么合并两个矩阵?Python矩阵合并需要使用什么方法

    python之中的第三方库numpy可以构建出来矩阵这种行列格式且对称的数据结构 而在之前的文章之中小编给大家介绍了矩阵是如何通过添加的方式来扩展行列的 而本次要来给大家分享的就是一个和它相似的内容 矩阵合并的方法以及详细代码示例 一起往下
  • Hive优化

    1 Fetch抓取 目的 让hive sql 能不走MR 尽量不走MR 通过hive的参数来配置 决定哪些sql可以不走MR set hive fetch task conversion 可选值 none minimal more 默认值
  • Java实现PDF导出/预览

    网上有很多关于PDF导出的文章 但是个人感觉实现的过于复杂 又是模板又是html的 有的还需要字体模板的支持 本片文章只是实现简单的PDF表格导出 可以实现PDF动态表格导出 预览 这类文章网上很少 就给你们整理一篇吧 实现思路 本地创建P
  • 自己写的跨数据库的表同步工具

    阅读本文大概需要 3 6 分钟 近期在做数据集市 遇到的痛点如下 1 数据采集过程繁琐 重复的脚本编写太多 从不同的数据库抽取数据 需要为不同的数据库写卸数脚本 再传输到数据集市文件服务器 再入库 每一环节都需要调度 2 不同的数据库写不同
  • Linux安装unrar

    Linux中unrar的安装 下载unrar wget https www rarlab com rar rarlinux x64 6 0 2 tar gz 解压文件 tar xf rarlinux x64 6 0 2 tar gz 安装
  • win10安装protobuf

    Win10安装protobuf 准备 安装VS 下载protobuf 安装cmake 下载gmock 安装 Cmake构建配置 使用VS编译protobuf 准备 安装VS 下载protobuf 下载地址 https github com
  • flutter app启动页的强制获取配置前先检查是否有网络

    判断网络状态 connectivity plus 2 3 9 late StreamSubscription
  • Java8 stream流的操作 map和list转换

    Java8 stream流的操作 1 将map的所有value转换成list 创建map并塞两个值 Map
  • Linux shell命令行可选参数的getopts命令使用例子

    Linux shell命令行可选参数的getopts命令使用例子 一 概述 getopts option DESCPRITION VARIABLE option 表示为某个脚本可以使用的选项 冒号如果某个选项 option 后面出现了冒号
  • pytorch余弦退火学习率CosineAnnealingLR的使用

    一 背景 再次使用CosineAnnealingLR的时候出现了一点疑惑 这里记录一下 其使用方法和参数含义 后面的代码基于 pytorch 版本 1 1 不同版本可能代码略有差距 但是含义是差不多的 二 余弦退火的目的和用法 2 1 为啥
  • flutter video_player视频播放器的横竖屏切换

    参考 需要使用到的库 import package flutter services dart 用到的方法 实现全屏功能代码 一般写在按钮或者初始化函数里 SystemChrome setPreferredOrientations Devi