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.

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*);*
  •  // Wait for it to complete.*
    
  •  yield return lFile;*
    
  •  // Continue on with other stuff.*
    

_ Debug.Log("File Download Complete: "+lFileURLs*);_
_
}*_

* DownloadComplete();*
}

public void DownloadComplete()
{
* Debug.Log(“All Downloads complete!”);*
}