I’m trying to load an mp3 from a URL in WebGL. For some reason it doesn’t seem to work. I’ve checked the downloaded bytes and it seems that it is downloading the data, but the AudioClip it returns has 0 samples. It doesn’t throw any errors, so I’m not quite sure as to why it is failing.
You are using DownloadHandlerAudioClip as in this example?
If that’s not fixing it please share your code.
Yes, I am using it like this:
public class TestAudioDownload : MonoBehaviour {
private const string urlMp3 = "url-to-mp3";
private void Start() {
StartCoroutine(DownloadAudio(urlMp3));
}
private IEnumerator DownloadAudio(string url) {
Debug.Log("Download starting");
using var request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
yield return request.SendWebRequest();
Debug.Log($"Download complete: {request.result}");
if (request.result == UnityWebRequest.Result.Success) {
AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
Debug.Log($"Size: {request.downloadedBytes}");
Debug.Log($"Length: {clip.length}");
Debug.Log($"Samples: {clip.samples}");
}
}
}
The “Size” is correct and shows that the file is being downloaded, but both “Length” and “Samples” are both 0, and playing the audio results in nothing. It works in the Editor but for some reason just doesn’t seem to work on WebGL.
Edit: trying AudioClip.GetData also writes to the console that there are no samples
It doesn’t match the example code exactly.
They manually create a download handler:
var dh = new DownloadHandlerAudioClip($"file://{cacheFileName}", AudioType.MPEG);
and passed into WebRequest as a parameter:
new UnityWebRequest($"file://{cacheFileName}", "GET", dh, null)
I thought Unity’s documentation says that UnityWebRequestMultimedia.GetAudioClip (link) creates a web request that’s already configured for audio clips.
I’ll try to use the constructor you linked but I think behind the scenes it does the same thing, other than setting compressed to true for the created audio clip.
Hi!
on the Web Platform the audio support depends on the audio capabilities of the browser and platform the build is running on. This means that AudioClip.GetData has the following limitations:
- It will only work for audio clips where the Load Type is set to Decompress on Load
- Decoding of the compressed audio data is asynchronous so you will need to use a coroutine and wait for the load state of the clip to be set to loaded(
audioClip.loadState == AudioDataLoadState.Loaded)
Refer to this page for more information: Unity - Scripting API: AudioClip.GetData
The same applies foraudioClip.lengthandaudioClip.samplessince the audio needs to have finished decoding before being able to know the length or number of samples.
So I think you also need to set the following properties on the download handler so the clip is loaded as DecompressOnLoad:
using var request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
var dh = (DownloadHandlerAudioClip)request.downloadHandler;
dh.compressed = false;
dh.streamAudio = false;
yield return request.SendWebRequest();
If you are also not able to play the downloaded audio clip it could be that the browser does not support the audio format/codec used by the audio clip. You can try to convert the file to a different format to resolve this issue.