• 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
Question by haiden01 · Jan 13, 2016 at 11:38 AM · audioaudioclipruntime-errorwww.erroraudioclip www load

Empty or Unloaded AudioClip before get it by www.GetAudioClip

since i have to load audio files to a AudioSource from a "Sounds" folder, in the same directory as the application .exe (PC), ive decided to make it by the www.GetAudioClip function. In the editor it works perfect, but when i compile the app, it doesnt work anymore. I have a gui button on the screen with some info of the AudioSource.clip (of an array of audio sources); it shows for example, the clip.loadState, wich is always "Unloaded" for some reason.

I dont know what im doing wrong, but i test many variations of the code... nothing works at runtime... an empty audio clip seems to be there in the audiosource, with just a name (that is provided by the script), without wave, time, etc.

Heres a part of the code i use.. if it is inapprehensible at some point, let me know it. Im sorry for mi bad english, as you can see somewhere in the code notes //i speak spanish.

I appreciate any feedback

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class POST : MonoBehaviour {
 
     public bool KbSelection = true;
     public AudioSource[] AllSources;
     public string[] dirs ;
     public string folderState = "Please select a folder";
     public int current;
     float GuiFxTime = 3;
     FileInfo[] info;

     void Start () {
         for (int cnt = 0; cnt < 88; cnt++) {
             gameObject.AddComponent<AudioSource> ();
         }
         AllSources = gameObject.GetComponents<AudioSource>();
     }
     
     void Update () {
         //llena la lista de directorios con la cantidad de carpetas que hay
         if (Directory.GetDirectories (Path.GetFullPath ("./Sounds")).Length != dirs.Length) {
             dirs = Directory.GetDirectories (Path.GetFullPath ("./Sounds"));
         }
         //renombra los items de la lista con su nombre solo en vez de su ruta completa
         for (int cnt = 0; cnt < Directory.GetDirectories ( Path.GetFullPath("./Sounds")).Length; cnt++) {
             dirs [cnt] = Path.GetFileName (Directory.GetDirectories (Path.GetFullPath ("./Sounds")) [cnt]);
         }
     }
 
     void OnGUI(){
         KBSEL ();
         //check the some data of first audioclip in AllSources, and play it (test)
         if (GUI.Button (new Rect (1110, 16, 100, 250), AllSources [0].clip.length.ToString () + "\n" + AllSources [0].clip.loadState.ToString () + "\n" + AllSources [0].clip.name)) {
             AllSources [0].Play ();
         }
     }
 
     void KBSEL(){
         //GUI FX
         if (GuiFxTime > 1) { GuiFxTime -= (Time.deltaTime)*10; }
         GUI.color = new Color (1.0f, 1.0f, 1.0f, 1/GuiFxTime );
         GUI.skin.box.alignment = TextAnchor.MiddleCenter;
         
         if (dirs.Length > 0) {
             //box used as "black faded background"
             GUI.Box (new Rect (-10, -10, Screen.width + 20, Screen.height + 20), "");
             //FOLDER SELECTION
             for (int cnt = 0; cnt < Directory.GetDirectories ( Path.GetFullPath("./Sounds")).Length; cnt++) {
                 if (GUI.Button (new Rect (10 + (((Screen.width - 20) / Directory.GetDirectories (Path.GetFullPath ("./Sounds")).Length) * cnt), 40, (Screen.width - 30) / Directory.GetDirectories (Path.GetFullPath ("./Sounds")).Length, 100),
                                 "Folder: " + dirs [cnt] +
                                 "\nWav clips: " +
                                 new DirectoryInfo (Directory.GetDirectories (Path.GetFullPath ("./Sounds")) [cnt]).GetFiles ("*.wav").Length +
                                 "\nOgg clips: " +
                                 new DirectoryInfo (Directory.GetDirectories (Path.GetFullPath ("./Sounds")) [cnt]).GetFiles ("*.ogg").Length)) {
                     //fill sound clips with the sounds on the selected folder
                     StartCoroutine(LoadClips(cnt));
                 }
             }
         } else {
             GUI.Box (new Rect (-10,-10,Screen.width +20, Screen.height +20), "Theres no sounds folders!");
         }
         
         //status label
         GUI.Label (new Rect (10, 10, Screen.width-20, 25), folderState, "box");
         
     }
 
     IEnumerator LoadClips(int cur){
         //folder have clips?
         if (new DirectoryInfo (Directory.GetDirectories (Path.GetFullPath ("./Sounds")) [cur]).GetFiles ("*.ogg").Length > 0) {
             //if is not the same folder currently load
             if(current != cur){
                 //fill "info"
                 info = new DirectoryInfo (Directory.GetDirectories (Path.GetFullPath ("./Sounds")) [cur]).GetFiles ("*.ogg");
                 //get the clips, and assign them on each audio source
                 for(int cnt = 0; cnt < info.Length; cnt++){
                     WWW www = new WWW ("file:///" + info[cnt]);
                     while(!www.isDone){
                         folderState = "Loading audio clips...";
                         yield return null;
                     }
                     yield return www;        //Ive tested with and without this
 
                     AudioClip aud = www.GetAudioClip(false); //Ive tested with www.audioClip, www.GetAudioClip(with all combinations of this 3 variables), and same with www.GetAudioClipCompressed
                     Debug.LogError( www.error);    //this is used to display the www.error in the console of the development build.. it returns the error "Couldnt open file /nameofthefile.ogg"
                     
                     aud.name = info[cnt].Name;    //to assign a name for each clip
                     AllSources [cnt].clip = aud;    //assign each clip to each audiosource (88 clips/sources)
                     
                     
                 }
                 KbSelection = !KbSelection;    //does nothing here... in the original displays the folder selection
                 current = cur;
             }else{
                 KbSelection = !KbSelection;
             }
             
             folderState = "Loaded Sounds Folder: " + dirs[current];    //label that shows current state
         } else {
             yield return null;
             folderState = "No playable audio clips on folder '" + dirs[cur] + "'";
         }        
     }
 }
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Creating a Custom Music folder - Works inside the editor - Issues with build. 0 Answers

Footstep sound does not finish playing, it keeps restarting on movement 0 Answers

Second AudioClip won't play 0 Answers

How to stop current audio before another audio starts? 0 Answers

How to reduce delay when playing sound 0 Answers


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