Search Unity

Why audio isn't playing when downloaded ?

Discussion in 'Web' started by stighy, May 15, 2019.

  1. stighy

    stighy

    Joined:
    Mar 19, 2016
    Posts:
    7
    Hello. I'm trying to download at runtime, various audio file format : Mp3 , Ogg, Wav but I can't play it at runtime.

    This is my code:
    WWW data = new WWW(audioURL); yield return data;
    AudioClip downloadedClip = data.GetAudioClipCompressed(false, AudioType.OGGVORBIS) as AudioClip;
    if (downloadedClip != null)
    {
    _audio.clip = downloadedClip;
    }
    ...

    This code to play Audio:

    public void PlayAudio() {
    // Here clip is play
    if (_audio.clip != null && _audio.isPlaying == false)
    _audio.Play();
    else
    Debug.Log("Background music not present!");
    _audio.loop = true;
    }

    I've also try to download Audio using this code:

    using (UnityWebRequest www2 = UnityWebRequestMultimedia.GetAudioClip(audioURL, AudioType.OGGVORBIS))
    {
    yield return www2.SendWebRequest();

    if (www2.isHttpError)
    {
    Debug.Log(www2.error);
    LogAdd(www.error, true);
    }
    else
    {
    AudioClip downloadedClip = DownloadHandlerAudioClip.GetContent(www2);
    _audio.clip = downloadedClip;
    }
    }

    I can't use AssetBundle because my web app is interactive: user can upload file (audio) then they will be reproduced later.

    Can you help me ?

    Thanks
     
  2. stighy

    stighy

    Joined:
    Mar 19, 2016
    Posts:
    7
    EDIT: When uploading Mp3 error is: Streaming of 'mpeg' on this platform is not supported UnityEngine.Networking.DownloadHandlerAudioClip:GetContent(UnityWebRequest)
     
  3. stighy

    stighy

    Joined:
    Mar 19, 2016
    Posts:
    7
    EDIT: no way to make it works on WebGL/browser. Using Editor it seems to work (specially with OGG). But the same code, same build, not works on webgl...
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    If you want to download music and stream it, do it in your index.html. You will need to `play` inside a click handler.