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