Search code examples
c#uwpwasapixaudio2

How to play raw pcm audio bytes on UWP apps?


I have a Universal Windows Platform (UWP) app where I want to play audio bytes that I recorded on a old Window phone 8.1 app.

The audio is a array of bytes with raw PCM audio (mono, 16 bits, 16kHz).

On my old Windows phone 8.1 app I just could use 3 lines of code for this.

SoundEffect sound = new SoundEffect(audioBytes, sampleRate, AudioChannels.Mono);
SoundEffectInstance soundInstance = sound.CreateInstance();
soundInstance.Play();

Unfortunatly 'SoundEffect' is gone in UWP.

Is there a simple way to do this in C# UWP apps?

For test purposes an audio sample (Mono, 16bits, 16000Hz, litle-endian)

PS: I looked at Wasapi but its all in c++ and can't find a easy example for someone that normally works in c#


Solution

  • In addition to media element you could try AudioGraph with FrameInputNode, here is sample: https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/AudioCreation/cs/AudioCreation/Scenario3_FrameInputNode.xaml.cs But be careful, AudioGraph is still buggy and could leak.