Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Download OGG in WebGL get Length and play?

Discussion in 'Audio & Video' started by battou, Mar 21, 2023.

  1. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    197
    I don understand why there is a probjem with downloading OGG files using webrequests in WebGL?

    If i use appropriate OGGVORBIS format then I get "Streaming of 'ogg' on this platform is not supported" error. Though Unity itself converts audio to Vorbis for WebGL builds.

    And the second problem is even if Im able to download and play ogg via AUDIOQUEUE type or mp3 - I always get Length 0, and I need it for a slider.

    Is there any proper way to download ogg, get its length and play in WebGL builds?
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    290
    Unity converts to MP4/AAC when WebGL is targeted.
    Streaming compressed files or hot-loading compressed formats is not the strong suit of the WebGL audio stack sadly. In this scenario the audio must always be uncompressed for streaming, so either stored uncompressed (PCM) or decompressed on load.

    The 0 length thing is surprising though... maybe it's a symptom of downloading a file, which might bypass the internal decoding that would load this information. If this issue persists could you provide a minimal project isolating this issue and submit a bug report with it? It's the official (and fastest!) way to process bugs.

    Thanks!
     
  3. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    197
    So, I managet to make it work for now. Here is the code. The secret ingredient was to await till audio_clip's loadState is Loaded after download web request is complete. Now I get Length, though Frequency and samples are differ from real files parameters. And I still need to use AUDIOQUEUE as file type to download OGG.

    Code (CSharp):
    1. var audio_download_handler = new DownloadHandlerAudioClip(string.Empty, audio_type)
    2.             {
    3.                 streamAudio = is_stream_audio,
    4.                 compressed = is_compressed_audio
    5.             };
    6.          
    7.             using var file_request = new UnityWebRequest(url, "GET")
    8.             {
    9.                 downloadHandler = audio_download_handler
    10.             };
    11.  
    12.             try
    13.             {
    14.                 using var audio_download = await file_request.SendWebRequest();
    15.  
    16.                 var audio_clip = audio_download_handler.audioClip;
    17.              
    18.              
    19.              
    20.                 if (audio_clip != null) audio_clip.name = Path.GetFileName(url);
    21.  
    22.                 await UniTask.WaitUntil(() => audio_clip.loadState == AudioDataLoadState.Loaded);
    23.              
    24.                 return audio_clip;
    25.             }
    And yes, its indeed AAC Unity converts to, my bad. But this case proves that WebGL CAN download and play OGG files. I dont need streaming play, only download and then play. Hope this can be implemented cleanely some day.
     
    Last edited: Mar 22, 2023
    SeventhString likes this.