Unity播放本地视频我就不介绍了 ,方法有很多包括MovieTexture,GUI,Handle,MobileMovieTexture等等都是可以实现的。当播放网络视频,前面讲到的Handle就可以实现播放网络视频的功能。关于Handle的使用方法,官方手册中写得比较详细http://docs.unity3d.com/ScriptReference/Handheld.PlayFullScreenMovie.html。其中用到的就是
void Start () {
Handheld.PlayFullScreenMovie(url,Color.black,FullScreenMovieControlMode.Full);
}
using UnityEngine;
using System.Collections;
using System.IO;
public class PlayerMovie : MonoBehaviour {
//网络视频地址
private string Url_movie;
//视频下载本地存储地址
private string Url_save;
//文件
FileInfo file;
void Awake()
{
Url_movie="http://xxx.../business_work/we.avi";
Url_save=Application.persistentDataPath+"/test.avi";
//初始化文件
file=new FileInfo (Url_save);
}
void Start()
{
//Handheld.PlayFullScreenMovie(Url_movie, Color.black, FullScreenMovieControlMode.Hidden);
//判断文件是否下载过
if(!file.Exists)
{
StartCoroutine("downmovie");
}else
{
//文件存在 直接播放视频
print ("文件存在 直接播放视频");
Handheld.PlayFullScreenMovie(Url_save,Color.black,FullScreenMovieControlMode.Full);
}
}
IEnumerator downmovie()
{
//加载www
WWW _www=new WWW(Url_movie);
yield return _www;
if(_www.isDone)
{
print("视频加载完成");
//获取www的字节
byte[] bytes=_www.bytes;
creat(bytes);
}
}
//文件的流写入
void creat(byte [] bytes)
{
Stream str;
//文件创建
str=file.Create();
//文件写入
str.Write(bytes,0,bytes.Length);
//关闭并销毁流
str.Close();
str.Dispose();
//播放视频
Playermov();
}
void Playermov()
{
Handheld.PlayFullScreenMovie(Url_save,Color.black,FullScreenMovieControlMode.Full);
}
}