- Home /
How do I properly stream music with WWWAudioExtensions?
Here is my stream code
IEnumerator musicStream(){
using(WWW www = new WWW(Url)){
yield return www;
AudioClip f = WWWAudioExtensions.GetAudioClip(www, true, true, AudioType.MPEG);
AudioSource.clip = f;
}
}
I have been trying to stream music with the GetAudioClip method. Stream is set to true but having the yield function before the stream just basically mean I am waiting for the www to finish loading everything then play the clip.
yield return www
AudioClip f = WWWAudioExtensions.GetAudioClip(www, true, true, AudioType.MPEG);
How do I properly use this, I cannot find any examples online.
Answer by vasupol11 · Aug 25, 2017 at 06:01 AM
To answer my own question again and for those who actually wonder if it is possible to stream music in unity, it is. Here is the code
www = new WWW(url);
while (www.progress < 0.01)
{
Debug.Log(www.progress);
yield return new WaitForSeconds(.1f);
}
// yield return null;
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
}
else
{
clipa = WWWAudioExtensions.GetAudioClip(www, false, true, AudioType.MPEG);
if (clipa.isReadyToPlay)
{
GetComponent<AudioSource>().clip = clipa;
GetComponent<AudioSource>().Play();
}
}
Explanation: the .01 progress gets rid of a weird error that doesn't display anything. This error actually occurs because the file name still hasn't been downloaded. Waiting just till .01 is enough to get that information and allow it to stream. Remember to dispose of www somewhere, I did not include it in this code.
@vasupol11 I'm getting the blank console error when I try to use this code to stream OGGVORBIS. Any ideas why that would be?
Thanks
As I understood OGGVORBIS may not be the right format for your platform: https://docs.unity3d.com/ScriptReference/WWW.GetAudioClip.html
Hey, can you update your answer as WWWAudioExtensions.GetAudioClip is obsolete now.
a deprecated, but not obsolete alternative is www.GetAudioClip(false, true, AudioType.$$anonymous$$PEG). I am testing this for a WebGL build, where networking is not available and i want to stream from indexeddb.