Multiple audio streams in a generic application (Runtime API), XNA SoundEffect replacement
Since XNA is SoundEffect
no longer available in the Windows Runtime API (for generic app development), I need something like this to play multiple audio streams simultaneously.
Requirements: Play the same audio file at the same time.
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
supports multiple (up to 16, I think) SoundEffectInstance
at the same time.
The standard API MediaElement
only supports 1 audio stream for Windows Phone 8.1.
I ran into this: https://github.com/rajenki/audiohelper , which uses the XAudio2 API, but it doesn't seem to support concurrent audio.
source to share
solvable. I have used SharpDX. Many 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
Here is the solution code:
Initialization:
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
};
Event handler:
var sourceVoice = new SourceVoice(xAudio, waveFormat, true);
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
sourceVoice.Start();
Officially provided code via SharpDX example does not use NativeFileStream, it should make it work.
source to share