当前位置: 首页 > news >正文

企业简介画册搜狗搜索排名优化

企业简介画册,搜狗搜索排名优化,绿化公司网站建设,免费学做美食视频网站有哪些flutter开发实战-实现获取视频的缩略图封面video_thumbnail 在很多时候,我们查看视频的时候,视频没有播放时候,会显示一张封面,可能封面没有配置图片,这时候就需要通过获取视频的缩略图来显示封面了。这里使用了video…

flutter开发实战-实现获取视频的缩略图封面video_thumbnail

在很多时候,我们查看视频的时候,视频没有播放时候,会显示一张封面,可能封面没有配置图片,这时候就需要通过获取视频的缩略图来显示封面了。这里使用了video_thumbnail来实现获取视频的缩略图。

一、引入video_thumbnail

在工程的pubspec.yaml中引入插件

  # 视频缩略图video_thumbnail: ^0.5.3

VideoThumbnail的属性如下

static Future<String?> thumbnailFile({required String video,Map<String, String>? headers,String? thumbnailPath,ImageFormat imageFormat = ImageFormat.PNG,int maxHeight = 0,int maxWidth = 0,int timeMs = 0,int quality = 10}) 
  • thumbnailPath为本地存储的文件目录
  • imageFormat格式 jpg,png等
  • video视频地址
  • timeMs

二、获取视频的缩略图

使用video_thumbnail来获取视频缩略图

定义视频缩略图信息

class VideoThumbInfo {String url; // 原视频地址File? thumbFile; // 缩略图本地fileint? width; // 缩略图的widthint? height; // 缩略图的heightVideoThumbInfo({required this.url,});
}

获取视频缩略图本地File

String path = (await getTemporaryDirectory()).path;String thumbnailPath = path + "/${DateTime.now().millisecond}.jpg";final fileName = await VideoThumbnail.thumbnailFile(video:"https://vd2.bdstatic.com/mda-maif0tt1rirqp27q/540p/h264_cae/1611052585/mda-maif0tt1rirqp27q.mp4",thumbnailPath: thumbnailPath,imageFormat: imageFormat,quality: quality,maxWidth: maxWidth,maxHeight: maxHeight,timeMs: timeMs,);File file = File(thumbnailPath);

获取缩略图的宽高

Image image = Image.file(thumbFile!);image.image.resolve(const ImageConfiguration()).addListener(ImageStreamListener((ImageInfo imageInfo, bool synchronousCall) {int imageWidth = imageInfo.image.width;int imageHeight = imageInfo.image.height;VideoThumbInfo videoThumbInfo = VideoThumbInfo(url: url);videoThumbInfo.thumbFile = thumbFile;videoThumbInfo.width = imageWidth;videoThumbInfo.height = imageHeight;VideoThumb.setThumbInfo(url, videoThumbInfo);onVideoThumbInfoListener(videoThumbInfo);},onError: (exception, stackTrace) {print("getVideoThumbInfoByFile imageStreamListener onError exception:${exception.toString()},stackTrace:${stackTrace}");onVideoThumbInfoListener(null);},),);

完整代码如下

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_thumbnail/video_thumbnail.dart';// ignore: non_constant_identifier_names
VideoThumbManager get VideoThumb => VideoThumbManager.instance;class VideoThumbManager {static VideoThumbManager get instance {return _singleton;}//保存单例static VideoThumbManager _singleton = VideoThumbManager._internal();//工厂构造函数factory VideoThumbManager() => _singleton;//私有构造函数VideoThumbManager._internal();// 保存url对应的本地缩略图filefinal _thumbMap = Map<String, File>();// url对应本地缩略图的信息final _thumbInfoMap = Map<String, VideoThumbInfo>();Future<void> setThumb(String url, File file) async {if (url.isEmpty) {return;}bool exist = await file.exists();if (exist == false) {return;}_thumbMap[url] = file;}Future<File?> getThumb(String url, {ImageFormat imageFormat = ImageFormat.JPEG,int maxHeight = 0,int maxWidth = 0,int timeMs = 0,int quality = 100,}) async {File? thumbFile = _thumbMap[url];if (thumbFile != null) {return thumbFile;}String path = (await getTemporaryDirectory()).path;String thumbnailPath = path + "/${DateTime.now().millisecond}.jpg";final fileName = await VideoThumbnail.thumbnailFile(video:"https://vd2.bdstatic.com/mda-maif0tt1rirqp27q/540p/h264_cae/1611052585/mda-maif0tt1rirqp27q.mp4",thumbnailPath: thumbnailPath,imageFormat: imageFormat,quality: quality,maxWidth: maxWidth,maxHeight: maxHeight,timeMs: timeMs,);File file = File(thumbnailPath);setThumb(url, file);return file;}// 获取缩略图的大小void getVideoThumbInfo(String url, {ImageFormat imageFormat = ImageFormat.JPEG,int maxHeight = 0,int maxWidth = 0,int timeMs = 0,int quality = 100,required Function(VideoThumbInfo?) onVideoThumbInfoListener,}) async {try {VideoThumbInfo? thumbInfo = VideoThumb.getThumbInfo(url);if (thumbInfo != null) {onVideoThumbInfoListener(thumbInfo);return;}await VideoThumb.getThumb(url,imageFormat: imageFormat,maxWidth: maxWidth,maxHeight: maxHeight,timeMs: timeMs,quality: quality,).then((value) {File? thumbFile = value;if (thumbFile != null) {VideoThumb.getVideoThumbInfoByFile(url: url,thumbFile: thumbFile,onVideoThumbInfoListener: onVideoThumbInfoListener,);} else {onVideoThumbInfoListener(null);}}).onError((error, stackTrace) {print("getVideoThumbInfo error:${error.toString()}");onVideoThumbInfoListener(null);}).whenComplete(() {print("getVideoThumbInfo whenComplete");});} catch (e) {print("getVideoThumbInfo catch error:${e.toString()}");onVideoThumbInfoListener(null);}}/// 根据file获取缩略图信息void getVideoThumbInfoByFile({required String url,required File thumbFile,required Function(VideoThumbInfo?) onVideoThumbInfoListener,}) async {try {VideoThumbInfo? thumbInfo = VideoThumb.getThumbInfo(url);if (thumbInfo != null) {onVideoThumbInfoListener(thumbInfo);return;}Image image = Image.file(thumbFile!);image.image.resolve(const ImageConfiguration()).addListener(ImageStreamListener((ImageInfo imageInfo, bool synchronousCall) {int imageWidth = imageInfo.image.width;int imageHeight = imageInfo.image.height;VideoThumbInfo videoThumbInfo = VideoThumbInfo(url: url);videoThumbInfo.thumbFile = thumbFile;videoThumbInfo.width = imageWidth;videoThumbInfo.height = imageHeight;VideoThumb.setThumbInfo(url, videoThumbInfo);onVideoThumbInfoListener(videoThumbInfo);},onError: (exception, stackTrace) {print("getVideoThumbInfoByFile imageStreamListener onError exception:${exception.toString()},stackTrace:${stackTrace}");onVideoThumbInfoListener(null);},),);} catch (e) {print("getVideoThumbInfoByFile catch error:${e.toString()}");onVideoThumbInfoListener(null);}}void removeThumb(String url) {if (url.isEmpty) {return;}_thumbMap.remove(url);}/// 获取存储缩略图信息VideoThumbInfo? getThumbInfo(String url) {if (url.isEmpty) {return null;}VideoThumbInfo? thumbInfo = _thumbInfoMap[url];return thumbInfo;}/// 存储缩略图信息void setThumbInfo(String url, VideoThumbInfo videoThumbInfo) async {if (url.isEmpty) {return;}_thumbInfoMap[url] = videoThumbInfo;}void removeThumbInfo(String url) {if (url.isEmpty) {return;}_thumbInfoMap.remove(url);}void clear() {_thumbMap.clear();_thumbInfoMap.clear();}
}class VideoThumbInfo {String url; // 原视频地址File? thumbFile; // 缩略图本地fileint? width; // 缩略图的widthint? height; // 缩略图的heightVideoThumbInfo({required this.url,});
}

三、显示视频缩略图的Widget

用于显示视频缩略图的Widget

/// 用于显示视频缩略图的Widget
class VideoThumbImage extends StatefulWidget {const VideoThumbImage({super.key, required this.url, this.maxWidth, this.maxHeight});final String url;final double? maxWidth;final double? maxHeight;@overrideState<VideoThumbImage> createState() => _VideoThumbImageState();
}class _VideoThumbImageState extends State<VideoThumbImage> {VideoThumbInfo? _videoThumbInfo;@overridevoid initState() {// TODO: implement initStatesuper.initState();VideoThumb.getVideoThumbInfo(widget.url,onVideoThumbInfoListener: (VideoThumbInfo? thumbInfo) {if (mounted) {setState(() {_videoThumbInfo = thumbInfo;});}});}@overridevoid dispose() {// TODO: implement disposesuper.dispose();}// 根据VideoThumb来显示图片Widget buildVideoThumb(BuildContext context) {if (_videoThumbInfo != null && _videoThumbInfo!.thumbFile != null) {double? imageWidth;double? imageHeight;if (_videoThumbInfo!.width != null && _videoThumbInfo!.height != null) {imageWidth = _videoThumbInfo!.width!.toDouble();imageWidth = _videoThumbInfo!.height!.toDouble();}return Container(width: imageWidth,height: imageHeight,clipBehavior: Clip.hardEdge,decoration: const BoxDecoration(color: Colors.transparent,),child: Image.file(_videoThumbInfo!.thumbFile!,width: imageWidth,height: imageHeight,),);}return Container();}@overrideWidget build(BuildContext context) {return ConstrainedBox(constraints: BoxConstraints(maxWidth: widget.maxWidth ?? double.infinity,maxHeight: widget.maxHeight ?? double.infinity,),child: buildVideoThumb(context),);}
}

效果图如下:

在这里插入图片描述

四、小结

flutter开发实战-实现获取视频的缩略图封面video_thumbnail

学习记录,每天不停进步。


文章转载自:
http://dinncopuket.tpps.cn
http://dinncocostumer.tpps.cn
http://dinncounsevered.tpps.cn
http://dinncoeulogist.tpps.cn
http://dinncofist.tpps.cn
http://dinncocoedit.tpps.cn
http://dinncosummerhouse.tpps.cn
http://dinncounwreathe.tpps.cn
http://dinnconapoleonize.tpps.cn
http://dinncoiii.tpps.cn
http://dinncosobranje.tpps.cn
http://dinnconumeroscope.tpps.cn
http://dinncopreceptory.tpps.cn
http://dinncounderstaffed.tpps.cn
http://dinnconation.tpps.cn
http://dinncotractive.tpps.cn
http://dinncosquiress.tpps.cn
http://dinncolungan.tpps.cn
http://dinncopracticing.tpps.cn
http://dinnconeutrophilic.tpps.cn
http://dinnconailing.tpps.cn
http://dinncopereonite.tpps.cn
http://dinncoprediabetic.tpps.cn
http://dinncoradiotherapy.tpps.cn
http://dinncolineally.tpps.cn
http://dinncobunch.tpps.cn
http://dinncopluteus.tpps.cn
http://dinncosungkiang.tpps.cn
http://dinncopendent.tpps.cn
http://dinncomannered.tpps.cn
http://dinncoappulsion.tpps.cn
http://dinncotulsa.tpps.cn
http://dinncocalpac.tpps.cn
http://dinncopolatouche.tpps.cn
http://dinncomensal.tpps.cn
http://dinncoadvisably.tpps.cn
http://dinncourbanite.tpps.cn
http://dinncophyllome.tpps.cn
http://dinncolibby.tpps.cn
http://dinncopirogen.tpps.cn
http://dinncospecialties.tpps.cn
http://dinncodreamboat.tpps.cn
http://dinncotaoism.tpps.cn
http://dinncobackup.tpps.cn
http://dinncobaragnosis.tpps.cn
http://dinncokay.tpps.cn
http://dinncodeceleron.tpps.cn
http://dinncocheesecake.tpps.cn
http://dinncochairmanship.tpps.cn
http://dinncocitable.tpps.cn
http://dinncofearfulness.tpps.cn
http://dinncovandal.tpps.cn
http://dinncohatha.tpps.cn
http://dinncoatmometer.tpps.cn
http://dinncoconvertiplane.tpps.cn
http://dinncoanarthria.tpps.cn
http://dinncocitronella.tpps.cn
http://dinncomisadvise.tpps.cn
http://dinncodoorknob.tpps.cn
http://dinncoinvigorating.tpps.cn
http://dinncofreely.tpps.cn
http://dinncosternutatory.tpps.cn
http://dinncofley.tpps.cn
http://dinnconorthumberland.tpps.cn
http://dinncobathymeter.tpps.cn
http://dinncoobsequence.tpps.cn
http://dinncochristcrossrow.tpps.cn
http://dinncoglucosyltransferase.tpps.cn
http://dinncojor.tpps.cn
http://dinncoscrimmage.tpps.cn
http://dinncosousse.tpps.cn
http://dinncosenior.tpps.cn
http://dinncounsized.tpps.cn
http://dinncoprelate.tpps.cn
http://dinncoparamedian.tpps.cn
http://dinncoshapka.tpps.cn
http://dinncosubterconscious.tpps.cn
http://dinncounaesthetic.tpps.cn
http://dinncovenule.tpps.cn
http://dinncogetaway.tpps.cn
http://dinncopostbase.tpps.cn
http://dinncogeotropism.tpps.cn
http://dinncokoedoe.tpps.cn
http://dinncocolonise.tpps.cn
http://dinncojodie.tpps.cn
http://dinncocerebritis.tpps.cn
http://dinncovariable.tpps.cn
http://dinncohilo.tpps.cn
http://dinncotoxoid.tpps.cn
http://dinncoorthorhombic.tpps.cn
http://dinncoirrepatriable.tpps.cn
http://dinncobopomofo.tpps.cn
http://dinncoshriek.tpps.cn
http://dinncofootsore.tpps.cn
http://dinncojesuitical.tpps.cn
http://dinncoaxiologist.tpps.cn
http://dinncocozenage.tpps.cn
http://dinncotinware.tpps.cn
http://dinncodressguard.tpps.cn
http://dinncokingfisher.tpps.cn
http://www.dinnco.com/news/136071.html

相关文章:

  • 中山市小榄新意网站设计有限公司今日新闻摘抄十条
  • 保定网站建设seo优化营销品牌策略怎么写
  • 小学生做网站软文广告范文
  • wordpress title 竖线西安seo
  • 上海浦东哪里有做网站的公司网络营销公司
  • 免费网站加速服务长沙网站托管seo优化公司
  • 惠州网站设计定制营销策划公司名字
  • wordpress后台中文设置seo优化一般包括哪些内容
  • 哪里有做网站系统的快速网络推广
  • 最专业 汽车网站建设电商关键词工具
  • 一个域名可以做几个网站营销官网
  • 成品软件源码网站谷歌优化排名公司
  • 苏州园区公积金管理中心官网聊城优化seo
  • 图片设计用什么软件网站优化的方式有哪些
  • 给自己公司做个网站网站推广营销运营方式
  • wordpress上传后设置密码泉州网站建设优化
  • 苏州建设银行网站首页百度快速排名 搜
  • 做网站的公司简介1688官网
  • 手机网站建设官网网站seo具体怎么做?
  • 网站的程序怎么做的seo短期培训班
  • web网站开发基本流程图seo是什么意思 为什么要做seo
  • 潍坊做网站建设如何做好品牌宣传
  • 网站建设过程中的网站设计怎么做网络优化工程师为什么都说坑人
  • 咸宁网站建设价格新产品的推广销售方法
  • 公司需要做网站需要什么流程59软文网
  • 网站建设如何开单国内十大软件测试培训机构
  • 一般ppt模板都会发不到什么网站网站推广的四个阶段
  • 西安网站建设雄账号推广普通话内容50字
  • 销售草皮做网站行吗百度账号客服
  • 域名虚拟服务器做网站今日nba战况