- Home /
Question by
idioti · Oct 19, 2018 at 06:54 PM ·
audiodownloadstreamingwebrequest
How to stream audio before completely downloaded?
Hi, I am trying to stream mp3 audio from an url. It works well, but I have to wait until audio is downloaded fully before the stream starts. I know that audio could start before the whole download is completed, just don't know how. Here is my code: void Start () { audioSource = GetComponent(); }
// Update is called once per frame
void Update () {
//TESTING
if(Input.GetKeyUp(KeyCode.P)){
StartCoroutine(GetAudio("https://firebasestorage.googleapis.com/v0/b/showstopper-3cd1b.appspot.com/o/Shlijani%20-%20Crna%20Ovca.mp3?alt=media&token=ab5a6ece-da3e-45d2-832a-3c202be7f5be"));
}
}
public IEnumerator GetAudio(string url){
Debug.Log("Get audio");
UnityWebRequest musicRequest = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
yield return musicRequest.SendWebRequest();
if (musicRequest.isNetworkError)
{
Debug.Log(musicRequest.error);
Debug.Log(musicRequest.responseCode);
}
else
{
handler = (DownloadHandlerAudioClip)musicRequest.downloadHandler;
handler.streamAudio = true;
AudioClip clip = handler.audioClip;
Debug.Log(clip + " length: " + clip.length);
if (clip)
{
audioSource.clip = clip;
Debug.Log("Audio clip not null");
audioSource.Play();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to check if DownloadHelperAudioClip still has internet connection for streaming during playback? 0 Answers
DownloadHandlerAudioClip compressed and streamAudio properties 0 Answers
Android can't download the audio 1 Answer
Usage of DownloadHandlerAudioClip.streamAudio 1 Answer
A method for streaming custom audio from the hard drive 0 Answers