- Home /
Question by
hongpai · Mar 23, 2014 at 03:41 PM ·
wwwresources.loadfilestream
Problems downloading movie using www
I try to download www.movie to resources folder,then I can play movie later. the file didn't appear until I hit the console window.Somebody can help me.
using UnityEngine;
using System.Collections;
using System.IO;
public class moviePlayerScript : MonoBehaviour {
private string videoFile;
public GUITexture ttt;
public WWW www;
public MovieTexture m1;
void Start()
{
videoFile = Application.dataPath + "/StreamingAssets/Resources/sample.ogg";//for pc path absolute path
}
IEnumerator DownloadAndPlayVideo()
{
if(System.IO.File.Exists(videoFile))
{
m1 = Resources.Load("sample") as MovieTexture;
if(m1!=null && !m1.isPlaying && m1.isReadyToPlay)
{
ttt.texture = m1;
m1.Play();
audio.clip = m1.audioClip;
audio.Play();
}
yield return null;
}
else{
string playVideoFile = videoFile;
www = new WWW("http://www.unity3d.com/webplayers/Movie/sample.ogg");
yield return www;
if (www != null && www.isDone && www.error == null)
{
FileStream stream = new FileStream(videoFile, FileMode.Create);
stream.Write(www.bytes, 0, www.bytes.Length);
stream.Flush();
stream.Close();
m1 = www.movie;
if(!m1.isPlaying && m1.isReadyToPlay)
{
ttt.texture = m1;
m1.Play();
audio.clip = m1.audioClip;
audio.Play();
}
yield break;
}
}
}
void OnGUI()
{
if(GUI.Button(new Rect(0,0,100,90), "Turn On"))
{
StartCoroutine(DownloadAndPlayVideo());
}
if(GUI.Button(new Rect(200,0,100,90), "Turn Off"))
{
StopCoroutine("DownloadAndPlayVideo");
m1.Stop();
}
}
}
Comment