Search code examples
c#unity-game-engineunity-webgl

Audiosource.GetOutputData() not working in Unity WebGL build


public Text data;
string url = "http://localhost/AudioVisualizer/Spiderman.wav";
float[] samples = new float[1024];
public AudioSource source;
public GameObject L,R;
WWW www;
 IEnumerator Start()
{
    www = new WWW(url);
    yield return www;
    source = GetComponent<AudioSource>();
    source.clip = www.GetAudioClip();
}
void Update()
{
    source.GetOutputData(samples, 1);
    data.text = samples[0].ToString();

    L.transform.localScale = new Vector3(1,Mathf.Abs(samples[0]*2), 1);
    R.transform.localScale = new Vector3(1, Mathf.Abs(samples[1]*2), 1);

    if (!source.isPlaying)
        source.Play();
}

When I run the above code in editor it runs just fine. But when I take the WebGL build there is no output data.

data.text = samples[0].ToString();

this line gives 0 as output.


Solution

  • According to the doc, GetOutputData is not supported for WebGL. This is because WebGL does not support threads.There are many other none supported audio features due to this. Only basic audio functions are supported.

    You will have to manually process the AudioClip yourself. I suggest you check this project out which is GetSpectrumData implementation for WebGL. That will help you implement GetOutputData for WebGL.