2

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#

Toine db
  • 709
  • 7
  • 25
  • where I can get `pcm audio bytes`? – Andrii Krupka Aug 05 '16 at 17:53
  • Is it good sample [file](http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02-16kHz.wav)? – Andrii Krupka Aug 05 '16 at 17:58
  • @AndriiKrupka I added an 'audio sample' audio file, read the file as bytes in code and you will have my scenario. PCM audio is the raw audio comming from a for example a microphone, a wav is a formated audio file (so not raw anymore). – Toine db Aug 06 '16 at 07:10
  • It most likely can be done with WASAPI, XAudio and/or AudioGraph. But I can find a example where I can play bytes or raw mono audio from 16000hz/16bits – Toine db Aug 07 '16 at 08:50

2 Answers2

1

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.

Oleg Mikhailov
  • 252
  • 1
  • 8
  • thanks for the sample, Ill look at it as soon as possible. Buggy and leaking doesn't matter because its just for an non-production app. – Toine db Aug 10 '16 at 15:27
0

Add MediaElement on your page

<MediaElement x:Name="mediaElement"/>

and setup source manually

using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;


var stream = audioBytes.AsBuffer().AsStream().AsRandomAccessStream();
mediaElement.SetSource(stream, "audio/x-wav");
mediaElement.Play();
Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41
  • Thanks for the input, but it doesn't work, this solution is probably intended to work with .wav files. Did you test it with the 'audio sample' I added? – Toine db Aug 07 '16 at 08:47