当前位置:网站首页>Unity get local video / download network video

Unity get local video / download network video

2022-06-12 15:33:00 Solitary smoke cold~

using System.Collections;
using System.IO;
using UnityEngine;
    public class DownVideo : MonoBehaviour
{
    
    FileStream fs;
    string path = null;
    public VideoPlayer video;
     void Awake()
    {
    
        StartCoroutine(DownOrLoadVideo(" Video name ", "http://kanyikan.oss-cn-shanghai.aliyuncs.com/kanrongmei/%E6%B1%89%E5%85%B3%E7%B4%AB%E7%A0%82.mp4"));
       // If the object is hidden   Unable to execute the coordination process   You can use global inheritance MONO If you call a coroutine with a single column, you won't report an error 
       //MainController.GetInstance().StartCoroutine(DownOrLoadVideo(" Video name ", "http://kanyikan.oss-cn-shanghai.aliyuncs.com/kanrongmei/%E6%B1%89%E5%85%B3%E7%B4%AB%E7%A0%82.mp4"));
    }
        /// <summary>
        ///  Load the video and download 
        /// </summary>
        /// <param name="itm">ImgTargetMgr</param>
        /// <param name="downloadVideoName"> Identification drawing name </param>
        /// <param name="videoURL"> Video network address </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(" download " + path);
            // Download or not 
            if (File.Exists(path + @"/" + downloadVideoName + ".mp4"))
            {
    
                // Local load 
                path += "/" + downloadVideoName + ".mp4";
                video.url = path ;// Assignment can play 
            }
            else
            {
    
                // Network load 
                WWW www = new WWW(videoURL);
                yield return www;

                if (!string.IsNullOrEmpty(www.error))
                {
    
                    Debug.Log(" request was aborted ");
                }
                else
                {
    
                    fs = File.Create(path + "/" + downloadVideoName + ".mp4"); //path For the path where you want to save the file .
                    fs.Write(www.bytes, 0, www.bytes.Length);
                    fs.Close();
                    path += "/" + downloadVideoName + ".mp4";
                      video.url = path ;// Assignment can play 
                    Debug.Log(" Video download successful ");
                }

            }
        }
    }
原网站

版权声明
本文为[Solitary smoke cold~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281738438801.html