QMediaRecorder Class

The QMediaRecorder class is used for encoding and recording a capture session. More...

Header: #include <QMediaRecorder>
CMake: find_package(Qt6 COMPONENTS Multimedia REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
Instantiated By: MediaRecorder
Inherits: QObject

Public Types

enum EncodingMode { ConstantQualityEncoding, ConstantBitRateEncoding, AverageBitRateEncoding, TwoPassEncoding }
enum Error { NoError, ResourceError, FormatError, OutOfSpaceError, LocationNotWritable }
enum Quality { VeryLowQuality, LowQuality, NormalQuality, HighQuality, VeryHighQuality }
enum RecorderState { StoppedState, RecordingState, PausedState }

Properties

Public Functions

QMediaRecorder(QObject *parent = nullptr)
virtual ~QMediaRecorder()
QUrl actualLocation() const
int audioBitRate() const
int audioChannelCount() const
int audioSampleRate() const
qint64 duration() const
QMediaRecorder::EncodingMode encodingMode() const
QMediaRecorder::Error error() const
QString errorString() const
bool isAvailable() const
QMediaFormat mediaFormat() const
QMediaMetaData metaData() const
QUrl outputLocation() const
QMediaRecorder::Quality quality() const
QMediaRecorder::RecorderState recorderState() const
void setAudioBitRate(int bitRate)
void setAudioChannelCount(int channels)
void setAudioSampleRate(int sampleRate)
void setEncodingMode(QMediaRecorder::EncodingMode mode)
void setMediaFormat(const QMediaFormat &format)
void setMetaData(const QMediaMetaData &metaData)
void setOutputLocation(const QUrl &location)
void setQuality(QMediaRecorder::Quality quality)
void setVideoBitRate(int bitRate)
void setVideoFrameRate(qreal frameRate)
void setVideoResolution(const QSize &size)
void setVideoResolution(int width, int height)
int videoBitRate() const
qreal videoFrameRate() const
QSize videoResolution() const

Public Slots

void pause()
void record()
void stop()

Signals

void actualLocationChanged(const QUrl &location)
void durationChanged(qint64 duration)
void errorChanged()
void errorOccurred(QMediaRecorder::Error error, const QString &errorString)
void mediaFormatChanged()
void metaDataChanged()
void recorderStateChanged(QMediaRecorder::RecorderState state)

Detailed Description

The QMediaRecorder class is a class for encoding and recording media generated in a QMediaCaptureSession.

QMediaCaptureSession session;
QAudioInput audioInput;
session.setAudioInput(&input);
QMediaRecorder recorder;
session.setRecorder(&recorder);
recorder.setQuality(QMediaRecorder::HighQuality);
recorder.setOutputLocation(QUrl::fromLocalFile("test.mp3"));
recorder.record();

Member Type Documentation

enum QMediaRecorder::EncodingMode

Enumerates encoding modes.

ConstantValueDescription
QMediaRecorder::ConstantQualityEncoding0Encoding will aim to have a constant quality, adjusting bitrate to fit.
QMediaRecorder::ConstantBitRateEncoding1Encoding will use a constant bit rate, adjust quality to fit.
QMediaRecorder::AverageBitRateEncoding2Encoding will try to keep an average bitrate setting, but will use more or less as needed.
QMediaRecorder::TwoPassEncoding3The media will first be processed to determine the characteristics, and then processed a second time allocating more bits to the areas that need it.

enum QMediaRecorder::Error

ConstantValueDescription
QMediaRecorder::NoError0No Errors.
QMediaRecorder::ResourceError1Device is not ready or not available.
QMediaRecorder::FormatError2Current format is not supported.
QMediaRecorder::OutOfSpaceError3No space left on device.
QMediaRecorder::LocationNotWritable4The output location is not writable.

enum QMediaRecorder::Quality

Enumerates quality encoding levels.

ConstantValue
QMediaRecorder::VeryLowQuality0
QMediaRecorder::LowQuality1
QMediaRecorder::NormalQuality2
QMediaRecorder::HighQuality3
QMediaRecorder::VeryHighQuality4

enum QMediaRecorder::RecorderState

ConstantValueDescription
QMediaRecorder::StoppedState0The recorder is not active.
QMediaRecorder::RecordingState1The recording is requested.
QMediaRecorder::PausedState2The recorder is paused.

Property Documentation

[read-only] actualLocation : const QUrl

This property holds the actual location of the last media content.

The actual location is usually available after recording starts, and reset when new location is set or new recording starts.

Access functions:

QUrl actualLocation() const

Notifier signal:

void actualLocationChanged(const QUrl &location)

[read-only] duration : const qint64

This property holds the recorded media duration in milliseconds.

Access functions:

qint64 duration() const

Notifier signal:

void durationChanged(qint64 duration)

outputLocation : QUrl

This property holds the destination location of media content.

Setting the location can fail, for example when the service supports only local file system locations but a network URL was passed. If the operation fails an errorOccured() signal is emitted.

The output location can be relative or empty; in the latter case the recorder uses the system specific place and file naming scheme.

Access functions:

QUrl outputLocation() const
void setOutputLocation(const QUrl &location)

[read-only] recorderState : const QMediaRecorder::RecorderState

This property holds the current state of the media recorder.

The state property represents the user request and is changed synchronously during record(), pause() or stop() calls. Recorder state may also change asynchronously when recording fails.

Access functions:

QMediaRecorder::RecorderState recorderState() const

Notifier signal:

void recorderStateChanged(QMediaRecorder::RecorderState state)

Member Function Documentation

QMediaRecorder::QMediaRecorder(QObject *parent = nullptr)

Constructs a media recorder which records the media produced by a microphone and camera. The media recorder is a child of parent.

[signal] void QMediaRecorder::actualLocationChanged(const QUrl &location)

Signals that the actual location of the recorded media has changed. This signal is usually emitted when recording starts.

Note: Notifier signal for property actualLocation.

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

Signals that the duration of the recorded media has changed.

Note: Notifier signal for property duration.

[signal] void QMediaRecorder::errorOccurred(QMediaRecorder::Error error, const QString &errorString)

Signals that an error has occurred, with errorString containing a description of the error.

[signal] void QMediaRecorder::metaDataChanged()

Signals that a media object's meta-data has changed.

If multiple meta-data elements are changed metaDataChanged() is emitted once.

Note: Notifier signal for property metaData.

[slot] void QMediaRecorder::pause()

Pauses recording.

The recorder state is changed to QMediaRecorder::PausedState.

Depending on the platform, pausing recording may be not supported. In this case the recorder state is unchanged.

[slot] void QMediaRecorder::record()

Start recording.

While the recorder state is changed immediately to c{QMediaRecorder::RecordingState}, recording may start asynchronously.

If recording fails error() signal is emitted with recorder state being reset back to QMediaRecorder::StoppedState.

Note: On mobile devices, recording will happen in the orientation the device had when calling record and is locked for the duration of the recording. To avoid artifacts on the user interface, we recommend to keep the user interface locked to the same orientation as long as the recording is ongoing using the contentOrientation property of QWindow and unlock it again once the recording is finished.

[signal] void QMediaRecorder::recorderStateChanged(QMediaRecorder::RecorderState state)

Signals that a media recorder's state has changed.

Note: Notifier signal for property recorderState.

[slot] void QMediaRecorder::stop()

Stops recording.

The recorder state is changed to QMediaRecorder::StoppedState.

[virtual] QMediaRecorder::~QMediaRecorder()

Destroys a media recorder object.

int QMediaRecorder::audioBitRate() const

Returns the bit rate of the compressed audio stream in bits per second.

See also setAudioBitRate().

int QMediaRecorder::audioChannelCount() const

Returns the number of audio channels.

See also setAudioChannelCount().

int QMediaRecorder::audioSampleRate() const

Returns the audio sample rate in Hz.

See also setAudioSampleRate().

QMediaRecorder::EncodingMode QMediaRecorder::encodingMode() const

Returns the encoding mode.

See also setEncodingMode() and EncodingMode.

QMediaRecorder::Error QMediaRecorder::error() const

Returns the current error state.

Note: Getter function for property error.

See also errorString().

QString QMediaRecorder::errorString() const

Returns a string describing the current error state.

Note: Getter function for property errorString.

See also error().

bool QMediaRecorder::isAvailable() const

Returns true if media recorder service ready to use.

QMediaMetaData QMediaRecorder::metaData() const

Returns the metaData associated with the recording.

Note: Getter function for property metaData.

See also setMetaData().

QMediaRecorder::RecorderState QMediaRecorder::recorderState() const

Returns the current media recorder state.

Note: Getter function for property recorderState.

See also QMediaRecorder::RecorderState.

void QMediaRecorder::setAudioBitRate(int bitRate)

Sets the audio bitRate in bits per second.

See also audioBitRate().

void QMediaRecorder::setAudioChannelCount(int channels)

Sets the number of audio channels.

A value of -1 indicates the recorder should make an optimal choice based on what is available from the audio source and the limitations of the codec.

See also audioChannelCount().

void QMediaRecorder::setAudioSampleRate(int sampleRate)

Sets the audio sampleRate in Hz.

A value of -1 indicates the recorder should make an optimal choice based on what is available from the audio source, and the limitations of the codec.

See also audioSampleRate().

void QMediaRecorder::setEncodingMode(QMediaRecorder::EncodingMode mode)

Sets the encoding mode setting.

If ConstantQualityEncoding is set, the quality encoding parameter is used and bit rates are ignored, otherwise the bitrates are used.

See also encodingMode() and EncodingMode.

void QMediaRecorder::setMetaData(const QMediaMetaData &metaData)

Sets the meta data to metaData.

Note: To ensure that meta-data is set correctly, it should be set before starting the recording. Once the recording is started, any meta-data set will be attached to the next recording.

Note: Setter function for property metaData.

See also metaData().

void QMediaRecorder::setVideoBitRate(int bitRate)

Sets the video bitRate in bits per second.

See also videoBitRate().

void QMediaRecorder::setVideoFrameRate(qreal frameRate)

Sets the video frameRate.

A value of 0 indicates the recorder should make an optimal choice based on what is available from the video source and the limitations of the codec.

See also videoFrameRate().

void QMediaRecorder::setVideoResolution(const QSize &size)

Sets the resolution of the encoded video to size.

Pass an empty QSize to make the recorder choose an optimal resolution based on what is available from the video source and the limitations of the codec.

See also videoResolution().

void QMediaRecorder::setVideoResolution(int width, int height)

Sets the width and height of the resolution of the encoded video.

This is an overloaded function.

int QMediaRecorder::videoBitRate() const

Returns the bit rate of the compressed video stream in bits per second.

See also setVideoBitRate().

qreal QMediaRecorder::videoFrameRate() const

Returns the video frame rate.

See also setVideoFrameRate().

QSize QMediaRecorder::videoResolution() const

Returns the resolution of the encoded video.

See also setVideoResolution().

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