I have a problem regarding the MovieTexture in Unity: I want to load movie and audio files in Unity on the same game object (not simultaneously). So, I have to differentiate between audio and video file, but I don’t get it working. Here is what I tried to do:
MovieTexture movieTexture;
AudioSource audio = gameObject.GetComponent<AudioSource>();
WWW file = new WWW(@"file:///"+pathToFile);
// example.ogg (It can be either an audio or a movie file, I don't know it before because
// it is send to unity and unity should differentiate if it is an audio or movie file)
if(file.movie != null)
{
movieTexture = file.movie;
gameObject.GetComponent<Renderer>().material.mainTexture = movieTexture;
audio.clip = movieTexture.audioClip;
movieTexture.Play();
audio.Play ();
}
else if(file.audioClip != null && file.movie == null) // At this place, if I want to load an audio file, Unity crashes...I tried many different audio files, but still the same issue
{
audio.clip = file.audioClip;
audio.Play();
}
How can I do this?
EDIT: I also tried it with waiting until the file is finished. The code looks like that:
MovieTexture movieTexture;
AudioSource audio = gameObject.GetComponent<AudioSource>();
WWW file = new WWW(@"file:///"+pathToFile);
while(!file.isDone)
{
continue;
}
// example.ogg (It can be either an audio or a movie file, I don't know it before because
// it is send to unity and unity should differentiate if it is an audio or movie file)
if(file.movie != null)
{
movieTexture = file.movie;
gameObject.GetComponent<Renderer>().material.mainTexture = movieTexture;
audio.clip = movieTexture.audioClip;
movieTexture.Play();
audio.Play ();
}
else if(file.audioClip != null && file.movie == null) // At this place, if I want to load an audio file, Unity crashes...I tried many different audio files, but still the same issue
{
audio.clip = file.audioClip;
audio.Play();
}
But it is not working either. Unity crashed if I want to load an audio .ogg file, because Unity can’t recognize it throught the if-condition.
I am using Unity 5.2 Personal Edition.