Load .ogg Bug Runtime

Hello everyone,

I experienced some issue on Unity 2021.3.5f1.
I tried to load at runtime an audio file (in attachment), with this code :

using System;
using System.Collections;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class TestAudio : MonoBehaviour
{
    [SerializeField] private Button _button;
    [SerializeField] private AudioSource _audioSource;
    // Start is called before the first frame update
    void Awake()
    {
        _button.onClick.AddListener(delegate { Play(); });
    }

    private void Start()
    {
        string path = @"Documents\P_00000066_0001001_065_CLICK.ogg";
        StartCoroutine(TestLoadAudio(path));
    }

    private void Play()
    {
        _audioSource.Play();
        StartCoroutine(CoroutinePlay());
    }

    private IEnumerator CoroutinePlay()
    {
        while (_audioSource.isPlaying)
        {
            yield return null;
        }
    }

    private IEnumerator TestLoadAudio(string path)
    {
        using (var uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
        {
            yield return uwr.SendWebRequest();

            if (uwr.result == UnityWebRequest.Result.ProtocolError
                || uwr.result == UnityWebRequest.Result.ConnectionError)
            {
                yield break;
            }

            if (uwr.isDone)
            {

                AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);
                _audioSource.clip = audioClip;
            }

            uwr.Dispose();
        }
    }
}

But when I play it not all song is played (we have to heard 3 “ticks” but only 2 are played).

I realized if i directely put the song on the audioSource or load it with Ressource.Load(“mysong”); it’s playing well (I heard 3 “ticks”).

When I compare size of these 2 audioSource I get :

AudioSource loaded by UnityWebRequest : Lenght = 0.516
AudioSource loaded by Ressources.Load : Lenght = 0,5768254

And the last tick is at the end of the song.

So, if somebody has an explanation or a solution, I will take it :slight_smile:

Thank you for your reply,
Have a nice day !

8928503–1223651–TestSong.zip (4.2 KB)
8928503--1223690--Result.png

Have you tried checking the loadState of the audioClip before playing it?

In general, I would not recommend calling AudioSource.Play before the AudioClip is completely loaded, especially if you’re handling the asset data manually.

Hello thanks for your reply.
Yes i agree with you. I didn’t test it.
I tried just now with your update and i get the state : “loaded” for this audioSource.

For now, we have found a temporary solution : it’s to add a some time at the end of the sounds file to be sure everything it’s played. It works but it’s not solution in time. So, if you have some other ideas, I will take it :slight_smile:

Have a nice day !

The update of my code :

if (uwr.isDone)
{
                AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);
                AudioClip originaleclip = _audioSource.clip;
                _audioSource.clip = audioClip;

                Debug.Log($"UnityWebRequest : SAMPLE = {audioClip.samples} " +
                    $"FREQUENCY =  {audioClip.frequency} " +
                    $"LENGHT = {audioClip.length} " +
                    $"STATE = {audioClip.loadState}");

                Debug.Log($"Resources.Load : SAMPLE = {originaleclip.samples} " +
                    $"FREQUENCY =  {originaleclip.frequency} " +
                    $"LENGHT = {originaleclip.length} " +
                    $"STATE = { audioClip.loadState}");
 }

What is the original format of your song? In the file you provided I only have 2 clicks, I would be interesting to have a look at the original file and the AudioClip properties. Maybe a sample rate conversion or something with the compression setting is wrong.

The original format of the song is a mp3 file.
It’s another software that convert this file to an .ogg.
We realize if we open this sound with VLC Media Player, we got ony 2 Clicks, but if we open it with some other software more efficient for sounds treatment (as Audacity for example) we got 3 clics :

So maybe the export of the .ogg file needs some parameters I don’t know, but it’s weird to have the 3 clicks with some software and the differents way of loading in Unity (Ressources.Load or WebRequest behavior is different)

8943690–1227048–P_00000066_0001001_065_CLICK.zip (8.95 KB)

If VLC has trouble with your file, I’d tend to think there is something wrong with the file… it’s quite a solid one!

Have you tried loading it in Unity as MP3 and let it handle internally the ogg conversion? I think you could still do your webrequest the same way since interally it will be encoded to Vorbis/Ogg.