using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class PlayerVideo : MonoBehaviour {
string urlPath = "http://192.168.0.108/test/end.mp4";//资源网络路径
string file_SaveUrl;//资源保路径
FileInfo file;
private bool down;
public VideoPlayer _videoPlayer;
public RawImage rawimage;
private void Awake()
{
_videoPlayer = GetComponent<VideoPlayer>();
rawimage = GetComponent<RawImage>();
}
private void Start()
{
string FullPath4 = "\\data\\";
string FullPath = Environment.CurrentDirectory + "\\data\\1我要立案.avi";
//编辑器内使用FullPath1,exe使用FullPath
string FullPath1 = Environment.CurrentDirectory + "\\Assets\\data\\1我要立案.avi";
//var path = Application.persistentDataPath;
//file_SaveUrl = path + "/end.mp4"; //保存的本地路径 记得加上文件后缀名
file_SaveUrl = FullPath;
file = new FileInfo(file_SaveUrl);
Debug.Log("path:" + file_SaveUrl);
DirectoryInfo mydir = new DirectoryInfo(file_SaveUrl);
if (File.Exists(file_SaveUrl))//判断一下本地是否有了该音频 如果有就不需下载
{
PlayVideo();
}
else
{
StartCoroutine(DownFile(urlPath));
}
}
IEnumerator DownFile(string url)
{
WWW www = new WWW(url);
down = false;
yield return www;
down = true;
if (www.isDone)
{
byte[] bytes = www.bytes;
CreatFile(bytes);
PlayVideo();
}
}
void CreatFile(byte[] bytes)
{
Stream stream;
stream = file.Create();
stream.Write(bytes, 0, bytes.Length);
Debug.Log("下载完成");
stream.Close();
stream.Dispose();
}
public void PlayVideo()
{
StartCoroutine(DelayPlayVideo(1));
}
IEnumerator DelayPlayVideo(float time)
{
_videoPlayer.source = VideoSource.Url;
_videoPlayer.url = file_SaveUrl;
_videoPlayer.Prepare();
_videoPlayer.playOnAwake = false;
while (!_videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
rawimage.texture = _videoPlayer.texture;
_videoPlayer.Play();
yield return new WaitForSeconds(0.02f);
//rawimage.texture = _videoPlayer.texture;
//_videoPlayer.Play();
}
}
网友评论