Question

I was having issues playing back network streams as well as audio files using AAC and MP3 using NAudio. Took a while to figure out but the below solution works.

Was it helpful?

Solution

Hope to helps anyone else that is having a hard time utilizing NAudio properly.

//Create Output Stream with Data from Audio File / Network Stream
WaveOutputStream outputStream = new MediaFoundationReader("Path to File"); 
//Create Volume Stream to control volume of output 
//ex: volumeStream.Volume = 0.5f; Float between 0 & 1 
WaveChannel32 volumeStream = new WaveChannel32(outputStream);
//Create WaveOutEvent since it works in Background and UI Threads
WaveOutEvent player = new WaveOutEvent();
//Init Player with Configured Volume Stream
player.Init(volumeStream);
player.Play();

This code can play any audio file that MediaFoundationReader supports (MP3,AAC,WAV) as well as network streams of these codecs. To reuse the above player call Dispose() on player,outputStream, and volumeStream then set each to null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top