QAudioDecoder Class

QAudioDecoder 类实现音频解码。更多

头文件: #include <QAudioDecoder>
CMake.QAudioDecoder 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 : const bool

此属性表示是否有可用的解码音频缓冲区

访问功能

bool bufferAvailable() const

通知信号

void bufferAvailableChanged(bool available)

[read-only] error : const QString

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

访问功能:

QString errorString() const

[read-only] isDecoding : const bool

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

访问功能:

bool isDecoding() const

通知信号:

void isDecodingChanged(bool)

source : QUrl

此属性保存解码器对象正在解码的活动文件名。

访问功能:

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

Notifier 信号:

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 为 true)。

如果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 情况的信号。

注意:该类重载了信号error。要使用函数指针语法连接到该信号,Qt 提供了一个方便的辅助工具来获取函数指针,如本例所示:

connect(audioDecoder, QOverload<QAudioDecoder::Error>::of(&QAudioDecoder::error),
    [=](QAudioDecoder::Error 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

警告: 安卓后台尚不支持设置所需的音频格式。默认的 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 ,则为空。

注: 属性源的获取函数。

另请参阅 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() 将从头开始恢复解码。

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