C# 通用应用程序(运行时API)中的多个音频流,XNA SoundEffect替换
由于XNAC# 通用应用程序(运行时API)中的多个音频流,XNA SoundEffect替换,c#,windows-8.1,windows-phone-8.1,win-universal-app,C#,Windows 8.1,Windows Phone 8.1,Win Universal App,由于XNASoundEffect在Windows运行时API(用于开发通用应用程序)中不再可用,我需要类似的东西来同时播放多个音频流 要求: 同时多次播放同一音频文件 以前的Silverlight应用程序具有SoundEffect: // Play sound 10 times, sound can be played together. // i.e. First sound continues playing while second sound starts playing. for(in
SoundEffect
在Windows运行时API(用于开发通用应用程序)中不再可用,我需要类似的东西来同时播放多个音频流
要求:
同时多次播放同一音频文件
以前的Silverlight应用程序具有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);
}
//播放声音10次,声音可以一起播放。
//即,第一个声音继续播放,而第二个声音开始播放。
对于(int i=0;i++;i<10)
{
Stream=TitleContainer.OpenStream(“sounds/Ding.wav”);
SoundEffect效果=SoundEffect.FromStream(流);
FrameworkDispatcher.Update();
效果。游戏();
//请稍等片刻,然后再玩。
睡眠(500);
}
SoundEffect
支持同时播放多个(我想最多16个)SoundEffectInstance
标准的MediaElement
API仅支持一个适用于Windows Phone 8.1的音频流
我遇到了这样一个问题:它使用XAudio2 API,但似乎也不支持同步音频。解决了。我用了夏普DX。非常感谢这里的作者: 以下是解决方案的代码: 初始化:
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,需要它才能正常工作