Search Unity

Better way to load local assets than WWW?

Discussion in 'Editor & General Support' started by FPSyndicate, Apr 19, 2013.

  1. FPSyndicate

    FPSyndicate

    Joined:
    Mar 31, 2011
    Posts:
    39
    So I made this pretty cool looking visualizer that reads any music file and, well, visualizes the music. I just added in the ability to load local files from your computer/smartphone.

    $Capture.PNG

    I am currently using the WWW class to load the music, but it seems to load 99% of the file really fast, then spend a few seconds (nearly a minute on my Android) to actually start playing the music. My code for loading the music is below:

    Code (csharp):
    1.  
    2. function GetTrack (track : int) {
    3.     www = new WWW ("file://"+ audioClips[track]);
    4.     trackName = audioClips[track];
    5.    
    6.    
    7.     if(www.isDone){
    8.         var currClip = www.audioClip;
    9.         var Audio = gameObject.GetComponent(AudioSource);
    10.         Audio.clip = currClip;
    11.         audio.Play();
    12.    }
    13. }
    14.  
    So what I'm asking is: Is there a better/faster way to load local files, or is there a way to start the music playing before the "download" finishes?

    I've tried playing the audio when
    Code (csharp):
    1. www.progress == 0.5;
    for example, but then no audio played. I can't seem to figure this one out. thanks for the help!

    [EDIT] These are not assets that are downloaded with the app, they are literally any *.mp3 file on your device, so i can't load an asset bundle, for example.

    (BTW, there's a working example on my website if you want to check it out, but it only plays one track since it hasn't been updated with the new code yet >> http://www.fpsyndicate.com/visualizer)

    Thanks!
     
    Last edited: Apr 19, 2013
  2. Cripplette

    Cripplette

    Joined:
    Sep 18, 2012
    Posts:
    61
    Can't help you but i just wanted to say that your app is funny, i like it :)
     
  3. Sammael

    Sammael

    Joined:
    Dec 11, 2012
    Posts:
    22
    try to use a C# File Stream (from System.IO) to read the files.
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,478
    Have you looked at Resources.Load?
     
  5. FPSyndicate

    FPSyndicate

    Joined:
    Mar 31, 2011
    Posts:
    39
    That looks really promising, but i can't seem to figure out how to convert the Stream into a GameObject/asset. Any hints?

    I looked into this, but as far as i can tell, it will only load an asset that is within the Assets folder of your project. Is there a way to hack this to get it to read any file? I'm trying to read audio files from anywhere on a person's computer/smartphone.
     
unityunity