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


你可能感兴趣的:(flutter,python,开发语言)