Load Music files with foreach and WWW

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() ) :

 IEnumerator LoadFile(string path)  // Charge les fichiers
     {
       
       
         // get all valid files
         var info = new DirectoryInfo(path);
         NombredeFichiers = DirCount(info);
         Debug.Log("Nombre de Musiques détectées : "+ NombredeFichiers);
         soundFiles = info.GetFiles()
             .Where(f => IsValidFileType(f.Name))
             .ToArray();

         Debug.Log("Nombres de chemins de musiques dans le soundFiles:" + soundFiles.Count());
      
      
         foreach (var s in soundFiles)

// I meet a problem with the foreach and the www combined
         
       WWW www = new WWW(s.FullName);
        // and load them
   

         print("loading " + path);

       

         AudioClip clip = www.GetAudioClip(true);
         while(!clip.isReadyToPlay)
             yield return www;               
         print("done loading");
         clip.name = Path.GetFileName(path);
         clips.Add(clip);

         Debug.Log("Nombre de Musiques APRES : " + clips.Count);

         DirectoryInfo dir = new DirectoryInfo(path);
         FileInfo[] info2 = dir.GetFiles("*.*");

    
      

     }

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)

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 :slight_smile:

Thank you a lot!

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;

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.

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 :slight_smile:

Pretty sure you just have to prepend the path with the “file://” protocol instead of messing around with System.Uri

@KelsoMRK You think i’ve just to : WWW www =new WWW(file:// path Of s); ?

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).

/ works on every standalone platform as a separator. Linux and Mac use it explicitly and Windows doesn’t care.

I tried this but it doesn’t work :frowning:

I think the problem come from the “File:///s”) … I don’t know how to write it .

 foreach (var s in soundFiles)
         {
             WWW www = new WWW(File:///s.ToString());
             AudioClip clip = www.GetAudioClip(true);
             while (!clip.isReadyToPlay)
                 yield return www;
             print("done loading");
             clip.name = Path.GetFileName(path);
             clips.Add(clip);
         }

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 :

IEnumerator LoadFile(string path)  // Charge les fichiers
     {

       

         // get all valid files
         var info = new DirectoryInfo(path);
         NombredeFichiers = DirCount(info);
         Debug.Log("Nombre de Musiques détectées : "+ NombredeFichiers);
         soundFiles = info.GetFiles()
             .Where(f => IsValidFileType(f.Name))
             .ToArray();

         Debug.Log("Nombres de chemins de musiques dans le soundFiles:" + soundFiles.Count());
       

         foreach (var s in soundFiles)
         {
           
             WWW www = new WWW("file://"+s);
             AudioClip clip = www.GetAudioClip(true);
             while (!clip.isReadyToPlay)
                 yield return www;
             print("done loading");
             clip.name = Path.GetFileName(path);
             clips.Add(clip);
           
             }

Little Up :slight_smile:

No - nothing is going to make your files load off the disk faster

1 Like

There is… change it to SSD :stuck_out_tongue:

3 Likes

Okay guys ! Thank you a lot for all your tips :slight_smile: I’d have failed without your helps :slight_smile: