How to Play a Sound in C#, .Net

How to play a sound in C#, .NET

You could use:

System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:\mywavfile.wav");
player.Play();

How to play a sound in NETCore?

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.

how to play a .wav file using c#

If you want the sound to play, you could use:

player.PlaySync();

rather than:

player.Play();

The reason that the latter doesn't work is that your program exits too quickly (once execution gets to the end of the Main method). PlaySync avoids this issue by using the current thread rather than a new thread.

how to play an audio file - .NET MAUI

Currently, Maui does not have any audio playback framework. And there are some relative known issues in maui, you can follow them here:

https://github.com/dotnet/maui/issues/7152 .

https://github.com/CommunityToolkit/Maui/issues/113

Thanks for your feedback and support for maui.

Playing sounds on Console - C#

Figured out the problem : I just needed to set the SoundLocation property of the SoundPlayer instance :

SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";

How can I play a sound with defined start- and end-time in .Net

In the End I use the NAudio library. That can handle this - not in a perfect way but ok.
see https://stackoverflow.com/a/13372540/2936206

c# play sound with one line of c# code

No need for a variable:

(new SoundPlayer(@"c:\Media\pacman.wav")).Play();


Related Topics



Leave a reply



Submit