QAudioDevice Class
QAudioDevice 类提供了有关音频设备及其功能的信息。更多内容...
| 头文件: | #include <QAudioDevice> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Multimedia) target_link_libraries(mytarget PRIVATE Qt6::Multimedia) |
| qmake: | QT += multimedia |
公共类型
| enum | Mode { Null, Input, Output } |
属性
- description : const QString
- id : const QByteArray
- isDefault : const bool
- mode : const Mode
公共函数
| QAudioDevice() | |
| QAudioDevice(const QAudioDevice &other) | |
| QAudioDevice(QAudioDevice &&other) | |
| ~QAudioDevice() | |
| QAudioFormat::ChannelConfig | channelConfiguration() const |
| QString | description() const |
| QByteArray | id() const |
| bool | isDefault() const |
| bool | isFormatSupported(const QAudioFormat &settings) const |
| bool | isNull() const |
| int | maximumChannelCount() const |
| int | maximumSampleRate() const |
| int | minimumChannelCount() const |
| int | minimumSampleRate() const |
| QAudioDevice::Mode | mode() const |
| QAudioFormat | preferredFormat() const |
| QList<QAudioFormat::SampleFormat> | supportedSampleFormats() const |
| void | swap(QAudioDevice &other) |
| bool | operator!=(const QAudioDevice &other) const |
| QAudioDevice & | operator=(QAudioDevice &&other) |
| QAudioDevice & | operator=(const QAudioDevice &other) |
| bool | operator==(const QAudioDevice &other) const |
详细说明
QAudioDevice 描述了系统中可用的音频设备,该设备可用于输入或播放。
Qt 使用 QAudioDevice 来构建与设备进行通信的类——例如QAudioSource 和QAudioSink 。它还用于确定在捕获会话或媒体播放过程中应使用的输入或输出设备。
QAudioDevice 实例在其整个生命周期内都会保留其属性,即使相应的物理设备已断开连接或其设置已被修改。为了跟踪更新的属性,用户应在相关信号触发时,从QMediaDevices 加载新的 QAudioDevice 实例。
您还可以查询每个设备支持的格式。在此上下文中,格式是由通道数、采样率和采样类型组成的集合。格式由QAudioFormat 类表示。
设备对上述各参数的支持值可通过minimumChannelCount()、maximumChannelCount()、minimumSampleRate()、maximumSampleRate() 和supportedSampleFormats() 获取。支持的组合取决于音频设备的能力。若需使用特定格式,可通过isFormatSupported() 检查设备是否支持该格式。例如:
{
sourceFile.setFileName("/tmp/test.raw");
sourceFile.open(QIODevice::ReadOnly);
QAudioFormat format;
// 配置格式,例如:
format.setSampleRate(44100);
format.setChannelCount(1);
format.setSampleFormat(QAudioFormat::Int16);
QAudioDevice info(QMediaDevices::defaultAudioOutput());
if(!info.isFormatSupported(format)) {
qWarning() << "Raw audio format not supported by backend, cannot play audio.";
return;
}
audio= newQAudioSink(format, this);
connect(audio,QAudioSink::stateChanged, this, &AudioInputExample::handleStateChanged);
audio->start(&sourceFile);
}可通过QMediaDevices 类获取可用设备列表。
例如:
const autodevices=QMediaDevices::audioOutputs();
for(constQAudioDevice&device: devices)
qDebug() << "Device: " << device.description();在此代码示例中,我们遍历所有能够输出声音(即播放受支持格式的音频流)的设备。对于找到的每个设备,我们只需打印其 deviceName() 即可。
另请参阅 QAudioSink 、QAudioSource 以及QAudioFormat 。
成员类型文档
enum QAudioDevice::Mode
描述了该设备的运行模式。
| 常量 | 值 | 描述 |
|---|---|---|
QAudioDevice::Null | 0 | 一个空设备。 |
QAudioDevice::Input | 1 | 一个输入设备。 |
QAudioDevice::Output | 2 | 一个输出设备。 |
属性文档
[read-only] description : const QString
返回音频设备的人类可读名称。
使用此字符串向用户展示该设备。
访问函数:
| QString | description() const |
[read-only] id : const QByteArray
返回音频设备的标识符。
设备名称因所使用的平台/音频插件而异。
它们是音频设备的唯一标识符。
访问函数:
| QByteArray | id() const |
[read-only] isDefault : const bool
如果这是默认音频设备,则返回 true。
访问函数:
| bool | isDefault() const |
[read-only] mode : const Mode
返回该设备是输入设备还是输出设备。
访问函数:
| QAudioDevice::Mode | mode() const |
成员函数文档
QAudioDevice::QAudioDevice()
创建一个空的 QAudioDevice 对象。
QAudioDevice::QAudioDevice(const QAudioDevice &other)
创建other 的副本。
[constexpr noexcept default] QAudioDevice::QAudioDevice(QAudioDevice &&other)
将构造从other 中移出。
[noexcept] QAudioDevice::~QAudioDevice()
销毁此音频设备的信息。
QAudioFormat::ChannelConfig QAudioDevice::channelConfiguration() const
返回设备的信道配置。
bool QAudioDevice::isFormatSupported(const QAudioFormat &settings) const
如果由该QAudioDevice 所描述的音频设备支持所提供的settings ,则返回true。
bool QAudioDevice::isNull() const
返回此QAudioDevice 对象是否包含有效的设备定义。
int QAudioDevice::maximumChannelCount() const
返回支持的最大声道数。
通常,单声道音频为 1,立体声音频为 2。
int QAudioDevice::maximumSampleRate() const
返回支持的最高采样率(单位:赫兹)。
int QAudioDevice::minimumChannelCount() const
返回支持的最小声道数。
通常,单声道音频为 1,立体声音频为 2。
int QAudioDevice::minimumSampleRate() const
返回支持的最低采样率(单位为赫兹)。
QAudioFormat QAudioDevice::preferredFormat() const
返回该设备的默认音频格式设置。
这些设置由所使用的平台/音频插件提供。
它们还取决于所使用的QtAudio::Mode。
典型的音频系统会提供类似以下设置:
- 输入设置:48000Hz 单声道 16 位。
- 输出设置:48000Hz 立体声 16 位。
QList<QAudioFormat::SampleFormat> QAudioDevice::supportedSampleFormats() const
返回一个包含受支持的样本类型的列表。
[noexcept] void QAudioDevice::swap(QAudioDevice &other)
将音频设备与other 互换。
bool QAudioDevice::operator!=(const QAudioDevice &other) const
如果该QAudioDevice 类表示的音频设备与以下设备不同,则返回true:other
[noexcept] QAudioDevice &QAudioDevice::operator=(QAudioDevice &&other)
将other 移动到此QAudioDevice 对象中。
QAudioDevice &QAudioDevice::operator=(const QAudioDevice &other)
将QAudioDevice 对象设置为与other 相等。
bool QAudioDevice::operator==(const QAudioDevice &other) const
如果该QAudioDevice 类表示的音频设备与other 表示的音频设备相同,则返回true。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.