我应该使用哪个 API 在 Windows 上播放音频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4019733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which API should I use for playing audio on Windows?
提问by Jader Dias
There are many ways of playing sounds on Windows. Which are the differences, advantages and disavantages of each method?
在 Windows 上有多种播放声音的方法。每种方法的区别、优点和缺点是什么?
I know there are at least 5 methods:
我知道至少有 5 种方法:
- 1991 WinMM.dll/mmsys.dll PlaySound
- 1995 MCIWnd (as suggested by @casablanca)
- 1996 DirectSound
- 1998 WaveOut
- 1999 ASIO
- 1999 Windows Media Player ActiveX control?
- 2005 WASAPI(which is used by XAudio2 - as suggested by @Han)
- 2007 XAudio2
- 1991 WinMM.dll/mmsys.dll PlaySound
- 1995 MCIWnd(由@casablanca 建议)
- 1996 DirectSound
- 1998 WaveOut
- 1999 年
- 1999 Windows Media Player ActiveX 控件?
- 2005 WASAPI(由 XAudio2 使用 - 正如@Han 所建议的那样)
- 2007 XAudio2
采纳答案by casablanca
Really depends on what you want to do. For most common scenarios, I've found that the MCIWndfunctions work well: they're really easy to use and can play any format for which a codec is installed.
真的取决于你想做什么。对于大多数常见场景,我发现MCIWnd函数运行良好:它们非常易于使用并且可以播放安装了编解码器的任何格式。
DirectSound is somewhat more difficult to use but gives you much more control over the output; it lets you add special effects and simulate 3D positioning.
DirectSound 使用起来有些困难,但可以让您更好地控制输出;它可以让您添加特殊效果并模拟 3D 定位。
The waveOut
functions are the lowest level API you can get to and they are a kind of double-edged sword: you get to control exactly what goes out to the speakers, but they accept only raw waveform data, which means you are responsible for all decoding and post-processing of input data. PlaySound
essentially provides a nice wrapper around this.
这些waveOut
函数是您可以获得的最低级别的 API,它们是一种双刃剑:您可以准确控制输出到扬声器的内容,但它们只接受原始波形数据,这意味着您负责所有解码和输入数据的后处理。PlaySound
本质上为此提供了一个很好的包装器。
回答by Michael Aaron Safyan
QSound, then it will fit right in with the rest of your Qtapplication, and it will work not only on Windows but also on Mac OS X and Linux, as well. It is not uncommon to find a core, platform-specific API that isn't very friendly to developers, and then a myriad of more developer friendly APIs built on top of the core. Using a core API may be negligibly faster, but using the layers on top of these core APIs is almost always more convenient and maintainable, and protects you from changes to the low-level core.
QSound,那么它将适合您的Qt应用程序的其余部分,它不仅可以在 Windows 上运行,还可以在 Mac OS X 和 Linux 上运行。找到一个对开发人员不太友好的核心、特定于平台的 API,然后在核心之上构建无数对开发人员更友好的 API,这种情况并不少见。使用核心 API 的速度可能快到可以忽略不计,但使用这些核心 API 之上的层几乎总是更方便和可维护,并保护您免受对低级核心的更改。
Edit
From the description of XAudio2:
XAudio2 is a low-level, cross-platform audio API for Microsoft Windows and Xbox 360. It provides a signal processing and mixing foundation for games that is similar to its predecessors, DirectSound and XAudio. For Windows game developers, XAudio2 is the long-awaited replacement for DirectSound.
XAudio2 是用于 Microsoft Windows 和 Xbox 360 的低级、跨平台音频 API。它为其前辈 DirectSound 和 XAudio 类似的游戏提供了信号处理和混合基础。对于 Windows 游戏开发人员来说,XAudio2 是期待已久的 DirectSound 替代品。
So, it looks like that would be the API to use if you want a core, platform-specific audio library.
因此,如果您想要一个核心的、特定于平台的音频库,这似乎是要使用的 API。
Edit 2
I was a little quick with my first answer... really, it depends on what you want to do. If all you want to do is play an audio file, then QSound is the way to go. If, however, you want to mix and generate audio on the fly, then using a more feature-capable library such as XAudio2 (which is a part of DirectX and is intended for creating sound as part of video games) would be the way to go.
编辑 2
我的第一个答案有点快......真的,这取决于你想做什么。如果您只想播放音频文件,那么 QSound 是您的最佳选择。但是,如果您想即时混合和生成音频,那么使用功能更强大的库,例如 XAudio2(它是 DirectX 的一部分,旨在作为视频游戏的一部分创建声音)将是走。