问题描述
由于 XNA SoundEffect
在 Windows 运行时 API(用于开发通用应用程序)中不再可用,我需要类似的东西来同时播放多个音频流.
As the XNA SoundEffect
is no longer available in the Windows Runtime API (for developing Universal App), I need something similar to play multiple audio streams at the same time.
要求:同时多次播放同一个音频文件.
Requirements:Play the same audio file multiple times, simultaneously.
以前使用 SoundEffect
实现 Silverlight:
Previous Silverlight implementation with SoundEffect
:
// 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
.
SoundEffect
supports multiple (up to 16 I think) SoundEffectInstance
being played simultaneously.
标准 MediaElement
API 仅支持 1 个适用于 Windows Phone 8.1 的音频流.
The standard MediaElement
API only supports 1 audio stream for Windows Phone 8.1.
我遇到了这个:https://github.com/rajenki/audiohelper 使用 XAudio2API 但它似乎也不支持同步音频.
I bumped into this: https://github.com/rajenki/audiohelper which uses the XAudio2 API but it doesn't seem to support simultaneous audio either.
推荐答案
已解决.我使用了 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
Solved. I used SharpDX. Huge thanks to the author here: 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的sample官方提供的代码没有使用NativeFileStream,需要它才能工作.
The officially provided code by SharpDX's sample does not use NativeFileStream, it is required to make it work.
这篇关于通用应用程序中的多个音频流(运行时 API),XNA SoundEffect 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!