- Home /
UnityWebRequestMultimedia to get AudioClip from API
What I'm trying to accomplish sounds fairly easy. I try to download an AudioClip from our own API using an UnityWebRequestMultimedia but the AudioClip which it retrieves seems to be null all the time.
This is the IEnumerator that runs in a Coroutine to load the AudioClip:
private static IEnumerator loadAudio(DownloadJob<AudioClip> job)
{
UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(job.URL, AudioType.MPEG);
yield return webRequest.SendWebRequest();
if (!webRequest.isNetworkError && !webRequest.isHttpError)
{
DownloadHandlerAudioClip dlHandler = (DownloadHandlerAudioClip)webRequest.downloadHandler;
if(dlHandler.isDone)
{
AudioClip audioClip = dlHandler.audioClip;
if(audioClip != null)
{
_audioCacheManager.Add(job.Key, audioClip);
job.Done(audioClip);
}
else
{
job.Failed("Retrieved AudioClip is null.");
}
}
else
{
job.Failed("The download process is not completely finished.");
}
}
else
{
job.Failed(webRequest.error);
}
}
I can't spot any issues with this piece of code and I suspect that the WebRequest does not follow the API's redirect that points it to the audio file directly even though the RedirectLimit has not been changed and should still be on the default value of 32. When the call URL is opened in the browser it'll show an audioplayer without problems.
Any ideas?
Answer by Roywise · Jul 24, 2018 at 11:49 AM
Apparently the WebRequest did get the correct data but Unity does not allow the use of DownloadHandlerAudioClip.audioClip when the audiofile is MP3 format because of the patent ( https://feedback.unity3d.com/suggestions/streaming-mp3-support-now-that-patent-has-expired ).
To work around this I took the raw data ( DataHandlerAudioClip.data ) and used an external library to convert it to WAV data and created an AudioClip with that. ( https://gamedev.stackexchange.com/questions/114885/how-do-i-play-mp3-files-in-unity-standalone )
Answer by RukiPomidory · Feb 25, 2021 at 06:33 PM
if someone still has trouble with it - just upgrade your project to unity 2020.1.14f1 I am not sure, when exactly it was fixed, but in 2020.1.14 loading clip from mp3 is not a problem.
Your answer
Follow this Question
Related Questions
A method for streaming custom audio from the hard drive 0 Answers
Usage of DownloadHandlerAudioClip.streamAudio 1 Answer
How can I load an AudioClip from a local audio file using UnityWebRequests? 1 Answer
How to use Unity Web request Multimedia to download FLAC audio File and convert it to audio clip? 2 Answers