Hello,
I try to load an audioclip from a file (which is not in resources).
I searched for that and found a WWW audioclip which is not supported in unity 2017 anymore.
So first I tried to load it from file and set the loaded data with SetData (I am unsure how to get it from float[ ] to Byte[ ]):
//AudioClip audioClip = new AudioClip();
byte[] data = System.IO.File.ReadAllBytes(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Path.GetFileName(Globals.ExpertData.AudioClipPaths[Globals.Step]));
float[] floatdata = new float[data.Length];
for (int i = 0; i < data.Length; i++)
{
floatdata[i] = data[i];
}
//AudioClip audioClip = AudioClip.Create("temp", data.Length, 1, 44100 * 2, false);
AudioClip audioClip = AudioClip.Create("temp", data.Length, 1, 48000 * 2, false);
audioClip.SetData(floatdata, 0);
//AudioClip audioClip = (AudioClip)Resources.Load( Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Path.GetFileName(Globals.ExpertData.VideoClipPaths[Globals.Step]), typeof(AudioClip));
//if (audioClip == null)
//{
// audioClip = (AudioClip)Resources.Load(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Path.GetFileName(Globals.ExpertData.VideoClipPaths[Globals.Step]) + "_", typeof(AudioClip));
//}
audioSource.clip = audioClip;
Did not work, Looks okay (audioClip not null, no error), but does not play, I hear nothing.
When I began to write this Topic, I stumbled about UnityWebRequestMultimedia.GetAudioClip.
Seems to be more for web than for a file, but I tried it:
AudioClip audioClip = null;
//using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Path.GetFileName(Globals.ExpertData.AudioClipPaths[Globals.Step]), AudioType.WAV))
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Path.GetFileName(Globals.ExpertData.AudioClipPaths[Globals.Step]), AudioType.WAV))
{
www.Send();
if (www.isHttpError || www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
audioClip = DownloadHandlerAudioClip.GetContent(www);
}
}
Does also not work.
Isn’t there an easier way to load an external Audio file? For the first time, I think it is a really simple quests but with extrem complicated solution to find in unity…
I am using the hololens, and want to have this as a Workaround for a bug, because the videoplayer does not Play Audio using the hololens (I already made a bug Report), so I detached the Audio track but I cannot even get this running from file…
Any help please? Is there an easier way or how do I get one of those two ways working?
Thanks a lot!