在HelloARVideo里面提供了播放本地StreamingAssets中视频的方法,但是如果视频很多,打包起来会很大,所以这里提供一种将视频放在网上,然后通过网络加载的方法。
- <div class="blockcode"><blockquote>using UnityEngine;
- using System.Collections;
- using System.IO;
- public class PlayVideo: MonoBehaviour
- {
- public GameObject Target; //网络视频地址
- private string Url_movie; //视频下载本地存储地址
- private string Url_save; //文件
- FileInfo file;
- int ForZ;
- //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
- public static readonly string PathURL =
- #if UNITY_ANDROID
- "jar:file://" + Application.dataPath + "!/assets/";
- #elif UNITY_IPHONE
- Application.dataPath + "/Raw/";
- #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
- "file://" + Application.dataPath + "/StreamingAssets/";
- #else
- string.Empty;
- #endif
- void Awake ()
- {
- ForZ = 0;
- Url_movie = "http://bmw2.thefront.com.cn/m2_2016/media/final.mp4";
- //Url_movie = "http://bsyqncdn.miaopai.com/stream/jnyEqgHIBvr~XVRWJXbT6g__.mp4";
- //Url_save = Application.persistentDataPath + "/test.mp4"; //初始化文件
- Url_save = PathURL + "112233.mp4";//初始化文件
- file = new FileInfo (Url_save);
- }
- void Start ()
- {
- if (!file.Exists) { //判断文件是否下载过
- StartCoroutine ("downmovie");
- } else { //文件存在 直接播放视频
- if (Target.activeSelf == true) {
- print ("文件存在 直接播放视频");
- // Handheld.PlayFullScreenMovie (Url_save, Color.black, FullScreenMovieControlMode.Full);
- }
- }
- }
- void Update ()
- {
- if (Target.activeSelf == false) {
- ForZ = 0;
- }
- if (Target.activeSelf == true && ForZ == 0) {
- Playermov ();
- ForZ = 1;
- }
- }
- 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 ()
- {
- print ("xx");
- Handheld.PlayFullScreenMovie (Url_movie, Color.black, FullScreenMovieControlMode.Hidden);
- }
- }
|
|
|
|
|
|