Search Unity

Load Music files with foreach and WWW

Discussion in 'Scripting' started by dernat71, Apr 21, 2015.

  1. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Hi guys !

    I'd like to load some audio files one by one ( better for FPS ) with coroutines but i meet a problem.

    I don't know how to mess with the class and the foreach command .. I'd like the WWW load for each (s) element in SoundFiles ...

    Can anyone help me ?

    Thanks a lot in advance !

    Here's my fonction ( i put an absolutepath for argument in the Awake() ) :

    Code (CSharp):
    1.  IEnumerator LoadFile(string path)  // Charge les fichiers
    2.      {
    3.        
    4.        
    5.          // get all valid files
    6.          var info = new DirectoryInfo(path);
    7.          NombredeFichiers = DirCount(info);
    8.          Debug.Log("Nombre de Musiques détectées : "+ NombredeFichiers);
    9.          soundFiles = info.GetFiles()
    10.              .Where(f => IsValidFileType(f.Name))
    11.              .ToArray();
    12.  
    13.          Debug.Log("Nombres de chemins de musiques dans le soundFiles:" + soundFiles.Count());
    14.      
    15.      
    16.          foreach (var s in soundFiles)
    17.  
    18. // I meet a problem with the foreach and the www combined
    19.          
    20.        WWW www = new WWW(s.FullName);
    21.         // and load them
    22.    
    23.  
    24.          print("loading " + path);
    25.  
    26.        
    27.  
    28.          AudioClip clip = www.GetAudioClip(true);
    29.          while(!clip.isReadyToPlay)
    30.              yield return www;              
    31.          print("done loading");
    32.          clip.name = Path.GetFileName(path);
    33.          clips.Add(clip);
    34.  
    35.          Debug.Log("Nombre de Musiques APRES : " + clips.Count);
    36.  
    37.          DirectoryInfo dir = new DirectoryInfo(path);
    38.          FileInfo[] info2 = dir.GetFiles("*.*");
    39.  
    40.    
    41.      
    42.  
    43.      }
     
    Last edited: Apr 21, 2015
  2. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Personally, I don't think its a good idea to pass in the full path into the WWW class.

    The WWW class accepts in uri. You probably need to convert it into a uri format using System.Uri.

    And there is the other option of having your own audio file reader to generate the samples and then creating the AudioClip objects during runtime. (To get past restrictions on audio formats for streaming audio files)
     
  3. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Hi pws devs !

    Could you give me another option for loading my musics? Or just the way to adapt the code with Uri restriction? I really want to have a viable prototype.. It shouldn't be a long-time solution :)

    Thank you a lot!
     
    Last edited: Apr 21, 2015
  4. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Have it to convert every "s" to Uri and stock it?
    Like

    foreach(var s in soundfiles)
    var uri =new System.Uri(s);
    var converted = uri.AbsoluteUri;

    ....
     
  5. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Yes.

    Trust me. You don't want to see my other option. It is a crazy stunt due to crazy requirements. Writing your own sound file reader is not pretty.
     
  6. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Holy crap i can't imagine in fact ! o_O Thank you for your help ! I will try to convert every informations of my Soundfiles container in Uri and then use it with WWW Class :)
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Pretty sure you just have to prepend the path with the "file://" protocol instead of messing around with System.Uri
     
  8. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    @KelsoMRK You think i've just to : WWW www =new WWW(file:// path Of s); ?
     
  9. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Actually, use System.Uri instead. For all you know, not all filesystems use '/' as path seperators.

    Let System.Uri handle the conversion for you. Try not to convert it manually unless System.Uri can't handle it(I'm pretty sure most cases are handled).
     
    Last edited: Apr 23, 2015
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    / works on every standalone platform as a separator. Linux and Mac use it explicitly and Windows doesn't care.
     
  11. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    I tried this but it doesn't work :(

    I think the problem come from the "File:///s") ... I don't know how to write it .

    Code (CSharp):
    1.  foreach (var s in soundFiles)
    2.          {
    3.              WWW www = new WWW(File:///s.ToString());
    4.              AudioClip clip = www.GetAudioClip(true);
    5.              while (!clip.isReadyToPlay)
    6.                  yield return www;
    7.              print("done loading");
    8.              clip.name = Path.GetFileName(path);
    9.              clips.Add(clip);
    10.          }
     
  12. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    HEEEY GOT IT TO WORK !

    But it's again a bit slow ... Is it possible to fix priority of the couroutines ? Edit : Okay just saw that's not possible to set priority of coroutines cause that's not thread .. A way should be to yield more often .. Is it possible here ? Have you an option to fast up my loading ?

    Anyway , Thank you guys for everything you've done until now !

    My solution :
    Code (CSharp):
    1. IEnumerator LoadFile(string path)  // Charge les fichiers
    2.      {
    3.  
    4.        
    5.  
    6.          // get all valid files
    7.          var info = new DirectoryInfo(path);
    8.          NombredeFichiers = DirCount(info);
    9.          Debug.Log("Nombre de Musiques détectées : "+ NombredeFichiers);
    10.          soundFiles = info.GetFiles()
    11.              .Where(f => IsValidFileType(f.Name))
    12.              .ToArray();
    13.  
    14.          Debug.Log("Nombres de chemins de musiques dans le soundFiles:" + soundFiles.Count());
    15.        
    16.  
    17.          foreach (var s in soundFiles)
    18.          {
    19.            
    20.              WWW www = new WWW("file://"+s);
    21.              AudioClip clip = www.GetAudioClip(true);
    22.              while (!clip.isReadyToPlay)
    23.                  yield return www;
    24.              print("done loading");
    25.              clip.name = Path.GetFileName(path);
    26.              clips.Add(clip);
    27.            
    28.              }
     
    Last edited: Apr 25, 2015
  13. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Little Up :)
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No - nothing is going to make your files load off the disk faster
     
    dernat71 likes this.
  15. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    There is... change it to SSD :p
     
    ZO5KmUG6R, aihodge and dernat71 like this.
  16. dernat71

    dernat71

    Joined:
    Jul 7, 2014
    Posts:
    14
    Okay guys ! Thank you a lot for all your tips :) I'd have failed without your helps :)