How to play a sound in NETCore?

As a workaround until .NET Core has audio support, you could try something like this:

public static void PlaySound(string file)
{
    Process.Start(@"powershell", $@"-c (New-Object Media.SoundPlayer '{file}').PlaySync();");
}

Of course this would only work on Windows with PowerShell installed, but you could detect which OS you are on and act accordingly.


There is now a way to do it with NAudio library (since 1.9.0-preview1) but it will only works on Windows.

So using NAudio, here the code to play a sound in .NET Core assuming you are doing it from a Windows environment.

using (var waveOut = new WaveOutEvent())
using (var wavReader = new WaveFileReader(@"c:\mywavfile.wav"))
{
   waveOut.Init(wavReader);
   waveOut.Play();
}

For a more global solution, you should go for @Fiodar's one taking advantage of Node.js.


Add package System.Windows.Extensions to your project.