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
C# .Equals(), .Referenceequals() and == Operator
How to Quickly Check If Two Data Transfer Objects Have Equal Properties in C#
"Open/Close" SQLconnection or Keep Open
Differencebetween Directory.Enumeratefiles VS Directory.Getfiles
Regex to Strip Line Comments from C#
An Attempt Was Made to Access a Socket in a Way Forbidden by Its Access Permissions. Why
Setting Canvas Properties in an Itemscontrol Datatemplate
Determine If a Sequence Contains All Elements of Another Sequence Using Linq
Entitytype Has No Key Defined Error
Why C# Doesn't Implement Indexed Properties
C# Little Endian or Big Endian
How to Use Non-Thread-Safe Async/Await APIs and Patterns with ASP.NET Web API
How to Implement Icomparable Interface
How to Use a MySQL User Defined Variable in a .Net MySQLcommand