如何在NETCore中播放声音? [英] How to play a sound in NETCore?
问题描述
我正在尝试在.Net Core控制台应用程序中播放声音,但无法弄清楚。
I'm trying to play a sound inside a .Net Core console application and I can't figure this out.
我正在寻找在.NET Core控制台中管理的内容。 .Net Core环境,可能像常规.Net一样:
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();
在dotnet核心Github上找到了他们谈论的问题。
Found an issue on dotnet core Github where they talk about it.
https://github.com/dotnet/core/issues/74
他们说没有用于回放音频的高级API,但是问题已经存在9个月了,所以我希望有新的东西吗?
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 ?
推荐答案
现在有一种使用NAudio库的方法(因为 1.9.0-preview1 ),但仅适用于Windows。
There is now a way to do it with NAudio library (since 1.9.0-preview1) but it will only works on Windows.
假设您是在Windows环境中使用NAudio使用NAudio编写的,那么这里的代码将在.NET Core中播放声音。
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 WavFileReader(@"c:\mywavfile.wav"))
{
waveOut.Init(wavReader);
waveOut.Play();
}
要获得更全球化的解决方案,您应该使用@Fiodar的解决方案Node.js。
For a more global solution, you should go for @Fiodar's one taking advantage of Node.js.
这篇关于如何在NETCore中播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!