unity 获取本地视频/下载网络视频

using System.Collections;
using System.IO;
using UnityEngine;
    public class DownVideo : MonoBehaviour
{
    FileStream fs;
    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"));
    }
        /// 
        /// 加载视频并下载
        /// 
        /// ImgTargetMgr
        /// 识别图名
        /// 视频网络地址
        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;

                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("视频下载成功");
                }

            }
        }
    }

你可能感兴趣的:(C#,unity,unity,音视频,网络,c#)