using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Video;
public class Test : MonoBehaviour
{
private FileStream fs;
private string path = null;
public VideoPlayer video;
void Awake()
{
StartCoroutine(DownOrLoadVideo("汉宫秋", "http://kanyikan.oss-cn-shanghai.aliyuncs.com/kanrongmei/%E6%B1%89%E5%85%B3%E7%B4%AB%E7%A0%82.mp4"));
//如果物体是隐藏的 执行不了协程 可以用全局继承了MONO的单列来调用协程就不会报错了
//MainController.GetInstance().StartCoroutine(DownOrLoadVideo("视频名字", "http://kanyikan.oss-cn-shanghai.aliyuncs.com/kanrongmei/%E6%B1%89%E5%85%B3%E7%B4%AB%E7%A0%82.mp4"));
}
/// <summary>
/// 加载视频并下载
/// </summary>
/// <param name="itm">ImgTargetMgr</param>
/// <param name="downloadVideoName">识别图名</param>
/// <param name="videoURL">视频网络地址</param>
IEnumerator DownOrLoadVideo(string downloadVideoName, string videoURL)
{
#if UNITY_EDITOR
path = Application.dataPath + @"/Video";
#elif UNITY_IPHONE || UNITY_ANDROID
string[] src = new string[1] { "Android" };
string[] srcs = Application.persistentDataPath.Split(src, System.StringSplitOptions.None);
path = srcs[0] + @"/Video";
#endif
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Debug.Log("下载" + path);
//是否下载
if (File.Exists(path + @"/" + downloadVideoName + ".mp4"))
{
//本地加载
path += "/" + downloadVideoName + ".mp4";
video.url = path;//赋值就能播放了
}
else
{
//网络加载
WWW www = new WWW(videoURL);
yield return www;
Debug.Log(www.error);
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log("请求失败");
}
else
{
fs = File.Create(path + "/" + downloadVideoName + ".mp4"); //path为你想保存文件的路径。
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
path += "/" + downloadVideoName + ".mp4";
video.url = path;//赋值就能播放了
Debug.Log("视频下载成功");
}
}
}
}
|