QAudioOutput Class
The QAudioOutput class provides an interface for sending audio data to an audio output device. 更多...
头文件: | #include <QAudioOutput> |
qmake: | QT += multimedia |
基类: | QObject |
公有函数
QAudioOutput(const QAudioFormat &format = QAudioFormat(), QObject *parent = Q_NULLPTR) | |
QAudioOutput(const QAudioDeviceInfo &audioDevice, const QAudioFormat &format = QAudioFormat(), QObject *parent = Q_NULLPTR) | |
~QAudioOutput() | |
int | bufferSize() const |
int | bytesFree() const |
QString | category() const |
qint64 | elapsedUSecs() const |
QAudio::Error | error() const |
QAudioFormat | format() const |
int | notifyInterval() const |
int | periodSize() const |
qint64 | processedUSecs() const |
void | reset() |
void | resume() |
void | setBufferSize(int value) |
void | setCategory(const QString &category) |
void | setNotifyInterval(int ms) |
void | setVolume(qreal volume) |
void | start(QIODevice *device) |
QIODevice * | start() |
QAudio::State | state() const |
void | stop() |
void | suspend() |
qreal | volume() const |
- 32 个公有函数继承自 QObject
信号
void | notify() |
void | stateChanged(QAudio::State state) |
- 2 个信号继承自 QObject
其他继承的成员
- 1 个属性继承自 QObject
- 1 个公有槽函数继承自 QObject
- 1 个公有变量继承自 QObject
- 10 个静态公有成员继承自 QObject
- 9 个受保护的函数继承自 QObject
- 2 个受保护的变量继承自 QObject
详细描述
QAudioOutput 提供一个接口, 将音频数据发送到音频输出设备.
你可以使用系统默认音频输出设备构建音频输出. 你也可以使用特定的QAudioDeviceInfo创建 QAudioOutput. 创建音频输出时, 你也应该发送用于播放的 QAudioFormat (详见 QAudioFormat).
播放文件:
开始播放音频流只需要调用 start(),需要一个参数 QIODevice. 然后, QAudioOutput 从io设备获取所需数据. 因此, 播放音频文件非常简单:
QFile sourceFile; // class member. QAudioOutput* audio; // class member. { sourceFile.setFileName("/tmp/test.raw"); sourceFile.open(QIODevice::ReadOnly); QAudioFormat format; // Set up the format, eg. format.setSampleRate(8000); format.setChannelCount(1); format.setSampleSize(8); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(format)) { qWarning() << "Raw audio format not supported by backend, cannot play audio."; return; } audio = new QAudioOutput(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); audio->start(&sourceFile); }
假设音频系统和输出设备支持文件, 则文件开始播放. 如果发生错误, 调用 error() 函数检查错误原因.
文件播放完成后, 我们需要停止设备:
void AudioOutputExample::handleStateChanged(QAudio::State newState) { switch (newState) { case QAudio::IdleState: // Finished playing (no more data) audio->stop(); sourceFile.close(); delete audio; break; case QAudio::StoppedState: // Stopped for other reasons if (audio->error() != QAudio::NoError) { // Error handling } break; default: // ... other cases as appropriate break; } }
在给定的任意时间, QAudioOutput 处于四种状态之一: 活动, 暂停, 停止或空闲. 这些状态由 QAudio::State 枚举描述. 状态变化通过 stateChanged() 信号通知. 例如, 你可以使用这个信号更新应用程序的GUI; 这里常见的例子是更改播放/暂停
按钮的状态. 你可以调用 suspend(), stop(), reset(), resume(), 和 start()修改状态.
播放流时, 你可以调用setNotifyInterval()设置通知间隔(单位: ms). 这个间隔时间表示 notify() 信号发送间隔. 这与流的位置相关, 即., 如果 QAudioOutput 处于暂停或空闲时, 则不会发出 notify() 信号. 一个典型的用例是使用 slider 调整流的播放位置. 不管音频输出处于何种状态, 你可以调用 elapsedUSecs() 设置播放位置.
如果发生错误, 你可以调用error()函数获取 error type. 详见 QAudio::Error. 发生错误时, 状态变为 QAudio::StoppedState. 你可以连接 stateChanged() 信号检查错误:
void AudioOutputExample::handleStateChanged(QAudio::State newState) { switch (newState) { case QAudio::IdleState: // Finished playing (no more data) audio->stop(); sourceFile.close(); delete audio; break; case QAudio::StoppedState: // Stopped for other reasons if (audio->error() != QAudio::NoError) { // Error handling } break; default: // ... other cases as appropriate break; } }
参见 QAudioInput and QAudioDeviceInfo.
成员函数
QAudioOutput::QAudioOutput(const QAudioFormat &format = QAudioFormat(), QObject *parent = Q_NULLPTR)
Construct a new audio output and attach it to parent. The default audio output device is used with the output format parameters.
QAudioOutput::QAudioOutput(const QAudioDeviceInfo &audioDevice, const QAudioFormat &format = QAudioFormat(), QObject *parent = Q_NULLPTR)
Construct a new audio output and attach it to parent. The device referenced by audioDevice is used with the output format parameters.
QAudioOutput::~QAudioOutput()
Destroys this audio output.
This will release any system resources used and free any buffers.
int QAudioOutput::bufferSize() const
Returns the audio buffer size in bytes.
If called before start(), returns platform default value. If called before start() but setBufferSize() was called prior, returns value set by setBufferSize(). If called after start(), returns the actual buffer size being used. This may not be what was set previously by setBufferSize().
参见 setBufferSize().
int QAudioOutput::bytesFree() const
Returns the number of free bytes available in the audio buffer.
Note: The returned value is only valid while in QAudio::ActiveState or QAudio::IdleState state, otherwise returns zero.
QString QAudioOutput::category() const
Returns the audio category of this audio stream.
Some platforms can group audio streams into categories and manage their volumes independently, or display them in a system mixer control. You can set this property to allow the platform to distinguish the purpose of your streams.
参见 setCategory().
qint64 QAudioOutput::elapsedUSecs() const
Returns the microseconds since start() was called, including time in Idle and Suspend states.
QAudio::Error QAudioOutput::error() const
Returns the error state.
QAudioFormat QAudioOutput::format() const
Returns the QAudioFormat being used.
[signal]
void QAudioOutput::notify()
This signal is emitted when a certain interval of milliseconds of audio data has been processed. The interval is set by setNotifyInterval().
int QAudioOutput::notifyInterval() const
Returns the notify interval in milliseconds.
参见 setNotifyInterval().
int QAudioOutput::periodSize() const
Returns the period size in bytes. This is the amount of data required each period to prevent buffer underrun, and to ensure uninterrupted playback.
Note: It is recommended to provide at least enough data for a full period with each write operation.
qint64 QAudioOutput::processedUSecs() const
Returns the amount of audio data processed since start() was called (in microseconds).
void QAudioOutput::reset()
Drops all audio data in the buffers, resets buffers to zero.
void QAudioOutput::resume()
Resumes processing audio data after a suspend().
Sets error() to QAudio::NoError. Sets state() to QAudio::ActiveState if you previously called start(QIODevice*). Sets state() to QAudio::IdleState if you previously called start(). emits stateChanged() signal.
void QAudioOutput::setBufferSize(int value)
Sets the audio buffer size to value in bytes.
Note: This function can be called anytime before start(). Calls to this are ignored after start(). It should not be assumed that the buffer size set is the actual buffer size used - call bufferSize() anytime after start() to return the actual buffer size being used.
参见 bufferSize().
void QAudioOutput::setCategory(const QString &category)
Sets the audio category of this audio stream to category.
Some platforms can group audio streams into categories and manage their volumes independently, or display them in a system mixer control. You can set this property to allow the platform to distinguish the purpose of your streams.
Not all platforms support audio stream categorization. In this case, the function call will be ignored.
Changing an audio output stream's category while it is opened will not take effect until it is reopened.
参见 category().
void QAudioOutput::setNotifyInterval(int ms)
Sets the interval for notify() signal to be emitted. This is based on the ms of audio data processed, not on wall clock time. The minimum resolution of the timer is platform specific and values should be checked with notifyInterval() to confirm the actual value being used.
参见 notifyInterval().
void QAudioOutput::setVolume(qreal volume)
Sets the output volume to volume.
The volume is scaled linearly from 0.0
(silence) to 1.0
(full volume). Values outside this range will be clamped.
The default volume is 1.0
.
Note: Adjustments to the volume will change the volume of this audio stream, not the global volume.
UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale will produce linear changes in perceived loudness, which is what a user would normally expect from a volume control. See QAudio::convertVolume() for more details.
参见 volume().
void QAudioOutput::start(QIODevice *device)
Starts transferring audio data from the device to the system's audio output. The device must have been opened in the ReadOnly or ReadWrite modes.
If the QAudioOutput is able to successfully output audio data, state() returns QAudio::ActiveState, error() returns QAudio::NoError and the stateChanged() signal is emitted.
If a problem occurs during this process, error() returns QAudio::OpenError, state() returns QAudio::StoppedState and the stateChanged() signal is emitted.
参见 QIODevice.
QIODevice *QAudioOutput::start()
Returns a pointer to the internal QIODevice being used to transfer data to the system's audio output. The device will already be open and write() can write data directly to it.
Note: The pointer will become invalid after the stream is stopped or if you start another stream.
If the QAudioOutput is able to access the system's audio device, state() returns QAudio::IdleState, error() returns QAudio::NoError and the stateChanged() signal is emitted.
If a problem occurs during this process, error() returns QAudio::OpenError, state() returns QAudio::StoppedState and the stateChanged() signal is emitted.
参见 QIODevice.
QAudio::State QAudioOutput::state() const
Returns the state of audio processing.
[signal]
void QAudioOutput::stateChanged(QAudio::State state)
This signal is emitted when the device state has changed. This is the current state of the audio output.
void QAudioOutput::stop()
Stops the audio output, detaching from the system resource.
Sets error() to QAudio::NoError, state() to QAudio::StoppedState and emit stateChanged() signal.
void QAudioOutput::suspend()
Stops processing audio data, preserving buffered audio data.
Sets error() to QAudio::NoError, state() to QAudio::SuspendedState and emits stateChanged() signal.
qreal QAudioOutput::volume() const
Returns the volume between 0.0 and 1.0 inclusive.
参见 setVolume().