QMediaRecorder 类

QMediaRecorder 类用于编码和录制捕获会话。 更多...

头: #include <QMediaRecorder>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
在 QML: MediaRecorder
继承: QObject

公共类型

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 }

特性

公共函数

QMediaRecorder (QObject * parent = nullptr)
virtual ~QMediaRecorder () override
QUrl actualLocation () const
void addMetaData (const QMediaMetaData & metaData )
int audioBitRate () const
int audioChannelCount () const
int audioSampleRate () const
bool autoStop () const
QMediaCaptureSession * captureSession () const
qint64 duration () const
QMediaRecorder::EncodingMode encodingMode () const
QMediaRecorder::Error error () const
QString errorString () const
bool isAvailable () const
QMediaFormat mediaFormat () const
QMediaMetaData metaData () const
QIODevice * outputDevice () 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 setAutoStop (bool autoStop )
void setEncodingMode (QMediaRecorder::EncodingMode mode )
void setMediaFormat (const QMediaFormat & format )
void setMetaData (const QMediaMetaData & metaData )
void setOutputDevice (QIODevice * device )
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

公共槽

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

信号

void actualLocationChanged (const QUrl & location )
void audioBitRateChanged ()
void audioChannelCountChanged ()
void audioSampleRateChanged ()
void autoStopChanged ()
void durationChanged (qint64 duration )
void encoderSettingsChanged ()
void encodingModeChanged ()
void errorChanged ()
void errorOccurred (QMediaRecorder::Error error , const QString & errorString )
void mediaFormatChanged ()
void metaDataChanged ()
void qualityChanged ()
void recorderStateChanged (QMediaRecorder::RecorderState state )
void videoBitRateChanged ()
void videoFrameRateChanged ()
void videoResolutionChanged ()

详细描述

Use the QMediaRecorder class to encode and record media generated in QMediaCaptureSession . You can generate:

To record media, connect a generator to a corresponding media capture session.

Performance of video encoding and recording is limited by the hardware, the operating system, the installed graphic drivers, and the input video format. If QCamera , QScreenCapture ,或 QWindowCapture produces video frames faster than QMediaRecorder can encode and record them, the recorder may drop some frames. This is likely to occur if the input frame resolution is high, 4K for example, and hardware-accelerated encoding is unavailable. If you generate input video via QVideoFrameInput , the method QVideoFrameInput::sendVideoFrame will do nothing and return false whenever this limitation is reached and the internal frame queue is full. Rely on the signal QVideoFrameInput::readyToSendVideoFrame to know when the recorder is ready to receive new frames again. If you cannot change the rate of video frame generation and dropping frames is undesirable, we recommend implementing your own frame queue on top of QVideoFrameInput , considering the memory limitations of the hardware.

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

成员类型文档编制

enum QMediaRecorder:: EncodingMode

枚举编码模式。

常量 描述
QMediaRecorder::ConstantQualityEncoding 0 编码的目标是拥有常量品质,调节比特率以拟合。
QMediaRecorder::ConstantBitRateEncoding 1 编码将使用常量比特率,调节品质以拟合。
QMediaRecorder::AverageBitRateEncoding 2 编码将试着保持平均比特率设置,但会根据需要使用更多 (或更少)。
QMediaRecorder::TwoPassEncoding 3 The 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

常量 描述
QMediaRecorder::NoError 0 没有错误。
QMediaRecorder::ResourceError 1 设备未就绪 (或不可用)。
QMediaRecorder::FormatError 2 不支持当前格式。
QMediaRecorder::OutOfSpaceError 3 设备没有剩余空间。
QMediaRecorder::LocationNotWritable 4 输出位置不可写。

enum QMediaRecorder:: Quality

枚举品质编码级别。

常量
QMediaRecorder::VeryLowQuality 0
QMediaRecorder::LowQuality 1
QMediaRecorder::NormalQuality 2
QMediaRecorder::HighQuality 3
QMediaRecorder::VeryHighQuality 4

enum QMediaRecorder:: RecorderState

常量 描述
QMediaRecorder::StoppedState 0 录制器不活动。
QMediaRecorder::RecordingState 1 请求录制。
QMediaRecorder::PausedState 2 录制器暂停。

特性文档编制

[read-only] actualLocation : const QUrl

此特性保持最后媒体内容的实际位置。

The actual location is reset when a new outputLocation or a non-null outputDevice is assigned. When record () is invoked and outputDevice is null or not writable, the recorder generates the actual location basing on the following rules.

  • outputLocation is empty, a directory, or a file without an extension, the recorder generates the appropriate extension based on the selected media format and system MIME types.
  • outputLocation is a directory, the recorder generates a new file name within it.
  • outputLocation is empty, the recorder generates a new file name in the system-specific directory for audio or video.
  • The recorder generates the actual location before emitting recorderStateChanged(RecordingState) .

访问函数:

QUrl actualLocation () const

通知程序信号:

void actualLocationChanged (const QUrl & location )

autoStop : bool

This property controls whether the media recorder stops automatically when all media inputs have reported the end of the stream or have been deactivated.

The end of the stream is reported by sending an empty media frame, which you can send explicitly via QVideoFrameInput or QAudioBufferInput .

Video inputs, specificly, QCamera , QScreenCapture and QWindowCapture , can be deactivated via the function setActive .

默认为 false .

QMediaRecorder::autoStop is only supported with the FFmpeg backend.

访问函数:

bool autoStop () const
void setAutoStop (bool autoStop )

通知程序信号:

void autoStopChanged ()

另请参阅 QCamera , QScreenCapture ,和 QWindowCapture .

[read-only] duration : const qint64

此特性保持录制媒体的持续时间 (以毫秒为单位)。

访问函数:

qint64 duration () const

通知程序信号:

void durationChanged (qint64 duration )

[read-only] error : const QMediaRecorder::Error

返回当前错误状态。

访问函数:

QMediaRecorder::Error error () const

通知程序信号:

void errorChanged ()

另请参阅 errorString ().

[read-only] errorString : const QString

返回描述当前错误状态的字符串。

访问函数:

QString errorString () const

通知程序信号:

void errorChanged ()

另请参阅 error ().

mediaFormat : QMediaFormat

This property holds the current QMediaFormat of the recorder.

The value of this property may change when invoking record (). If this happens, the mediaFormatChanged() signal will be emitted. This will always happen if the QMediaFormat::audioCodec or QMediaFormat::fileFormat properties are set to unspecified. If a video source ( QCamera , QScreenCapture ,或 QVideoFrameInput ) is connected to the QMediaCaptureSession , QMediaFormat::videoCodec must also be specified. The QMediaFormat::audioCodec and QMediaFormat::videoCodec property values may also change if the media backend does not support the selected file format or codec.

The QMediaFormat::fileFormat property value may also change to an audio only format if a video format was requested, but QMediaCaptureSession does not have a video source connected. For example, if QMediaFormat::fileFormat 被设为 QMediaFormat::MPEG4 , it may be changed to QMediaFormat::Mpeg4Audio .

Applications can determine if mediaFormat will change before recording starts by calling the QMediaFormat::isSupported () function. When recording without any video inputs, record () will not be changed the QMediaFormat if the following is true:

When recording with video input, mediaFormat will not be changed if the following is true:

注意: The QMediaRecorder does not take the file name extension from the outputLocation property into account when determining the QMediaFormat::fileFormat , and will not adjust the extension of the outputLocation QUrl to match the selected file format if an extension is specified. Applications should therefore make sure to set the QMediaRecorder::mediaFormat::fileFormat to match the file extension, or not specify a file extension. If no file extension is specified, the actualLocation file extension will be updated to match the file format used for recording.

访问函数:

QMediaFormat mediaFormat () const
void setMediaFormat (const QMediaFormat & format )

通知程序信号:

void mediaFormatChanged ()

另请参阅 QMediaFormat::isSupported () 和 actualLocation .

metaData : QMediaMetaData

返回录制关联的 metaData。

访问函数:

QMediaMetaData metaData () const
void setMetaData (const QMediaMetaData & metaData )

通知程序信号:

void metaDataChanged ()

outputLocation : QUrl

此特性保持媒体内容的目的地位置。

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, the errorOccured() signal is emitted.

The output location is ignored if a writable outputDevice has been assigned to the recorder. This behavior may change in the future, so we recommend setting only one output, either outputLocation or outputDevice .

The output location can be empty, a directory, or a file. The path to a directory or file can be relative or absolute. The record () method generates the actual location according to the specified output location and system-specific settings. Refer to the actualLocation property description for details.

访问函数:

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

另请参阅 actualLocation and outputDevice ().

quality : Quality

返回录制品质。

访问函数:

QMediaRecorder::Quality quality () const
void setQuality (QMediaRecorder::Quality quality )

通知程序信号:

void qualityChanged ()

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

此特性保持媒体录制器的当前状态。

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

访问函数:

QMediaRecorder::RecorderState recorderState () const

通知程序信号:

void recorderStateChanged (QMediaRecorder::RecorderState state )

成员函数文档编制

QMediaRecorder:: QMediaRecorder ( QObject * parent = nullptr)

Constructs a media recorder. The media recorder is a child of parent .

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

销毁媒体录制器对象。

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

注意: 通知程序信号对于特性 actualLocation .

void QMediaRecorder:: addMetaData (const QMediaMetaData & metaData )

添加 metaData 到录制媒体。

int QMediaRecorder:: audioBitRate () const

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

注意: getter 函数对于特性 audioBitRate。

另请参阅 setAudioBitRate ().

[signal] void QMediaRecorder:: audioBitRateChanged ()

Signals when the recording audio bit rate changes.

注意: 通知程序信号对于特性 audioBitRate .

int QMediaRecorder:: audioChannelCount () const

Returns the number of audio channels.

注意: Getter function for property audioChannelCount.

另请参阅 setAudioChannelCount ().

[signal] void QMediaRecorder:: audioChannelCountChanged ()

Signals when the recording audio channel count changes.

注意: 通知程序信号对于特性 audioChannelCount .

int QMediaRecorder:: audioSampleRate () const

Returns the audio sample rate in Hz.

注意: Getter function for property audioSampleRate.

另请参阅 setAudioSampleRate ().

[signal] void QMediaRecorder:: audioSampleRateChanged ()

Signals when the recording audio sample rate changes.

注意: 通知程序信号对于特性 audioSampleRate .

QMediaCaptureSession *QMediaRecorder:: captureSession () const

Returns the media capture session.

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

发射信号,当 duration of the recorded media has changed.

注意: 通知程序信号对于特性 duration .

[signal] void QMediaRecorder:: encoderSettingsChanged ()

Signals when the encoder settings change.

QMediaRecorder::EncodingMode QMediaRecorder:: encodingMode () const

Returns the encoding mode.

注意: Getter function for property encodingMode.

另请参阅 setEncodingMode () 和 EncodingMode .

[signal] void QMediaRecorder:: encodingModeChanged ()

Signals when the encoding mode changes.

注意: 通知程序信号对于特性 encodingMode .

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

发射信号,当 error has occurred, with errorString containing a description of the error.

bool QMediaRecorder:: isAvailable () const

返回 true if media recorder service ready to use.

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

注意: 通知程序信号对于特性 metaData .

QIODevice *QMediaRecorder:: outputDevice () const

Returns the output IO device for media content.

另请参阅 setOutputDevice ().

[slot] void QMediaRecorder:: pause ()

暂停录制。

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.

[signal] void QMediaRecorder:: qualityChanged ()

Signals when the recording quality changes.

注意: 通知程序信号对于特性 quality .

[slot] void QMediaRecorder:: record ()

Starts recording.

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

若录制失败 error () signal is emitted with recorder state being reset back to QMediaRecorder::StoppedState .

This method updates actualLocation according to its generation rules.

注意: 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.

QMediaRecorder::RecorderState QMediaRecorder:: recorderState () const

Returns the current media recorder state.

注意: getter 函数对于特性 recorderState。

另请参阅 QMediaRecorder::RecorderState .

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

Signals that a media recorder's state 已改变。

注意: 通知程序信号对于特性 recorderState .

void QMediaRecorder:: setAudioBitRate ( int bitRate )

设置音频 bitRate 以位每秒为单位。

注意: setter 函数对于特性 audioBitRate .

另请参阅 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.

注意: setter 函数对于特性 audioChannelCount .

另请参阅 audioChannelCount ().

void QMediaRecorder:: setAudioSampleRate ( int sampleRate )

设置音频 sampleRate (以 Hz 为单位)。

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

注意: setter 函数对于特性 audioSampleRate .

另请参阅 audioSampleRate ().

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

设置编码 mode 设置。

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

注意: setter 函数对于特性 encodingMode .

另请参阅 encodingMode () 和 EncodingMode .

void QMediaRecorder:: setMetaData (const QMediaMetaData & metaData )

将元数据设为 metaData .

注意: 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.

注意: setter 函数对于特性 metaData .

另请参阅 metaData ().

void QMediaRecorder:: setOutputDevice ( QIODevice * device )

Set the output IO device for media content.

The device 必须已打开以 WriteOnly or ReadWrite modes before the recording starts.

The media recorder doesn't take ownership of the specified device . If the recording has been started, the device must be kept alive and open until the signal recorderStateChanged(StoppedState) is emitted.

This method resets actualLocation immediately unless the specified device is null .

If a writable output device is assigned to the recorder, outputLocation is ignored, and actualLocation is not generated when recording starts. This behavior may change in the future, so we recommend setting only one output, either outputLocation or outputDevice .

QMediaRecorder::setOutputDevice is only supported with the FFmpeg backend.

另请参阅 outputDevice () 和 outputLocation .

void QMediaRecorder:: setVideoBitRate ( int bitRate )

设置视频 bitRate 以位每秒为单位。

注意: setter 函数对于特性 videoBitRate .

另请参阅 videoBitRate ().

void QMediaRecorder:: setVideoFrameRate ( qreal frameRate )

设置视频 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.

注意: setter 函数对于特性 videoFrameRate .

另请参阅 videoFrameRate ().

void QMediaRecorder:: setVideoResolution (const QSize & size )

Sets the resolution of the encoded video to size .

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

注意: setter 函数对于特性 videoResolution .

另请参阅 videoResolution ().

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

设置 width and height of the resolution of the encoded video.

这是重载函数。

注意: setter 函数对于特性 videoResolution .

[slot] void QMediaRecorder:: stop ()

The recorder will stop the recording. Processing pending video and audio data might however still take some time. The recording is finished, once the state of the media recorder changes to QMediaRecorder::StoppedState .

int QMediaRecorder:: videoBitRate () const

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

注意: getter 函数对于特性 videoBitRate。

另请参阅 setVideoBitRate ().

[signal] void QMediaRecorder:: videoBitRateChanged ()

Signals when the recording video bit rate changes.

注意: 通知程序信号对于特性 videoBitRate .

qreal QMediaRecorder:: videoFrameRate () const

返回视频帧速率。

注意: getter 函数对于特性 videoFrameRate。

另请参阅 setVideoFrameRate ().

[signal] void QMediaRecorder:: videoFrameRateChanged ()

Signals when the recording video frame rate changes.

注意: 通知程序信号对于特性 videoFrameRate .

QSize QMediaRecorder:: videoResolution () const

Returns the resolution of the encoded video.

注意: getter 函数对于特性 videoResolution。

另请参阅 setVideoResolution ().

[signal] void QMediaRecorder:: videoResolutionChanged ()

Signals when the video recording resolution changes.

注意: 通知程序信号对于特性 videoResolution .