wav 文件具有结构 44 字节的起始音频信号。如果读取 44-to end 文件音频数据(wav 文件)你已经在 .NET(opeanl)中播放你的音乐
https://dpaste.com/8VNKMAUU5
public static void playwav()
{
Console.WriteLine("Hello World!");
OpenTK.Audio.OpenAL.ALDevice device = OpenTK.Audio.OpenAL.ALDevice.Null;
OpenTK.Audio.OpenAL.ALContext ctx = OpenTK.Audio.OpenAL.ALContext.Null;
string defname = OpenTK.Audio.OpenAL.ALC.GetString(device, OpenTK.Audio.OpenAL.AlcGetString.DefaultDeviceSpecifier);
Console.WriteLine(defname);
device = OpenTK.Audio.OpenAL.ALC.OpenDevice(defname);
if (!OpenTK.Audio.OpenAL.ALC.IsExtensionPresent(device, "ALC_EXT_EFX"))
return;
int[] atrributes = { 0, 0, 0, 0 };
int[] iSends = { 0 };
atrributes[0] = ((int)OpenTK.Audio.OpenAL.AlcContextAttributes.EfxMaxAuxiliarySends);
atrributes[1] = 4;
Console.WriteLine("EFX Extension found!\n");
ctx = OpenTK.Audio.OpenAL.ALC.CreateContext(device, atrributes);
OpenTK.Audio.OpenAL.ALC.MakeContextCurrent(ctx);
OpenTK.Audio.OpenAL.ALC.ProcessContext(ctx);
OpenTK.Audio.OpenAL.ALC.GetInteger(device, OpenTK.Audio.OpenAL.AlcGetInteger.EfxMaxAuxiliarySends, 1, iSends);
//OpenTK.Audio.OpenAL.AL.GetProcAddress("LPALGENEFFECTS");
int buffer;
string str = @"C:\WorkC#\ConsoleApp3\ConsoleApp3\20211222-204048_.wav";
//Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle("20211222-204048_.wav",true);
FileStream fileStream = File.OpenRead(str);
byte[] bufdata = new byte[fileStream.Length];
//bufdata[0] = 0;
byte[] RIFF = new byte[4];
byte[] sizeRid = new byte[4];
byte[] WAVE = new byte[4];
byte[] FMT = new byte[4];
fileStream.Read(RIFF, 0, 4);
fileStream.Read(sizeRid, 0, 4);
fileStream.Read(WAVE, 0, 4);
fileStream.Read(FMT, 0, 4);
fileStream.Read(bufdata, 44, (int)(fileStream.Length - 45));
OpenTK.Audio.OpenAL.AL.GenBuffer(out buffer);
UInt16 value = BitConverter.ToUInt16(RIFF, 0);
Console.WriteLine(((char)value));
Console.WriteLine(sizeRid.Length);
Console.WriteLine(WAVE.Length);
OpenTK.Audio.OpenAL.AL.BufferData(buffer, OpenTK.Audio.OpenAL.ALFormat.Stereo16, bufdata, 44100);
Console.WriteLine((bufdata));
int src;
OpenTK.Audio.OpenAL.AL.GenSource(out src);
OpenTK.Audio.OpenAL.AL.Source(src, OpenTK.Audio.OpenAL.ALSourcei.Buffer, buffer);
OpenTK.Audio.OpenAL.AL.SourcePlay(src);
int state = 4114;
while (state == (int)OpenTK.Audio.OpenAL.ALSourceState.Playing)
{
OpenTK.Audio.OpenAL.AL.GetSource(src, OpenTK.Audio.OpenAL.ALGetSourcei.SourceState, out state);
Console.Write(state + "\r");
//int state = (int)OpenTK.Audio.OpenAL.ALSourceState.Playing;
//OpenTK.Audio.OpenAL.AL.SourcePlay(src);
}
OpenTK.Audio.OpenAL.ALC.DestroyContext(ctx);
OpenTK.Audio.OpenAL.ALC.CloseDevice(device);
//OpenTK.Audio.OpenAL.ALC.GetContextsDevice();
}