Differentiate movie and audio file on game object
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.
Answer by HenryStrattonFW · Nov 09, 2015 at 12:53 PM
The code you provided should not even compile, since you define a WWW object called "file", and then a few lines later define another WWW object, also called "file".
WWW requests like this are asynchronous, which means they do not complete immediately. in order to wait for them to be complete I suggest using them with co routines. Since you want multiple files, here is one way that you could download both files, and wait until you have both to proceed.
public void LoadFiles()
{
string[] lFiles = new string[]
{
"PATH_TO_MOVIE",
"PATH_TO_AUDIO"
};
StartCoroutine(_LoadFile(lFiles));
}
IEnumerator _LoadFile(string[] lFileURLs)
{
for(int i=0;i<lFileURLs.Length; i++)
{
// Create the WWW request
WWW lFile = new WWW(lFileURLs[i]);
// Wait for it to complete.
yield return lFile;
// Continue on with other stuff.
Debug.Log("File Download Complete: "+lFileURLs[i]);
}
DownloadComplete();
}
public void DownloadComplete()
{
Debug.Log("All Downloads complete!");
}
Hey, thanks for your fast answer. I did something wrong in my code. I am really sorry for that! It should be only one www request. This file is an ogg file because it is loaded from disk. In this case I don't know if it is an audio or a movie file that's why I have to check that. I check it with "if it is an audio file than play audio, otherwise play movieTexture". However, it is not working if I want to make this if-condition. See my edit.
Your answer
Follow this Question
Related Questions
Why does AudioSource.time return Infinity in Unity 5.3? 2 Answers
Play audio after enemy dies 1 Answer
My sound effect is not playing 0 Answers