如何在 NETCore 中播放声音? [英] How to play a sound in NETCore?

查看:32
本文介绍了如何在 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 :

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 core 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.

因此使用 NAudio,这里是在 .NET Core 中播放声音的代码,假设您是在 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();
}

要获得更全球化的解决方案,您应该选择@Fiodar 的利用 Node.js 的解决方案.

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

这篇关于如何在 NETCore 中播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆