Flutter 视频播放器组件封装

添加插件

pubspec.yaml

chewie: ^1.0.0
video_player: ^2.0.2

封装播放器

video_view.dart

class VideoView extends StatefulWidget {
  final String url; // 播放地址
  final String cover; // 封面
  final bool autoPlay; // 是否自动播放
  final bool looping; // 是否循环
  final double aspectRatio; // 宽高比
  const VideoView(this.url,
      {Key key, this.cover, this.autoPlay, this.looping, this.aspectRatio})
      : super(key: key);

  @override
  _VideoViewState createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;

  // 封面 (FractionallySizedBox:可控制组件在水平/垂直方向上填充满父容器)
  get _placeHolder => FractionallySizedBox(
        widthFactor: 1,
        child: cacheImage(widget.cover),
      );

  // 进度条颜色设置
  get _progressColors => ChewieProgressColors(
        playedColor: primary,
        bufferedColor: primary,
        handleColor: Colors.grey,
        backgroundColor: primary[50],
      );

  @override
  void initState() {
    super.initState();
    _videoPlayerController = VideoPlayerController.network(widget.url);
    _chewieController = ChewieController(
        //要播放的视频的控制器
        videoPlayerController: _videoPlayerController,
        // 缩放比
        aspectRatio: widget.aspectRatio,
        // 是否自动播放
        autoPlay: widget.autoPlay,
        // 封面
        looping: widget.looping,
        // 进度条颜色
        placeholder: _placeHolder,
        // 进度条颜色
        materialProgressColors: _progressColors,
        // 是否静音
        allowMuting: false,
        // 是否显示播放速度
        allowPlaybackSpeedChanging: false,
        customControls: MaterialControls(
          showLoadingOnInitialize: false,
          showBigPlayIcon: false,
          bottomGradient: blackLinearGradient(),
        ));
  }

  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
    _chewieController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width; // 播放器宽度
    double screenHeight = screenWidth / widget.aspectRatio; // 播放器高度
    return Container(
      width: screenWidth,
      height: screenHeight,
      color: Colors.grey,
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }
}

自定义播放器UI

点击查看自定义播放器UI源码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值