Load external OGG file into Unity WebGL build at runtime

2.1k Views Asked by At

I am trying to load some sounds in OGG format into my game at runtime in a WebGL build. I use a WWW class to fetch the file which has an ".ogg" extension and then I call www.audioClip to get the downloaded file. This works on other platforms but fails in WebGL.

Unity throws up this error message: "Streaming of 'ogg' on this platform is not supported". Strange since I am not trying to stream it, and I have tried explicitly calling GetAudioClip(false, false, AudioType.OGGVORBIS) and got the same result.

I have tried converting my OGG file to AAC (with M4A and MP4 extensions) and loading this with www.audioClip (error that it cannot determine the file type from the URL) and www.GetAudioClip(false, false, AudioType.MPEG) (no error but also no sound). The closest thing to a solution I've seen online is to use MP3 instead but I don't want to do this for licensing reasons.

Is WebGL in Unity restricted to audio assets that are build into the application?

1

There are 1 best solutions below

0
On

try:

WWW data = new WWW (url); yield return data;
AudioClip ac = data.GetAudioClipCompressed(false, AudioType.AUDIOQUEUE) as AudioClip;
if(ac != null)
{
    ac.name = "mySoundFile.ogg";
    gameObject.GetComponent<AudioSource> ().clip = ac;
}
else
{
    gameObject.GetComponent<AudioSource> ().clip = null;
    Debug.Log("no audio found.");
}

works for me with .ogg files.