MVH/NAudio-2.2.1/Docs/PlayAudioFileConsoleApp.md
2024-06-07 00:47:07 +02:00

22 lines
849 B
Markdown

## 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.
```c#
using(var audioFile = new AudioFileReader(audioFile))
using(var outputDevice = new WaveOutEvent())
{
outputDevice.Init(audioFile);
outputDevice.Play();
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
Thread.Sleep(1000);
}
}
```