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
Thank you for your reply,
Have a nice day !
8928503–1223651–TestSong.zip (4.2 KB)