ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-21 09:05:50  阅读:171  来源: 互联网

标签:win-universal-app windows-phone-8-1 windows-8-1 c


由于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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有