c# windows-8 windows-runtime sharpdx xaudio2

c# - ¿Cómo puedo reproducir múltiples sonidos al mismo tiempo usando SharpDX en WinRT?



windows-8 windows-runtime (1)

Estoy tratando de hacer un tipo de instrumento musical de aplicación. El problema que tengo es que un nuevo sonido solo se reproducirá si el anterior está terminado. Me gustaría poder jugarlos simultáneamente.

Así es como mi código se ve así:

Primero, la clase MyWave que simplemente contiene un buffer de audio y otra información:

class MyWave { public AudioBuffer Buffer { get; set; } public uint[] DecodedPacketsInfo { get; set; } public WaveFormat WaveFormat { get; set; } }

En la clase SoundPlayer:

private XAudio2 xaudio; private MasteringVoice mvoice; Dictionary<string, MyWave> sounds; // Constructor public SoundPlayer() { xaudio = new XAudio2(); xaudio.StartEngine(); mvoice = new MasteringVoice(xaudio); sounds = new Dictionary<string, MyWave>(); } // Reads a sound and puts it in the dictionary public void AddWave(string key, string filepath) { MyWave wave = new MyWave(); var nativeFileStream = new NativeFileStream(filepath, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read); var soundStream = new SoundStream(nativeFileStream); var buffer = new AudioBuffer() { Stream = soundStream, AudioBytes = (int)soundStream.Length, Flags = BufferFlags.EndOfStream }; wave.Buffer = buffer; wave.DecodedPacketsInfo = soundStream.DecodedPacketsInfo; wave.WaveFormat = soundStream.Format; this.sounds.Add(key, wave); } // Plays the sound public void Play(string key) { if (!this.sounds.ContainsKey(key)) return; MyWave w = this.sounds[key]; var sourceVoice = new SourceVoice(this.xaudio, w.WaveFormat); sourceVoice.SubmitSourceBuffer(w.Buffer, w.DecodedPacketsInfo); sourceVoice.Start(); } }

Google no fue muy útil, no pude encontrar nada útil. Entonces, ¿cómo puedo reproducir múltiples sonidos simultáneamente?


Tendrá que crear (y preferiblemente agrupar) varias instancias de SourceVoice y reproducirlas simultáneamente.

De hecho, tu código actual debería funcionar, ¿no? Es posible que desee agregar un detector de eventos StreamEnd al SourceVoice para que se deshaga de él después de que se complete la reproducción, y recuerde habilitar las devoluciones de llamadas cuando llame al constructor de SourceVoice.