Can we stream audio from a URL instead of download and then play?
I am working on a unity app where a sound clip from url should be played, but in current implementation of new unity WWW it first gets download before playing. But the sound clip is bit long and needs to play almost in real time. anyone can help me in this regards? I am using unity 2017.
My Code is:
wwwSound = UnityWebRequestMultimedia.GetAudioClip( SoundURL, AudioType.MPEG);
yield return wwwSound.SendWebRequest();
if (wwwSound.isNetworkError)
{
Debug.Log (wwwSound.error);
}
else
{
Debug.Log ("Sound Received at: " + SoundURL);
AudioClip ac = ((DownloadHandlerAudioClip)wwwSound.downloadHandler).audioClip;
}
Answer by CrashZeon · Jan 25, 2019 at 08:24 PM
I got this to work using the following code. Keep in mind that this is basic and isn't the most efficient way to do this nor does it dispose of the handlers once completed.
IEnumerator SongCoroutine(string path, string file)
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(URLpath, AudioType.MPEG))
{
DownloadHandlerAudioClip dHA = new DownloadHandlerAudioClip(string.Empty, AudioType.MPEG);
dHA.streamAudio = true;
www.downloadHandler = dHA;
www.SendWebRequest();
while (www.downloadProgress < 0.01)
{
Debug.Log(www.downloadProgress);
yield return new WaitForSeconds(.1f);
}
if (www.isNetworkError)
{
Debug.Log("error");
}
else
{
musicOne.clip = dHA.audioClip;
_m1SongTime = musicOne.clip.length + Time.time;
}
}
}
Hey! the solution seems to work but with a wierd error or atleast for me. The audioclip starts playing but after awhile the sound stops and I get static. I have tried it both on iOS and in the editor.
$$anonymous$$aybe it is the audioformat ? currently im just testing with a .wav file that is hosted on amazon cloudfront.
Do you also experience this?