QAudioDevice Class

The QAudioDevice class provides an information about audio devices and their functionality. More...

Header: #include <QAudioDevice>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia

Public Types

enum Mode { Null, Input, Output }

Properties

Public Functions

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

Detailed Description

QAudioDevice describes an audio device available in the system, either for input or for playback.

A QAudioDevice is used by Qt to construct classes that communicate with the device – such as QAudioSource, and QAudioSink. It is also used to determine the input or output device to use in a capture session or during media playback.

You can also query each device for the formats it supports. A format in this context is a set consisting of a channel count, sample rate, and sample type. A format is represented by the QAudioFormat class.

The values supported by the device for each of these parameters can be fetched with minimumChannelCount(), maximumChannelCount(), minimumSampleRate(), maximumSampleRate() and supportedSampleFormats(). The combinations supported are dependent on the audio device capabilities. If you need a specific format, you can check if the device supports it with isFormatSupported(). For instance:

{
    sourceFile.setFileName("/tmp/test.raw");
    sourceFile.open(QIODevice::ReadOnly);

    QAudioFormat format;
    // Set up the format, eg.
    format.setSampleRate(8000);
    format.setChannelCount(1);
    format.setSampleFormat(QAudioFormat::UInt8);

    QAudioDevice info(QMediaDevices::defaultAudioOutput());
    if (!info.isFormatSupported(format)) {
        qWarning() << "Raw audio format not supported by backend, cannot play audio.";
        return;
    }

    audio = new QAudioSink(format, this);
    connect(audio, QAudioSink::stateChanged, this, &AudioInputExample::handleStateChanged);
    audio->start(&sourceFile);
}

The set of available devices can be retrieved from the QMediaDevices class.

For instance:

const auto devices = QMediaDevices::audioOutputs();
for (const QAudioDevice &device : devices)
    qDebug() << "Device: " << device.description();

In this code sample, we loop through all devices that are able to output sound, i.e., play an audio stream in a supported format. For each device we find, we simply print the deviceName().

See also QAudioSink, QAudioSource, and QAudioFormat.

Member Type Documentation

enum QAudioDevice::Mode

Describes the mode of this device.

ConstantValueDescription
QAudioDevice::Null0A null device.
QAudioDevice::Input1An input device.
QAudioDevice::Output2An output device.

Property Documentation

[read-only] description : const QString

Returns a human readable name of the audio device.

Use this string to present the device to the user.

Access functions:

QString description() const

[read-only] id : const QByteArray

Returns an identifier for the audio device.

Device names vary depending on the platform/audio plugin being used.

They are a unique identifier for the audio device.

Access functions:

QByteArray id() const

[read-only] isDefault : const bool

Returns true if this is the default audio device.

Access functions:

bool isDefault() const

[read-only] mode : const Mode

Returns whether this device is an input or output device.

Access functions:

QAudioDevice::Mode mode() const

Member Function Documentation

QAudioDevice::QAudioDevice()

Constructs a null QAudioDevice object.

QAudioDevice::QAudioDevice(const QAudioDevice &other)

Constructs a copy of other.

[noexcept] QAudioDevice::QAudioDevice(QAudioDevice &&other)

Move constructs from other.

[noexcept] QAudioDevice::~QAudioDevice()

Destroy this audio device info.

QAudioFormat::ChannelConfig QAudioDevice::channelConfiguration() const

Returns the channel configuration of the device.

bool QAudioDevice::isFormatSupported(const QAudioFormat &settings) const

Returns true if the supplied settings are supported by the audio device described by this QAudioDevice.

bool QAudioDevice::isNull() const

Returns whether this QAudioDevice object holds a valid device definition.

int QAudioDevice::maximumChannelCount() const

Returns the maximum number of supported channel counts.

This is typically 1 for mono sound, or 2 for stereo sound.

int QAudioDevice::maximumSampleRate() const

Returns the maximum supported sample rate (in Hertz).

int QAudioDevice::minimumChannelCount() const

Returns the minimum number of supported channel counts.

This is typically 1 for mono sound, or 2 for stereo sound.

int QAudioDevice::minimumSampleRate() const

Returns the minimum supported sample rate (in Hertz).

QAudioFormat QAudioDevice::preferredFormat() const

Returns the default audio format settings for this device.

These settings are provided by the platform/audio plugin being used.

They are also dependent on the QtAudio::Mode being used.

A typical audio system would provide something like:

  • Input settings: 48000Hz mono 16 bit.
  • Output settings: 48000Hz stereo 16 bit.

QList<QAudioFormat::SampleFormat> QAudioDevice::supportedSampleFormats() const

Returns a list of supported sample types.

[noexcept] void QAudioDevice::swap(QAudioDevice &other)

Swaps the audio device with the other.

bool QAudioDevice::operator!=(const QAudioDevice &other) const

Returns true if this QAudioDevice class represents a different audio device than other

[noexcept] QAudioDevice &QAudioDevice::operator=(QAudioDevice &&other)

Moves other into this QAudioDevice object.

QAudioDevice &QAudioDevice::operator=(const QAudioDevice &other)

Sets the QAudioDevice object to be equal to other.

bool QAudioDevice::operator==(const QAudioDevice &other) const

Returns true if this QAudioDevice class represents the same audio device as other.

© 2024 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.