Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Download and save www......mp3 file

Discussion in 'Scripting' started by Tarzan111, Jun 7, 2016.

  1. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
    Hi, i'm developing a mobile application (iOS/Android) with some long mp3 and wav files (>10 min).
    I'd like to put the mp3 file on a server and at the first app opening, the user can download the songs via www features.

    Code (CSharp):
    1. WWW www = new WWW(url);
    2. yield return www;
    3. What to do with www.audioClip???
    But i don't know how to save the .mp3 file into the app to avoid downloading the song everytime, and how to load it later.

    Thanks
    Leo
     
    Last edited: Jun 7, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Trodith likes this.
  3. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    sure...

    Are you doing this so that the mp3 gets cached so the next time you go to get it, you don't have to dl it again?

    Note that AssetBundles can be downloaded and cached, which will do this for you (you'd distribute your mp3's in an AssetBundle). They're nice because you can version AssetBundles as well so you can flag mandatory updates to the AssetBundle through the AssetBundle version. See:
    https://docs.unity3d.com/ScriptReference/WWW.LoadFromCacheOrDownload.html
     
  5. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
    Yes exactly. I'll work on it tomorow.
    Thanks for the advice lordofduct
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    I would assume you can do this with asset bundles as well. Cache the bundle and load it.
     
  7. Tarzan111

    Tarzan111

    Joined:
    Oct 8, 2014
    Posts:
    72
    I tried something like that :
    Code (CSharp):
    1. public AudioClip audiolist;
    2.  
    3. IEnumerator getsounds () {
    4. while (!Caching.ready) yield return null;
    5.         var www = WWW.LoadFromCacheOrDownload("http://www.myurl.com/mysound.wav", 5);
    6.         yield return www;
    7.         if(!string.IsNullOrEmpty(www.error))
    8.         {
    9.             Debug.Log(www.error);
    10.             //return;
    11.         }
    12.         var myLoadedAssetBundle = www.assetBundle;
    13.         var asset = myLoadedAssetBundle.mainAsset;
    14.         audiolist=asset.audioClip;
    15.         Debug.Log("Load over");
    But it doesn't work, not sure how tu use "assetBundle" to get the audioclip.


    I also tried write and readallbyte.
    Code (CSharp):
    1. WWW www = new WWW("http://www.myurl.com/mysound.wav");
    2.             yield return www;
    3.             audiolist=www.audioClip;
    4.             Debug.Log("Download over");
    5.             System.IO.File.WriteAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav", www.bytes);
    Working well, i can see my file into my Resources folder.

    Then when i'm trying to load it :
    Code (CSharp):
    1. byte[] levelData = System.IO.File.ReadAllBytes(Application.dataPath + "/../Assets/Ressources/audio1.wav");//full local save file
    2.             float[] f = ConvertByteToFloat(levelData);
    3.             audiolist=AudioClip.Create("testSound", f.Length, 1, 44100, false, false);
    4.                audiolist.GetData(f, 0);
    Function ConvertBytoFloat :
    Code (CSharp):
    1. private float[] ConvertByteToFloat(byte[] array)
    2.             {
    3.                 float[] floatArr = new float[array.Length / 4];
    4.                 for (int i = 0; i < floatArr.Length; i++)
    5.                 {
    6.                     if (BitConverter.IsLittleEndian)
    7.                         Array.Reverse(array, i * 4, 4);
    8.                     floatArr[i] = BitConverter.ToSingle(array, i * 4);
    9.                 }
    10.                 return floatArr;
    11.             }
    Using unity 5.3 Free
     
    Last edited: Jun 8, 2016
  8. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    To read the audio from cache use following snippet
    Code (CSharp):
    1.  
    2.             WWW www = new WWW("file:///" + [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Application']Application[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=dataPath']dataPath[/URL] + "/../Assets/Ressources/audio1.wav");
    3.             yield return www;
    4.             audiolist=www.audioClip;
    5.             Debug.Log("Loading audio over");
    6.  
     
  9. schashm3

    schashm3

    Joined:
    Oct 9, 2018
    Posts:
    10
    You do not have to write so much stories...Unity solved this...just get your audio from UnityWebRiquest.

    Code (CSharp):
    1. IEnumerator GetAudioClip()
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("http://www.my-server.com/audio.ogg", AudioType.OGGVORBIS))
    4.         {
    5.             yield return www.Send();
    6.  
    7.             if (www.isError)
    8.             {
    9.                 Debug.Log(www.error);
    10.             }
    11.             else
    12.             {
    13.                 AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);
    14.             }
    15.         }
    16.     }
     
    Last edited: Apr 28, 2019
  10. schashm3

    schashm3

    Joined:
    Oct 9, 2018
    Posts:
    10
    and write this replace DownloadOrLoadFromCash...this is obsolete...
    Code (CSharp):
    1. if(!System.IO.File.Exist("path")){
    2. //downloadAudio()
    3. }else{
    4. StartCoroutine(GetAudioClip());
    5. }