本页内容

QAudioDecoder Class

QAudioDecoder 类实现了音频解码功能。更多内容...

头文件: #include <QAudioDecoder>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
继承自: QObject
状态: 技术预览

该类处于技术预览阶段,内容可能会有变动。

公共类型

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

属性

公共函数

QAudioDecoder(QObject *parent = nullptr)
virtual ~QAudioDecoder() override
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

公共槽

void start()
void stop()

信号

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()

详细说明

QAudioDecoder 类是一个用于解码音频媒体文件的高级类。它与QMediaPlayer 类类似,不同之处在于音频是通过此 API 返回的,而不是直接路由到音频硬件。

另请参阅 QAudioBuffer 。

成员类型文档

enum QAudioDecoder::Error

定义媒体播放器的错误状态。

常量值描述
QAudioDecoder::NoError0未发生错误。
QAudioDecoder::ResourceError1无法解析媒体资源。
QAudioDecoder::FormatError2媒体资源的格式不受支持。
QAudioDecoder::AccessDeniedError3没有播放该媒体资源的相应权限。
QAudioDecoder::NotSupportedError4QAudioDecoder 此功能在此平台上不受支持

属性文档

[read-only] bufferAvailable : bool

该属性表示是否存在可用的解码音频缓冲区

访问函数:

bool bufferAvailable() const

通知信号:

void bufferAvailableChanged(bool available)

[read-only] error : QString

返回当前错误的人类可读描述;如果没有错误,则返回空字符串。

访问函数:

QString errorString() const

[read-only] isDecoding : bool

true 如果解码器当前正在运行并解码音频数据。

访问函数:

bool isDecoding() const

通知信号:

void isDecodingChanged(bool)

source : QUrl

该属性存储解码器对象当前正在解码的文件名。

访问函数:

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

通知器信号:

void sourceChanged()

成员函数文档

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

使用parent 构建一个QAudioDecoder实例。

[override virtual noexcept] QAudioDecoder::~QAudioDecoder()

销毁音频解码器对象。

QAudioFormat QAudioDecoder::audioFormat() const

返回解码器当前设置的音频格式。

注意: 如果音频格式被设置为无效格式,则此值 可能与解码后样本的格式不同。

另请参阅 setAudioFormat() 和formatChanged()。

bool QAudioDecoder::bufferAvailable() const

如果存在可读取的缓冲区,则返回 true;否则返回 false。如果没有可用的缓冲区,调用read() 函数将返回一个无效的缓冲区。

注意: 这是属性 bufferAvailable 的获取器 函数。

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

指示新缓冲区是否可用(若available 为真)。

如果available 为 false,则没有可用的缓冲区。

注意: 这是属性bufferAvailable 的通知器 信号。

另请参阅 bufferAvailable() 和bufferReady()。

[signal] void QAudioDecoder::bufferReady()

表示有一个新的解码音频缓冲区可供读取。

另请参阅 read() 和bufferAvailable()。

qint64 QAudioDecoder::duration() const

返回音频流的总时长(以毫秒为单位);如果无法获取,则返回 -1。

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

表示解码数据的估计duration 值已发生变化。

另请参阅 positionChanged()。

QAudioDecoder::Error QAudioDecoder::error() const

返回QAudioDecoder 的当前错误状态。

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

表示已发生error 错误。

注意:此 信号已被重载。要连接到此信号:

// Connect using qOverload:
connect(audioDecoder, qOverload(&QAudioDecoder::error),
        receiver, &ReceiverClass::slot);

// Or using a lambda:
connect(audioDecoder, qOverload(&QAudioDecoder::error),
        this, [](QAudioDecoder::Error error) { /* handle error */ });
有关更多示例和方法,请参阅“连接到重载信号”。

另请参阅 errorString()。

[signal] void QAudioDecoder::finished()

表示解码已成功完成。如果解码失败,则会发出错误信号。

另请参阅 start()、stop() 和error()。

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

表示解码器的当前音频格式已更改为format 。

另请参阅 audioFormat() 和setAudioFormat()。

bool QAudioDecoder::isSupported() const

如果该平台支持音频解码,则返回 true。

qint64 QAudioDecoder::position() const

返回从解码器读取的最后一个缓冲区的位置(单位为毫秒);如果尚未读取任何缓冲区,则返回 -1。

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

表示解码器当前的position 已发生变化。

另请参阅 durationChanged()。

QAudioBuffer QAudioDecoder::read() const

从解码器读取一个缓冲区(如果可用)。如果当前没有可用的已解码缓冲区,或者操作失败,则返回一个无效缓冲区。在这两种情况下,该函数都不会阻塞。

在调用 read() 之前,您应响应bufferReady() 信号,或检查bufferAvailable() 函数,以确保获取到有效数据。

void QAudioDecoder::setAudioFormat(const QAudioFormat &format)

将解码后采样值的音频格式设置为format 。

该属性仅可在解码器停止运行时设置。在其他情况下设置此属性将被忽略。

如果解码器不支持此格式,则error()将被设置为FormatError 。

如果未指定格式,则将使用解码音频本身的格式。否则,将进行一些格式转换。

若希望将解码后的格式重置为原始音频文件的格式,可指定一个无效的format 。

警告: Android 后端目前尚不支持设置 所需音频格式。但在默认的 FFMPEG 后端中该功能有效。

另请参阅 audioFormat()。

void QAudioDecoder::setSource(const QUrl &fileName)

将当前音频文件名设置为fileName 。

设置此属性时,将停止当前的任何解码操作,并丢弃所有音频缓冲区。

您只能指定源文件名或源QIODevice 。设置其中一个将自动取消设置另一个。

注意: 属性source 的设置 函数。

另请参阅 source()。

void QAudioDecoder::setSourceDevice(QIODevice *device)

将当前音频QIODevice 设置为device 。

设置此属性时,将停止当前的任何解码操作,并丢弃所有音频缓冲区。

您只能指定源文件名或源QIODevice 。设置其中一个将自动取消设置另一个。

另请参阅 sourceDevice()。

QUrl QAudioDecoder::source() const

返回待解码的当前文件名。如果调用了setSourceDevice ,则该值将为空。

注意: 这是 source 属性的获取 函数。

另请参阅 setSource()。

[signal] void QAudioDecoder::sourceChanged()

表示解码器的电流源已发生变化。

注意: 这是属性source的通知 信号。

另请参阅 source() 和sourceDevice()。

QIODevice *QAudioDecoder::sourceDevice() const

返回当前源QIODevice ,如果已设置该源。如果调用了setSource(),则该值将为nullptr。

另请参阅 setSourceDevice()。

[slot] void QAudioDecoder::start()

开始解码音频资源。

随着数据的解码,当解码的数据量足够多时,将触发bufferReady()信号。此时调用read()将返回一个音频缓冲区,且不会阻塞。

如果在缓冲区准备就绪之前调用read(),将返回一个无效的缓冲区,此操作同样不会阻塞。

另请参阅 read()。

[slot] void QAudioDecoder::stop()

停止音频解码。再次调用start() 将从头开始继续解码。

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