- Home /
Usage of DownloadHandlerAudioClip.streamAudio
Hi all,
I am trying to start playing an AudioClip before it has been completely download as hinted at in the documentation for DownloadHandlerAudioClip.streamAudio. I have written this sample code to illustrate the use I would expect to work:
private IEnumerator StartAudioStream()
{
// Prepare stream
string url = "www.brainybetty.com/FacebookFans/Feb112010/ChillingMusic.wav";
DownloadHandlerAudioClip downloadHandler = new DownloadHandlerAudioClip(string.Empty, AudioType.WAV);
downloadHandler.streamAudio = true;
UnityWebRequest request = new UnityWebRequest(url, "GET", downloadHandler, null);
// Start stream
UnityWebRequestAsyncOperation token = request.SendWebRequest();
AudioClip audioClip = null;
while (audioClip == null) // Ensure audio header completed
{
try
{
audioClip = DownloadHandlerAudioClip.GetContent(request);
}
catch (Exception) { }
Debug.LogFormat("Waiting for AudioClip: bytes={0}", request.downloadedBytes); // The complete file is 4834942 bytes
yield return 1f;
}
// Play AudioClip
source.clip = audioClip;
source.Play();
yield return 0;
}
Thank you in advance for your time.
No. So far I have to wait for the AudioClip to finish downloading before adding audio to the player.
Answer by anileapen05 · Jul 06, 2019 at 06:20 PM
private IEnumerator LoadAudio(string audioName, int track_id, float start_time)
{
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(
musicPath.Replace("#","%23") +
//musicPath +
audioName, AudioType.OGGVORBIS))
{
var synRes = www.SendWebRequest();
yield return synRes;
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
stream_music = DownloadHandlerAudioClip.GetContent(www);
musicSource.clip = stream_music;
musicSource.time = start_time;
current_track_num = track_id;
current_track = GetCurrentTrack();
musicSource.Play();
}
}
}
Your answer
Follow this Question
Related Questions
A method for streaming custom audio from the hard drive 0 Answers
AudioClip "Stream from disc" does not work in standalone apps 0 Answers
How to have www streaming to work with a radio station? 1 Answer
How to avoid lag when using WWW class to download sound 3 Answers
Is There A way For unity to Determine If Audio Was Recorded Backwards 1 Answer