Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to play a sound inside a .Net Core console application and I can't figure this out.

I am looking for something managed inside the .Net Core environment, maybe like regular .Net :

// Not working on .Net Core    
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"c:mywavfile.wav");
player.Play();

Found an issue on dotnet core Github where they talk about it.

https://github.com/dotnet/core/issues/74

They say there is no high-level API for audio playback but the issue is 9 months old, so I hope there is something new ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
300 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...