Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 849 Bytes

PlayAudioFileConsoleApp.md

File metadata and controls

22 lines (17 loc) · 849 Bytes

Play an Audio File from a Console application

To play a file from a console application, we will use AudioFileReader as a simple way of opening our audio file, and WaveOutEvent as the output device.

We simply need to pass the audioFile into the outputDevice with the Init method, and then call Play.

Since Play only means "start playing" and isn't blocking, we can wait in a loop until playback finishes.

Afterwards, we need to Dispose our audioFile and outputDevice, which in this example we do by virtue of putting them inside using blocks.

using(var audioFile = new AudioFileReader(audioFile))
using(var outputDevice = new WaveOutEvent())
{
    outputDevice.Init(audioFile);
    outputDevice.Play();
    while (outputDevice.PlaybackState == PlaybackState.Playing)
    {
        Thread.Sleep(1000);
    }
}