public SoundEffect(AudioDevice device, string filename) { dev = device; List <byte> totalBytes = new List <byte> (); byte[] buffer = new byte[4096]; var stream = File.OpenRead(filename); ALFormat fmt; int sampleRate; using (var decoder = DecoderFactory.GetDecoderFromStream(stream)) { int read = 0; fmt = decoder.Format; sampleRate = decoder.SampleRate; while ((read = decoder.Read(4096, buffer)) == 4096) { totalBytes.AddRange(buffer); } totalBytes.AddRange(buffer.Take(read)); } dev.EnsureAudioThread(() => { alBuffer = AL.GenBuffer(); AL.BufferData( alBuffer, fmt, totalBytes.ToArray(), totalBytes.Count, sampleRate ); }); }
public void PlayMusic(string filename) { StopMusic(); Stream = File.OpenRead(filename); MusicDecoder = DecoderFactory.GetDecoderFromStream(Stream); MusicStreamer = new StreamingAudio(Device, MusicDecoder.Format, MusicDecoder.SampleRate); MusicStreamer.BufferNeeded += (instance, buffer) => MusicDecoder.Read(buffer.Length, buffer); MusicStreamer.PlaybackFinished += (sender, e) => { if (loopMusic) { PlayMusic(filename); } else { MusicStreamer.Dispose(); MusicDecoder.Dispose(); MusicStreamer = null; } }; MusicStreamer.Play(); }