Audio/Video Recording Development

When to Use

To record audios and videos, use APIs described in this section to set the encoding format, sampling rate, and bit rate, and encapsulate output files based on the parameters.

Available APIs

The following table describes APIs available for audio and video recording.

Table 1 APIs available for media recording

API

Function

Description

Recorder

int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId)

Sets a video source for recording.

Recorder

int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder)

Sets a video encoder for recording.

Recorder

int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height)

Sets the width and height of the video to record.

Recorder

int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate)

Sets the frame rate of the video to record.

Recorder

int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate)

Sets the encoding bit rate of the video to record.

Recorder

int32_t SetCaptureRate(int32_t sourceId, double fps)

Sets the video capture rate.

Recorder

std::shared_ptr<OHOS::Surface> GetSurface(int32_t sourceId);

Obtains the surface of the video source.

Recorder

int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId)

Sets an audio source for recording.

Recorder

int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder)

Sets an audio encoder for recording.

Recorder

int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate)

Sets the audio sampling rate for recording.

Recorder

int32_t SetAudioChannels(int32_t sourceId, int32_t num)

Sets the number of audio channels for recording.

Recorder

int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate)

Sets the encoding bit rate of the audio to record.

Recorder

int32_t SetMaxDuration(int32_t duration)

Sets the maximum duration of an output file, in seconds.

Recorder

int32_t SetOutputFormat(OutputFormatType format)

Sets the output file format.

Recorder

int32_t SetOutputPath(const string &path);

Sets the output file path.

Recorder

int32_t SetOutputFile(int32_t fd)

Sets the file descriptor of the output file.

Recorder

int32_t SetNextOutputFile(int32_t fd);

Sets the file descriptor of the next output file.

Recorder

int32_t SetMaxFileSize(int64_t size)

Sets the maximum size of an output file, in bytes.

Recorder

int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback)

Registers a recording listener.

Recorder

int32_t Prepare()

Prepares for recording.

Recorder

int32_t Start()

Starts recording.

Recorder

int32_t Pause()

Pauses recording.

Recorder

int32_t Resume()

Resumes recording.

Recorder

int32_t Stop(bool block)

Stops recording.

Recorder

int32_t Reset();

Resets recording.

Recorder

int32_t Release()

Releases recording resources.

Recorder

int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration)

Sets the duration to split an output file.

Recorder

int32_t SetParameter(int32_t sourceId, const Format &format)

Sets an extended parameter for recording.

Limitations and Constraints

None

How to Develop

  1. Create a Recorder instance.

    Recorder *recorder = new Recorder();
    
  2. Sets parameters for the Recorder instance, including the media source information, encoding format, sampling rate, bit rate, and video width and height.

    int32_t sampleRate = 48000; 
    int32_t channelCount = 1;
    AudioCodecFormat audioFormat = AAC_LC;
    AudioSourceType inputSource = AUDIO_MIC;
    int32_t audioEncodingBitRate = sampleRate;
    VideoSourceType source = VIDEO_SOURCE_SURFACE_ES;
    int32_t frameRate = 30;
    double fps = 30;
    int32_t rate = 4096;
    int32_t sourceId = 0;
    int32_t audioSourceId = 0;
    int32_t width = 1920;
    int32_t height = 1080;
    VideoCodecFormat encoder = H264;
    recorder->SetVideoSource(source, sourceId); // Set the video source and obtain the source ID.
    recorder->SetVideoEncoder(sourceId, encoder); // Set the video encoding format.
    recorder->SetVideoSize(sourceId, width, height); // Set the video width and height.
    recorder->SetVideoFrameRate(sourceId, frameRate); // Set the video frame rate.
    recorder->SetVideoEncodingBitRate(sourceId, rate); // Set the video encoding bit rate.
    recorder->SetCaptureRate(sourceId, fps); // Set the capture rate for video frames.
    recorder->SetAudioSource(inputSource, audioSourceId); // Set the audio source and obtain the source ID.
    recorder->SetAudioEncoder(audioSourceId, audioFormat); // Set the audio encoding format.
    recorder->SetAudioSampleRate(audioSourceId, sampleRate); // Set the audio sampling rate.
    recorder->SetAudioChannels(audioSourceId, channelCount); // Set the number of audio channels.
    recorder->SetAudioEncodingBitRate(audioSourceId, audioEncodingBitRate); // Set the audio encoding bit rate.
    
  3. Prepare the Recorder instance for recording.

    recorder->Prepare(); // Prepare for recording.
    
  4. Start recording. The Recorder instance starts recording based on the audio and video sources.

    recorder->Start(); // Start recording.
    
  5. Stop recording and release resources.

    recorder->Stop(); // Stop recording.
    recorder->Release(); // Release recording resources.