我正在嘗試製作一種樂器類型的應用程序。我遇到的問題是,只有在舊的聲音完成後才能播放新的聲音。我希望能夠同時播放它們。如何在WinRT中使用SharpDX同時播放多個聲音?
這是我的代碼看起來像:
首先,它只是擁有一個音頻緩衝區和其他一些信息的MYWAVE類:
class MyWave
{
public AudioBuffer Buffer { get; set; }
public uint[] DecodedPacketsInfo { get; set; }
public WaveFormat WaveFormat { get; set; }
}
在聲音播放類:
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();
}
}
谷歌不是很有幫助,我找不到任何有用的東西。那麼我怎麼能同時播放多個聲音呢?
這個問題是如何解決的?你能幫我用代碼嗎?請 – 2015-07-31 15:37:17