编程语言
首页 > 编程语言> > c#-通用应用程序(运行时API)中的多个音频流,XNA SoundEffect替代

c#-通用应用程序(运行时API)中的多个音频流,XNA SoundEffect替代

作者:互联网

由于XNA SoundEffect在Windows Runtime API(用于开发Universal App)中不再可用,因此我需要类似的东西来同时播放多个音频流.

要求:
同时播放同一音频文件多次.

先前使用SoundEffect的Silverlight实现:

// Play sound 10 times, sound can be played together.
// i.e. First sound continues playing while second sound starts playing.
for(int i=0; i++; i < 10)
{
    Stream stream = TitleContainer.OpenStream("sounds/Ding.wav");
    SoundEffect effect = SoundEffect.FromStream(stream);
    FrameworkDispatcher.Update();
    effect.Play();
    // Wait a while before playing again.
    Thread.Sleep(500);
}

SoundEffect支持同时播放多个(我认为最多16个)SoundEffectInstance.

标准MediaElement API仅支持Windows Phone 8.1的1个音频流.

我碰到了这个问题:https://github.com/rajenki/audiohelper使用XAudio2 API,但它似乎也不支持同时音频.

解决方法:

解决了.我使用SharpDX.非常感谢作者在这里:http://www.hoekstraonline.net/2013/01/13/how-to-play-a-wav-sound-file-with-directx-in-c-for-windows-8/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-play-a-wav-sound-file-with-directx-in-c-for-windows-8

这是解决方案的代码:

初始化:

        xAudio = new XAudio2();
        var masteringVoice = new MasteringVoice(xAudio);
        var nativeFileStream = new NativeFileStream("Assets/Ding.wav", NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);

        stream = new SoundStream(nativeFileStream);
        waveFormat = stream.Format;
        buffer = new AudioBuffer
        {
            Stream = stream.ToDataStream(),
            AudioBytes = (int)stream.Length,
            Flags = BufferFlags.EndOfStream
        };

事件处理程序:

        var sourceVoice = new SourceVoice(xAudio, waveFormat, true);


        sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
        sourceVoice.Start();

SharpDX的示例提供的官方代码不使用NativeFileStream,需要使用它才能正常工作.

标签:win-universal-app,windows-phone-8-1,windows-8-1,c
来源: https://codeday.me/bug/20191121/2050833.html