Search Unity

streaming local sound files on iOS

Discussion in 'Scripting' started by smallfly, Feb 6, 2013.

  1. smallfly

    smallfly

    Joined:
    Feb 3, 2013
    Posts:
    14
    Hi,

    My application/project will include sounds tracks, I would like to avoid to pre-load them into memory when the application starts. How can I 'streaming' them from a resources folder (StreamAssets folder ?) when I need them?

    I was not able to find clear answers. Well I found that thread - http://forum.unity3d.com/threads/35654-Source-for-iphone-streaming-audio-player - but the link is broken and the thread is kinda old.

    Any help is appreciated.

    Thanks.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Do you use Resources.Load()? Also, I believe you can set an audio file to compress in memory or stream from disk.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Streaming from StreamingAssets would in C# be something like

    Code (csharp):
    1.  
    2. var request = new WWW("file://"+Application.dataPath + "/Raw/yourfilename.mp3");
    3. yield return request;
    4. // use request.audio
    5.  
     
  4. smallfly

    smallfly

    Joined:
    Feb 3, 2013
    Posts:
    14
    Thanks both of you for the replies.

    I checked the Unity Scripting reference for both, and I not sure wich one to use.
    Does one of the way of doing this has advantages over the other?

    Thanks
     
    Last edited: Feb 7, 2013
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Dreamora's method uses a www agent, so I am assuming it is taken from the web. (?) I am not sure, if true, how useful that is. What if you do not have web access? However, if true, you would not need to include the audio file in your build, thus freeing up memory size.
     
    Last edited: Feb 7, 2013
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    WWW can load off the disk - which is exactly what Dreamora's example does. (file:// protocol)
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    And is it better for performance Vs Resources.Load()?
     
  8. smallfly

    smallfly

    Joined:
    Feb 3, 2013
    Posts:
    14
    I have been able to make it work with WWW request

    Here is my method:

    Code (csharp):
    1.  
    2.     private IEnumerator loadAndPlayAudioClip() {
    3.         string path = "file://"+Application.dataPath + "/Raw/Audio/";
    4.         string fileName = "street-voices.wav";
    5.                
    6.         // Start a download of the given URL
    7.         WWW request = new WWW(path + fileName);
    8.        
    9.         // Wait for download to complete
    10.         yield return request;
    11.        
    12.         // use request.audio       
    13.         AudioClip audioTrack = request.GetAudioClip(false, true);
    14.         audio.clip = audioTrack;
    15.         audio.Play();
    16.     }
    17.  
    But I too would like to know is there is a difference in performance (cpu and memory) usage vs Resources.Load().