Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Tenshiii · Nov 10, 2015 at 04:32 PM · unity 5wwwaudiosourceaudioclipmovietexture

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!");
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tenshiii · Nov 11, 2015 at 12:59 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Precisely timestamping when audio leaves speaker 0 Answers

My audio doesn't play, I'm through my options... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges