Getting Audio Clip from WWW URLand saving it in the project.

So, I have gone so far as getting the audio clip from a URL with this:

m_www = new WWW(m_sWebURL);
m_acAudioClip = m_www.GetAudioClip();

Then, I’m saving the newly downloaded Audio Clip to Application.persistentDataPath (I won’t copy the code unless needed as to not bulk up my post)

And it works. And whenever I launch the application again, I don’t have to download the Audio Clip because it’s already stored in memory. So I can just load it from memory and play it.

I need to do this for potentially thousands of Audio Files from the web, and it works just fine except for one small issue.

For some reason, an Audio Clip that when downloaded normally has a 27 megabyte size, turns into a 151 megabyte monster when I use the above procedure.
That big file size is not good, not only because it increases the size of the App, but because mobile devices have trouble loading them from memory.

I don’t know why this happens, because the devices can download it no problem, it’s the loading from memory that’s crashing them due to lack of memory.

Please, any ideas?

You can try getting raw bytes from WWW and save those to disk. That should be the same than what you’ve downloaded.

Hi Aurimas, thanks for the reply! do you have a code sample I can work with?

1 Like

Thanks! I’ll give it a go

Aurimas, sorry to bother you again, I did get the file down to the original size. Now, how do I get that file from disk to an AudioClip.

The info is in bytes and I don’t know how to transform them to AudioClip.

Thanks!

Or anyone?

Using:

WWW clip = new WWW(path)
yield return clip

myAudioclip = clip

Gives me an error that doesn’t show anything on the console. I know the www clip was loaded because it carries info, but for some reason I can’t get it out to an Audio Clip.

Any help is appreciated.

Solved it, not sure why, but I had to load the AUDIO FILE not as a .WAV which it is, but as a .MPEG.

m_acAudioClip = [www.GetAudioClip(false](http://www.GetAudioClip(false), false, AudioType.MPEG);

Here’s the code to load from memory:

 IEnumerator LoadFromMemory()
    {
        m_tmStatus.text = "Trying to load";
        string path = Application.persistentDataPath + "/TESTING.wav";

        WWW www = new WWW("file://"+path);

        if (File.Exists(path))
        {
            while (www.progress < 1)
            {
                Debug.Log(www.progress);
                yield return new WaitForSeconds(0.1f);
            }
            m_tmStatus.text = "Loading: "+www.progress;
            m_acAudioClip = www.GetAudioClip(false, false, AudioType.MPEG);
            m_tmStatus.text = "Done loading";
        }
        else
        {
            m_tmStatus.text = "path null";
        }
    }

And here’s the code to download from a URL and then Save locally on device:

private void LoadFromURL()
    {
        m_www = new WWW(m_sWebURL);
        StartCoroutine("DownloadAndPlayVideo");
    }

    IEnumerator DownloadAndPlayVideo()
    {
        m_tmStatus.text = "Progress: " + m_www.progress;
        yield return new WaitForSeconds(1);

        if (m_www != null && m_www.isDone && m_www.error == null)
        {
            m_acAudioClip = m_www.GetAudioClip();
            print("m_www: " + m_www.bytesDownloaded);
            System.IO.File.WriteAllBytes(Application.persistentDataPath + "/TESTING.wav",m_www.bytes);
        }

        if (!m_www.isDone)
        {
            m_tmStatus.text = "Progress: " + m_www.progress;
            StartCoroutine("DownloadAndPlayVideo");
        }
        else
        {
            m_tmStatus.text = "Load from URL complete";
        }
    }

Hope this helps someone, I was struggling a lot with it.

1 Like