Hi. I’m new to Unity and looking for some help.
I am saving system.io.stream as a mp3 file and trying to play the audio right after. The code runs fine with no error audio does not play. I think it is being triggered to play before the download is complete / object is created. I’m able to run it fine with a file that already exist.
using (var fileStream = File.Create(audioPath))
{
audioAPIResponse.AudioStream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
var playClip = Resources.Load<AudioClip>(filename);
SoundManager.Instance.PlaySound(playClip);
}
audioPath = Resources/<epoch_time>.mp3
filename = <epoch_time>
SoundManager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
public static SoundManager Instance;
[SerializeField] private AudioSource _musicSource;
void Awake() {
if (Instance == null) {
Instance = this;
DontDestroyOnLoad(gameObject);
}
else {
Destroy(gameObject);
}
}
public void PlaySound(AudioClip clip) {
_musicSource.PlayOneShot(clip);
}
}
What am I doing wrong? Any way to improve this flow? Maybe not save as file but play the stream and discard?