- Home /
Question by
xproject2013 · Jul 17, 2015 at 09:49 AM ·
c#scripting problemimportingmovie
Unity 5 how to load movie (movieTexture) from local folder in Win
Hi, I'm trying to load an external movie (.mp4) in a folder of my PC but it doesn't work, here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
public class LoadFilesController : MonoBehaviour {
string path = "C:/Video/";
string url = "file:///C:/Video/";
string fileName = "video.mp4";
string[] files;
RawImage player;
AudioSource sound;
MovieTexture movie;
void Start () {
player = GetComponent<RawImage> ();
sound = GetComponent<AudioSource> ();
files = Directory.GetFiles (path);
foreach (string file in files){
//Debug.Log(file);
}
StartCoroutine (loadMovie ());
}
public void setMovie(string movieName){
fileName = movieName;
StartCoroutine (loadMovie ());
}
IEnumerator loadMovie(){
WWW www = new WWW (url + fileName);
yield return www;
Debug.Log (www.movie.duration + " <--- wwww");
if (www.error != null) {
Debug.Log ("Error: Can't laod movie! - " + www.error);
yield break;
} else {
MovieTexture video = www.movie as MovieTexture;
Debug.Log("Movie loaded");
Debug.Log(www.movie);
movie = video;
setMovie();
playMovie();
}
}
public void setMovie(){
Debug.Log (movie.name + " ------");
player.texture = movie;
sound.clip = movie.audioClip;
}
public void playMovie(){
movie.Play ();
sound.Play ();
}
public void pauseMovie(){
movie.Pause ();
sound.Pause ();
}
}
Comment
Best Answer
Answer by xproject2013 · Jul 17, 2015 at 12:12 PM
I found the solutions: the movie must be in .ovg format otherwise it can't be load
do you have any solution to this? or a shortcut for .OVG quick importation? like shortcut to converting any video files to .ovg? and quickly importing to Unity at runtime?
Answer by Seyed_Morteza_Kamaly · Nov 19, 2015 at 09:14 PM
IEnumerator loadAndPlay(){
WWW diskMovieDir = new WWW("file:///C:/yourvideoDir/video.mp4");
//Wait for file finish loading
while(!diskMovieDir.isDone){
yield return null;
}
//Save the loaded movie from WWW to movetexture
MovieTexture movieToPlay = diskMovieDir.movie;
//Hook the movie texture to the current renderer
MeshRenderer ren = container.GetComponent<MeshRenderer>();
ren.material.mainTexture = movieToPlay ;
movieToPlay.play();
}
Your answer
Follow this Question
Related Questions
Audio delay after loading ogg external movie at runtime 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers