一.缩略图如何获取
1.第一种就是直接通过手动截屏,然后对截好的图片进行保存,接着再通过配置表进行配置
2.第二种就是直接通过代码动态保存视频的某一帧为视频的缩略图。
(我在这里采取的是第二种)
首先就是我们得把我们所需要进行处理的视频的路径配置好
这里我省略了如何获取表述数据的方式代码比较多
视频路径我们配置完成之后我们就可以通过携程的方式把每个视频的0.1秒的位置进行保存用来做视频的缩略图。
IEnumerator IESaveVideoThumb(string filePath, Action<string,string,Sprite> call)
{
foreach (var file in videoInfos)
{
mediaPlayer.m_VideoPath = file.VideoPath;
mediaPlayer.OpenVideoFromFile(mediaPlayer.m_VideoLocation, mediaPlayer.m_VideoPath, mediaPlayer.m_AutoStart);
if (!File.Exists(Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png"))
{
yield return new WaitForSeconds(0.2f);
yield return null;
// mediaPlayer.Info.GetDurationMs() 取的是毫秒
float durationMs = mediaPlayer.Info.GetDurationMs() / 1000f;
float ms = durationMs < 1 ? durationMs : 0.1f;
//ms= 1;
//取视频中1秒位置保存为图片
Texture2D t = mediaPlayer.ExtractFrame(null, ms);
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
string path = Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png";
if (!Directory.Exists(Path.GetDirectoryName(file.VideoPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(file.VideoPath));
}
if (!File.Exists(path))
{
//写入文件
try
{
File.WriteAllBytes(path, t.EncodeToJPG());
Debug.Log(filePath);
}
catch
{
Debug.LogError(filePath);
}
}
else
{
Debug.Log("文件已存在!");
}
yield return new WaitForSeconds(0.1f);
//调用回调函数 刷新UI界面
call?.Invoke(path, file.VideoPath, sprite);
}
else
{
call?.Invoke(Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png", file.VideoPath, null);
}
}
Debug.Log(mediaPlayer.Control.IsPlaying());
if (mediaPlayer.Control.IsPlaying())
{
mediaPlayer.Control.Seek(0);
mediaPlayer.Control.Stop();
Debug.Log(mediaPlayer.Control.IsPlaying());
}
}
二.如何将缩略图刷新到UI
上面我们已经得到了所有视频的缩略图,接下来就是怎么把缩略显示到UI上面,大概思路就是两个方向,我们第一次运行的时候和第一次运行之后两种情况,第一次时候我们需要把图片写入到本地,得先写入,这里我是在第一次时候直接把每次的截取的图片写入之前把Texture2D 转换为Sprite,把他为参数直接刷新到UI上面
//取视频中1秒位置保存为图片
Texture2D t = mediaPlayer.ExtractFrame(null, ms);
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
public void UpdateUI(string path,string filePath,Sprite s)
{
//加载视频缩略图
disPlay.gameObject.SetActive(false);
GameObject gameObject = Instantiate(obj, content);
gameObject.name = typeof(PlayVideoItem).Name;
FileInfo fileinfo = new FileInfo(path);
gameObject.GetComponent<PlayVideoItem>().Init(filePath, "file://" + fileinfo.FullName,s);
}
第一次之后的情况则是直接通过保存好的图片的路径进行本地下载,然后在刷新到UI上面
public void Init(string videoPath,string url,Sprite s)
{
path = videoPath;
this.url = url;
if(s!=null)
{
playButton.GetComponent<Image>().sprite = s;
}
else
{
StartCoroutine(LoadAssetsOutside(url));
}
}
IEnumerator LoadAssetsOutside(string filePath)
{
WWW www = new WWW(filePath);
yield return www;
Texture2D t = www.texture;
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
playButton.GetComponent<Image>().sprite = sprite;
www.Dispose();
}
三.效果图: