我怎样才能在WinRT中使用SharpDX同时播放多个声音?(How can I play mult

2019-07-04 07:15发布

我试图让应用程序的乐器类型。 我遇到的问题是,如果旧的已完成一个新的声音将只播放。 我想能够同时播放。

这是我的代码看起来像:

首先,它只是拥有一个音频缓冲区和其他一些信息的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();
    }
}

谷歌是不是非常有帮助,我无法找到任何有用的东西。 所以,我怎么能同时播放多个声音?

Answer 1:

你必须创建(并优选池)多SourceVoice实例,并同时播放。

事实上,当前的代码应工作,不是吗? 您可能需要一个StreamEnd事件监听器添加到SourceVoice播放完成后处置的本身,并记住调用SourceVoice的构造函数时启用回调。



文章来源: How can I play multiple sounds at the same time using SharpDX in WinRT?