QAudioDevice Class
QAudioDevice 类提供有关音频设备及其功能的信息。更多
头文件: | #include <QAudioDevice> |
CMake.QAudioDevice 类 | 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 XML 使用 QAudioDevice 来构建与设备通信的类,如QAudioSource 和QAudioSink 。它还用于确定在捕获会话或媒体播放过程中使用的输入或输出设备。
QAudioDevice 实例在其整个生命周期内都会保留其属性,即使相应的物理设备断开连接或其设置被修改。要跟踪更新的属性,用户应在相关信号触发时从QMediaDevices 加载新的 QAudioDevice 实例。
您还可以查询每个设备所支持的格式。这里的格式是由通道数、采样率和采样类型组成的集合。格式由QAudioFormat 类表示。
可通过minimumChannelCount(),maximumChannelCount(),minimumSampleRate(),maximumSampleRate() 和supportedSampleFormats() 获取设备支持的每个参数值。支持的组合取决于音频设备的功能。如果需要特定格式,可以使用isFormatSupported() 检查设备是否支持。例如
{ sourceFile.setFileName("/tmp/test.raw"); sourceFile.open(QIODevice::ReadOnly); QAudioFormatformat;// 设置格式,例如format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioDeviceinfo(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 的副本。
[noexcept]
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 类代表的音频设备与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。
© 2025 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.