Load wav or ogg file in C# from custom location?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic

I am using the following code in c# where audSound is an AudioStreamPlayer

        AudioStreamOGGVorbis d  =  GD.Load("C:/Sounds/mad.ogg") as AudioStreamOGGVorbis;
        audSound.Stream = d;
        audSound.Play();

This will work if it in a in the"res://" folder, but I can’t get it to load outside this folder. Could someone help?

:bust_in_silhouette: Reply From: ondesic

I wish more people would respond on this forum. Here is the solution that worked for me:

public void OpenSong(string path)
{        
        File file = new File();
        Error err = file.Open(path, File.ModeFlags.Read);
        byte[] b = file.GetBuffer((int)file.GetLen());

        if(path.Extension() == "wav")
        {
            AudioStreamSample a = new AudioStreamSample();
            a.Format = AudioStreamSample.FormatEnum.Format16Bits;
        
            a.Stereo = true;
            a.Data = b;
            audSound.Stream = a;   
        }
        else if(path.Extension() == "ogg")
        {
            AudioStreamOGGVorbis d = new AudioStreamOGGVorbis();
            d.Data = b;
            audSound.Stream = d;   
        }

         audSound.Play();
}