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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Load .ogg Bug Runtime

Discussion in 'Audio & Video' started by NicolasCEXT, Apr 5, 2023.

  1. NicolasCEXT

    NicolasCEXT

    Joined:
    Feb 3, 2022
    Posts:
    3
    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 :

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Text;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.UI;
    8.  
    9. public class TestAudio : MonoBehaviour
    10. {
    11.     [SerializeField] private Button _button;
    12.     [SerializeField] private AudioSource _audioSource;
    13.     // Start is called before the first frame update
    14.     void Awake()
    15.     {
    16.         _button.onClick.AddListener(delegate { Play(); });
    17.     }
    18.  
    19.     private void Start()
    20.     {
    21.         string path = @"Documents\P_00000066_0001001_065_CLICK.ogg";
    22.         StartCoroutine(TestLoadAudio(path));
    23.     }
    24.  
    25.     private void Play()
    26.     {
    27.         _audioSource.Play();
    28.         StartCoroutine(CoroutinePlay());
    29.     }
    30.  
    31.     private IEnumerator CoroutinePlay()
    32.     {
    33.         while (_audioSource.isPlaying)
    34.         {
    35.             yield return null;
    36.         }
    37.     }
    38.  
    39.     private IEnumerator TestLoadAudio(string path)
    40.     {
    41.         using (var uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
    42.         {
    43.             yield return uwr.SendWebRequest();
    44.  
    45.             if (uwr.result == UnityWebRequest.Result.ProtocolError
    46.                 || uwr.result == UnityWebRequest.Result.ConnectionError)
    47.             {
    48.                 yield break;
    49.             }
    50.  
    51.             if (uwr.isDone)
    52.             {
    53.  
    54.                 AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);
    55.                 _audioSource.clip = audioClip;
    56.             }
    57.  
    58.             uwr.Dispose();
    59.         }
    60.     }
    61. }
    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<AudioSource>("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 :)

    Thank you for your reply,
    Have a nice day !
     

    Attached Files:

  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    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.
     
  3. NicolasCEXT

    NicolasCEXT

    Joined:
    Feb 3, 2022
    Posts:
    3
    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 :)


    Have a nice day !

    The update of my code :
    Code (CSharp):
    1.  
    2. if (uwr.isDone)
    3. {
    4.                 AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);
    5.                 AudioClip originaleclip = _audioSource.clip;
    6.                 _audioSource.clip = audioClip;
    7.  
    8.                 Debug.Log($"UnityWebRequest : SAMPLE = {audioClip.samples} " +
    9.                     $"FREQUENCY =  {audioClip.frequency} " +
    10.                     $"LENGHT = {audioClip.length} " +
    11.                     $"STATE = {audioClip.loadState}");
    12.  
    13.                 Debug.Log($"Resources.Load : SAMPLE = {originaleclip.samples} " +
    14.                     $"FREQUENCY =  {originaleclip.frequency} " +
    15.                     $"LENGHT = {originaleclip.length} " +
    16.                     $"STATE = { audioClip.loadState}");
    17.  }
    18.  
     

    Attached Files:

  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    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.
     
  5. NicolasCEXT

    NicolasCEXT

    Joined:
    Feb 3, 2022
    Posts:
    3
    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)
     

    Attached Files:

  6. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    305
    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.