c# - Universal App(Runtime API)中的多个音频流,XNA SoundEffect 替换

标签 c# windows-8.1 windows-phone-8.1 win-universal-app

由于 XNA SoundEffect 在 Windows 运行时 API(用于开发通用应用程序)中不再可用,我需要类似的东西来同时播放多个音频流。

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

以前使用 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,需要它才能工作。

关于c# - Universal App(Runtime API)中的多个音频流,XNA SoundEffect 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613040/

相关文章:

c# - Marketplace Windows Phone Beta 测试错误代码 : 805a0194

c# - 向用户发送电子邮件以重置密码

C# 向量类 - 插值设计决策

windows-7 - InnoSetup - 编译符号工具失败,退出代码为 0x1

c# - 调用不带括号的函数?

c# - 如何获取被点击的 ListView 项目

windows-phone-8.1 - 如何调用 REST Web 服务进行 Windows 8.1 手机应用程序开发

java - double 理论

c# - SortedList 可以为每个值取两个键吗?

c# - 如何将光标移动到 RichEditBox 中文本的末尾?