QAudioDecoder Class

The QAudioDecoder class implements decoding audio. More...

Header: #include <QAudioDecoder>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
Inherits: QObject
Status: Preliminary

This class is under development and is subject to change.

Public Types

enum Error { NoError, ResourceError, FormatError, AccessDeniedError, NotSupportedError }

Properties

Public Functions

QAudioDecoder(QObject *parent = nullptr)
virtual ~QAudioDecoder()
QAudioFormat audioFormat() const
bool bufferAvailable() const
qint64 duration() const
QAudioDecoder::Error error() const
QString errorString() const
bool isDecoding() const
bool isSupported() const
qint64 position() const
QAudioBuffer read() const
void setAudioFormat(const QAudioFormat &format)
void setSource(const QUrl &fileName)
void setSourceDevice(QIODevice *device)
QUrl source() const
QIODevice *sourceDevice() const

Public Slots

void start()
void stop()

Signals

void bufferAvailableChanged(bool available)
void bufferReady()
void durationChanged(qint64 duration)
void error(QAudioDecoder::Error error)
void finished()
void formatChanged(const QAudioFormat &format)
void isDecodingChanged(bool)
void positionChanged(qint64 position)
void sourceChanged()

Detailed Description

The QAudioDecoder class is a high level class for decoding audio media files. It is similar to the QMediaPlayer class except that audio is provided back through this API rather than routed directly to audio hardware.

See also QAudioBuffer.

Member Type Documentation

enum QAudioDecoder::Error

Defines a media player error condition.

ConstantValueDescription
QAudioDecoder::NoError0No error has occurred.
QAudioDecoder::ResourceError1A media resource couldn't be resolved.
QAudioDecoder::FormatError2The format of a media resource isn't supported.
QAudioDecoder::AccessDeniedError3There are not the appropriate permissions to play a media resource.
QAudioDecoder::NotSupportedError4QAudioDecoder is not supported on this platform

Property Documentation

[read-only] bufferAvailable : const bool

This property holds whether there is a decoded audio buffer available

Access functions:

bool bufferAvailable() const

Notifier signal:

void bufferAvailableChanged(bool available)

[read-only] error : const QString

Returns a human readable description of the current error, or an empty string is there is no error.

Access functions:

QString errorString() const

[read-only] isDecoding : const bool

true if the decoder is currently running and decoding audio data.

Access functions:

bool isDecoding() const

Notifier signal:

void isDecodingChanged(bool)

source : QUrl

This property holds the active filename being decoded by the decoder object.

Access functions:

QUrl source() const
void setSource(const QUrl &fileName)

Notifier signal:

void sourceChanged()

Member Function Documentation

[explicit] QAudioDecoder::QAudioDecoder(QObject *parent = nullptr)

Construct an QAudioDecoder instance with parent.

[virtual noexcept] QAudioDecoder::~QAudioDecoder()

Destroys the audio decoder object.

QAudioFormat QAudioDecoder::audioFormat() const

Returns the audio format the decoder is set to.

Note: This may be different than the format of the decoded samples, if the audio format was set to an invalid one.

See also setAudioFormat() and formatChanged().

bool QAudioDecoder::bufferAvailable() const

Returns true if a buffer is available to be read, and false otherwise. If there is no buffer available, calling the read() function will return an invalid buffer.

Note: Getter function for property bufferAvailable.

[signal] void QAudioDecoder::bufferAvailableChanged(bool available)

Signals the availability (if available is true) of a new buffer.

If available is false, there are no buffers available.

Note: Notifier signal for property bufferAvailable.

See also bufferAvailable() and bufferReady().

[signal] void QAudioDecoder::bufferReady()

Signals that a new decoded audio buffer is available to be read.

See also read() and bufferAvailable().

qint64 QAudioDecoder::duration() const

Returns total duration (in milliseconds) of the audio stream or -1 if not available.

[signal] void QAudioDecoder::durationChanged(qint64 duration)

Signals that the estimated duration of the decoded data has changed.

See also positionChanged().

QAudioDecoder::Error QAudioDecoder::error() const

Returns the current error state of the QAudioDecoder.

[signal] void QAudioDecoder::error(QAudioDecoder::Error error)

Signals that an error condition has occurred.

Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:

connect(audioDecoder, QOverload<QAudioDecoder::Error>::of(&QAudioDecoder::error),
    [=](QAudioDecoder::Error error){ /* ... */ });

See also errorString().

[signal] void QAudioDecoder::finished()

Signals that the decoding has finished successfully. If decoding fails, error signal is emitted instead.

See also start(), stop(), and error().

[signal] void QAudioDecoder::formatChanged(const QAudioFormat &format)

Signals that the current audio format of the decoder has changed to format.

See also audioFormat() and setAudioFormat().

bool QAudioDecoder::isSupported() const

Returns true is audio decoding is supported on this platform.

qint64 QAudioDecoder::position() const

Returns position (in milliseconds) of the last buffer read from the decoder or -1 if no buffers have been read.

[signal] void QAudioDecoder::positionChanged(qint64 position)

Signals that the current position of the decoder has changed.

See also durationChanged().

QAudioBuffer QAudioDecoder::read() const

Read a buffer from the decoder, if one is available. Returns an invalid buffer if there are no decoded buffers currently available, or on failure. In both cases this function will not block.

You should either respond to the bufferReady() signal or check the bufferAvailable() function before calling read() to make sure you get useful data.

void QAudioDecoder::setAudioFormat(const QAudioFormat &format)

Set the desired audio format for decoded samples to format.

This property can only be set while the decoder is stopped. Setting this property at other times will be ignored.

If the decoder does not support this format, error() will be set to FormatError.

If you do not specify a format, the format of the decoded audio itself will be used. Otherwise, some format conversion will be applied.

If you wish to reset the decoded format to that of the original audio file, you can specify an invalid format.

Warning: Setting a desired audio format is not yet supported on the Android backend. It does work with the default FFMPEG backend.

See also audioFormat().

void QAudioDecoder::setSource(const QUrl &fileName)

Sets the current audio file name to fileName.

When this property is set any current decoding is stopped, and any audio buffers are discarded.

You can only specify either a source filename or a source QIODevice. Setting one will unset the other.

Note: Setter function for property source.

See also source().

void QAudioDecoder::setSourceDevice(QIODevice *device)

Sets the current audio QIODevice to device.

When this property is set any current decoding is stopped, and any audio buffers are discarded.

You can only specify either a source filename or a source QIODevice. Setting one will unset the other.

See also sourceDevice().

QUrl QAudioDecoder::source() const

Returns the current file name to decode. If setSourceDevice was called, this will be empty.

Note: Getter function for property source.

See also setSource().

[signal] void QAudioDecoder::sourceChanged()

Signals that the current source of the decoder has changed.

Note: Notifier signal for property source.

See also source() and sourceDevice().

QIODevice *QAudioDecoder::sourceDevice() const

Returns the current source QIODevice, if one was set. If setSource() was called, this will be a nullptr.

See also setSourceDevice().

[slot] void QAudioDecoder::start()

Starts decoding the audio resource.

As data gets decoded, the bufferReady() signal will be emitted when enough data has been decoded. Calling read() will then return an audio buffer without blocking.

If you call read() before a buffer is ready, an invalid buffer will be returned, again without blocking.

See also read().

[slot] void QAudioDecoder::stop()

Stop decoding audio. Calling start() again will resume decoding from the beginning.

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