Unity Stream Audio From URL or Unity Load Audio Clip from File

Learn how to load audio clip from a file in unity 3d and how to stream audio from URL in unity. This works well when you have a lot of sounds in your app.

Other tutorials

Don’t want to do it yourself?

Hire me on Fiverr and I’ll work for you at $25 per hour.

Unity Stream Audio From URL

Let’s jump right into it. It happens a lot that you have to load multiple audio clips inside your unity game or app. Or even if you want to use a placeholder clip URL to change the pointing audio clip later on? You are at the right place. Let me show you how you can do that.

Create a UnityWebRequest to download an audio clip via HTTP GET and create an AudioClip based on the retrieved data.

This method creates a UnityWebRequest and sets the target URL to the string URI argument. This method sets no other flags or custom headers.

This method attaches a DownloadHandlerAudioClip object to the UnityWebRequest. DownloadHandlerAudioClip is a specialized DownloadHandler. It is optimized for storing data which is to be used as an audio clip in the Unity Engine. Using this class significantly reduces memory reallocation compared to downloading raw bytes and creating an audio clip manually in script.

This method attaches no UploadHandler to the UnityWebRequest.

Learn to Stream Audio From URL

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class MyBehaviour : MonoBehaviour
{
    public AudioSource audioSource;
    AudioClip myClip;
    void Start()
    {
        StartCoroutine(GetAudioClip());
    }

    IEnumerator GetAudioClip()
    {
        using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("http://www.my-server.com/audio.ogg", AudioType.OGGVORBIS))
        {
            yield return www.Send();

            if (www.isError)
            {
                Debug.Log(www.error);
            }
            else
            {
                myClip = DownloadHandlerAudioClip.GetContent(www);
                audioSource.clip = myClip;
                audioSource.Play();
            }
        }
    }
}

Now this code simply, get you the audio clip downloaded in your myClip variable. Next, you simply assign it to your AudioSource and Play it. Make sure to assign the AudioSource component in your script.

Unity Load Audio Clip from File

The next one is how to load an audio clip from your local file system. There will be many scenarios when you have to load an audio clip from a file in your unity 3d game or app. It is much similar to the one above but for this we are going to use some old WWW code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sound : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(LoadAudio());
    }

    public AudioSource audioContainer;
    IEnumerator LoadAudio()
    {
        string FullPath = "C:/AudioFiles/audio1.wav";
        FullPath = "file:///" + FullPath;
        WWW URL = new WWW(FullPath);
        yield return URL;

        audioContainer.clip = URL.GetAudioClip(false, true);
        audioContainer.Play();
    }

}

Thanks for reading 🙂

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest