flutter video_player插件基础播放

VideoPlayerController? _videoController;
Future<void>? _initializeVideoPlayerFuture;

initState(){
super.initState();
_init();
}

_init(){
_videoController = VideoPlayerController.network(
    _videoUrl!,
    videoPlayerOptions: VideoPlayerOptions(
      mixWithOthers: true,
    )
);

_initializeVideoPlayerFuture = _videoController!.initialize().then((_) {

  _videoController!.setVolume(0);
  _videoController!.setLooping(true);
  double _videoWidth = _videoController!.value.size.width;
  double _videoHeight = _videoController!.value.size.height;
  print('_videoWidth= $_videoWidth  _videoHeight= $_videoHeight');
  print('aspectRatio= ${_videoController!.value.aspectRatio}');
  _sizeWidth = _videoWidth;
  _sizeHeight = _videoHeight;

  double _aspectRatio = _videoHeight / _videoWidth;
  _logic.videoAspectRatio = _aspectRatio;
  print('videoAspectRatio===== ${_logic.videoAspectRatio}');
  _videoController!.play();
  _isVideoInit = true;
  setState(() {});
});

}

_videoPlayerO(){
  return FutureBuilder(
    future: _initializeVideoPlayerFuture,
    builder: (BuildContext context, value) {
      if (value.connectionState == ConnectionState.done) {
        return AspectRatio(
          // 设置视频播放器的宽高比。
          aspectRatio: _videoController!.value.aspectRatio,
          // 播放视频的组件
          child: VideoPlayer(_videoController!),
        );

        // SizedBox.expand(//尽量大的去填充父布局
        //       child: FittedBox(//使用FittedBox设置BoxFit.cover使子控件等比占据父容器
        //         fit: BoxFit.cover,
        //         child: SizedBox(
        //             width: _sizeWidth,height: _sizeHeight,
        //             child: VideoPlayer(_videoController)),
        //       )
        //   );

      } else {
        return Center(
            child: _coverImageView(showLoading: true)
        );
      }
    },
  );
}

//视频封面
_coverImageView({bool? showLoading}){
  return Stack(
    alignment: Alignment.center,
    children: [
      _logic.articleDetail!.articlePic != null ?
      Utils().roundedImage(_logic.articleDetail!.articlePic!.last.url, 2,
                  fit: BoxFit.cover, borderColor: Colors.black)
          : SizedBox(width: double.infinity, height: double.infinity),
        CircularProgressIndicator(
          valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
          strokeWidth: 2,
        )
    ],
  );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值