Starting from API version 20, audio encoding in synchronous mode is supported.
You can call native APIs to perform audio encoding, which compresses audio PCM data into a desired format.
PCM data can be from any source. For example, you can use a microphone to record audio data or import edited PCM data. After audio encoding, you can output streams in the desired format and encapsulate the streams into a target file.
For details about the supported encoding capabilities, see AVCodec Supported Formats.
When to Use
Asynchronous mode is generally recommended for most use cases. For details, see Audio Encoding. Synchronous mode can be used if you need to actively request buffers for PCM encoding.
Audio recording
Record PCM data, encode it into the desired format, and then multiplex it in the target file format. For details about multiplexing, see Media Data Multiplexing.
Audio editing
Export edited PCM data, encode it into the corresponding audio format, and then multiplex it into a file. For details about multiplexing, see Media Data Multiplexing.
Read AudioCodec for the API reference.
Refer to the code snippet below to complete the entire audio encoding process, including creating an encoder, setting encoding parameters (such as the sample rate, bit rate, and audio channel count), and starting, refreshing, resetting, and destroying the encoder.
The sample code uses custom utility macros and data structures defined in the sample project, such as CHECK_AND_RETURN_RET_LOG, AVCODEC_SAMPLE_ERR_*, AVCODEC_SAMPLE_LOG*, CodecUserData, CodecBufferInfo, SampleInfo. These are not SDK APIs. Refer to the sample project for their complete definitions, and implement or replace them when porting the code.
During application development, you must call the APIs in the defined sequence. Otherwise, an exception or undefined behavior may occur.
The figure below shows the call relationship of audio codec in synchronous mode.
The dotted line indicates an optional operation. Audio encoding does not involve decryption. Therefore, you do not need to call OH_AudioCodec_SetDecryptionConfig.
The solid line indicates a mandatory operation.

target_link_libraries(sample PUBLIC libnative_media_codecbase.so) target_link_libraries(sample PUBLIC libnative_media_core.so) target_link_libraries(sample PUBLIC libnative_media_acodec.so)
The word sample in the preceding code snippet is only an example. Use the actual project directory name.
Add the header files and namespace.
#include <multimedia/player_framework/native_avcodec_audiocodec.h> #include <multimedia/native_audio_channel_layout.h> #include <multimedia/player_framework/native_avcapability.h> #include <multimedia/player_framework/native_avcodec_base.h> #include <multimedia/player_framework/native_avformat.h> #include <multimedia/player_framework/native_avbuffer.h> // C++ standard library namespace. using namespace std;
Create an encoder instance. In the code snippet below, **OH_AVCodec *** is the pointer to the encoder instance created.
You can create an encoder by MIME type or codec name.
int32_t AudioEncoder::Create(const std::string &codecMime)
{
// Set the encoding flag. true indicates encoding.
constexpr bool isEncoder = true;
// Create the encoder by MIME type. Use the MIME type of the actual encoding format.
encoder_ = OH_AudioCodec_CreateByMime(codecMime.c_str(), isEncoder);
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Create failed");
return AVCODEC_SAMPLE_ERR_OK;
} int32_t AudioEncoder::CreateByName(const std::string &codecMime)
{
// Create the encoder by codec name.
OH_AVCapability *capability = OH_AVCodec_GetCapability(codecMime.c_str(), true);
CHECK_AND_RETURN_RET_LOG(capability != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "GetCapability failed");
const char *name = OH_AVCapability_GetName(capability);
encoder_ = OH_AudioCodec_CreateByName(name);
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "CreateByName failed");
return AVCODEC_SAMPLE_ERR_OK;
} Call OH_AudioCodec_Configure to configure the encoder.
Key values of configuration options are described as follows:
| Key | Description | AAC | FLAC | MPEG (MP3) | G711mu | Opus | AMR-NB | AMR-WB |
|---|---|---|---|---|---|---|---|---|
| OH_MD_KEY_AUD_SAMPLE_RATE | Sample rate. | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory |
| OH_MD_KEY_AUD_CHANNEL_COUNT | Audio channel count. | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory |
| OH_MD_KEY_AUDIO_SAMPLE_FORMAT | Output audio stream format. | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory | Mandatory |
| OH_MD_KEY_BITRATE | Bit rate. | Optional | Mandatory | Mandatory | - | Mandatory | Mandatory | Mandatory |
| OH_MD_KEY_CHANNEL_LAYOUT | Audio channel layout. | Optional | Mandatory | - | - | - | - | - |
| OH_MD_KEY_MAX_INPUT_SIZE | Maximum input length. | Optional | Optional | Optional | Optional | Optional | Optional | Optional |
| OH_MD_KEY_PROFILE | Encoding profile. | Optional | - | - | - | - | - | - |
| OH_MD_KEY_COMPLIANCE_LEVEL | Compatibility level. | - | Optional | - | - | - | - | - |
The sample below lists the value range of each audio encoding type.
| AAC-LC | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 | 1, 2, 3, 4, 5, 6, 8 |
| HE-AAC, HE-AAC v2| 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 | 1, 2, 3, 4, 5, 6, 8 |
| Opus |8000, 12000, 16000, 24000, 48000 | 1-2 |
| AMR (AMR-NB) | 8000 | 1 |
| AMR (AMR-WB) | 16000 | 1 |
The following example performs AAC encoding at 32000 bit/s on PCM audio with a 44100 Hz sample rate, two stereo channels, and the SAMPLE_S16LE sample format:
Example of calling FLAC encoding:
Before configuration, use the OH_AVCapability query APIs to check whether the encoder supports the parameters. Enable synchronous mode with OH_MD_KEY_ENABLE_SYNC_MODE: set it to 1 in the OH_AVFormat passed to Configure when synchronous mode is required. The sample sets this parameter only when synchronous mode is used.
OH_AVErrCode ret;
// (Mandatory) Configure the audio sample rate.
constexpr uint32_t DEFAULT_SAMPLERATE = 44100;
// (Mandatory) Configure the audio bit rate.
constexpr uint64_t DEFAULT_BITRATE = 32000;
// (Mandatory) Configure the audio channel count.
constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2;
// (Mandatory) Configure the audio bit depth.
constexpr OH_BitsPerSample SAMPLE_FORMAT = OH_BitsPerSample::SAMPLE_S16LE;
// (Optional) Configure the AAC profile. The default value is AAC_PROFILE_LC. Other values are AAC_PROFILE_HE and AAC_PROFILE_HE_V2.
constexpr int32_t AAC_PROFILE = OH_AACProfile::AAC_PROFILE_LC;
OH_AVFormat *format = OH_AVFormat_Create();
// Set the format.
OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT);
OH_AVFormat_SetIntValue(format,OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE);
OH_AVFormat_SetLongValue(format,OH_MD_KEY_BITRATE, DEFAULT_BITRATE);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
OH_AVFormat_SetLongValue(format,OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_ENABLE_SYNC_MODE, 1); // Set the synchronous mode.
// Configure the encoder.
ret = OH_AudioCodec_Configure(audioEnc_, format);
if (ret != AV_ERR_OK) {
// Handle exceptions.
} In addition to the parameters configured above, you can also configure the following optional parameters during the Configure phase:
OH_MD_KEY_AUDIO_MAX_INPUT_BUFFER_SIZE: Configures the maximum input buffer size (in bytes) for the audio encoder. For frame-aligned encoders such as FLAC and MP3, setting this parameter enables the internal PCM caching mechanism, allowing input data to be accepted without frame-size alignment. The encoder automatically caches and splits frames. The actual buffer size is subject to encoder implementation limits. If the configured value exceeds the upper limit, it is set to the upper limit (10 MB).
OH_MD_KEY_AUDIO_ENCODER_PTS_MODE: Configures the output PTS mode of the audio encoder. The value type is OH_AudioEncoderPTSMode. The PTS calculation method for output frames varies by mode:
OH_AUDIO_ENCODER_PTS_MODE_DEFAULT: Default behavior. The PTS output method may vary across different encoders.
OH_AUDIO_ENCODER_PTS_MODE_ZERO_START: PTS starts from 0 and increments by frame duration. This mode is suitable for scenarios where input PTS is not relied upon and timing needs to start from 0.
OH_AUDIO_ENCODER_PTS_MODE_FIRST_INPUT_START: PTS starts from the PTS of the first input frame and increments by frame duration. This mode is suitable for scenarios where PTS continuity with the input stream must be maintained.
OH_MD_KEY_AUDIO_ENCODER_ENABLE_SAMPLE_FORMAT_CONVERT: Configures the sample format conversion switch for the audio encoder. 1 indicates enabled, and 0 indicates disabled (default). Audio encoders natively support a limited set of sample formats (for example, G711mu supports only SAMPLE_S16LE). When enabled, the encoder automatically converts the input PCM data to a format supported by the encoder for encoding. The supported input sample formats are extended to: SAMPLE_U8, SAMPLE_S16LE, SAMPLE_S24LE, SAMPLE_S32LE, and SAMPLE_F32LE. This is suitable for scenarios where the input PCM format differs from the encoder's native format.
// Configure the maximum input buffer size of the encoder (optional). After this is set for FLAC/MP3, non-frame-aligned input data can be accepted. The value 4096 here is only a reference, and you can set it dynamically based on actual needs.
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_MAX_INPUT_BUFFER_SIZE, 4096);
// Configure the encoder PTS output mode (optional).
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_ENCODER_PTS_MODE,
OH_AUDIO_ENCODER_PTS_MODE_FIRST_INPUT_START);
// Configure the encoder sample format conversion switch (optional). When enabled, more sample formats are supported for input.
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_ENCODER_ENABLE_SAMPLE_FORMAT_CONVERT, 1); Call OH_AudioCodec_Prepare() to prepare internal resources for the encoder.
The sample project wraps OH_AudioCodec_Prepare() in Config(). Config() first completes Configure(), then calls OH_AudioCodec_Prepare() to prepare the encoder. The following snippet shows the preparation part:
{
// Prepare the encoder.
int ret = OH_AudioCodec_Prepare(encoder_);
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR, "Prepare failed, ret: %{public}d", ret);
} Call OH_AudioCodec_Start() to start the encoder.
int32_t AudioEncoder::Start()
{
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Encoder is null");
int ret = OH_AudioCodec_Start(encoder_);
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR, "Start failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_OK;
} Invoke synchronous mode to write PCM samples to encode and obtain the encoded audio frames.
The method for determining the number of samples per frame (SAMPLES_PER_FRAME) is as follows:
AAC-LC encodes 1024 PCM samples per frame. Therefore, you are advised to input exactly 1024 samples at a time.
HE-AAC encodes 2048 PCM samples per frame. Therefore, you are advised to input exactly 2048 samples at a time.
For FLAC encoding, set SAMPLES_PER_FRAME based on the table below.
| Sample Rate | Sample Count |
|---|---|
| 8000 | 576 |
| 16000 | 1152 |
| 22050 | 2304 |
| 24000 | 2304 |
| 32000 | 2304 |
| 44100 | 4608 |
| 48000 | 4608 |
| 88200 | 8192 |
| 96000 | 8192 |
Data volume (in bytes) of a single encoding input: Number of samples (SAMPLES_PER_FRAME) * Number of channels * Number of bytes per sample.
Request an input buffer: call OH_AudioCodec_QueryInputBuffer() to query an available input buffer index (AV_ERR_TRY_AGAIN_LATER is returned on timeout), then call OH_AudioCodec_GetInputBuffer() to obtain the buffer.
OH_AVBuffer *AudioEncoder::GetInputBuffer(CodecBufferInfo &info, int64_t timeoutUs)
{
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, nullptr, "Encoder is null");
auto ret = OH_AudioCodec_QueryInputBuffer(encoder_, &info.bufferIndex, timeoutUs);
switch (ret) {
case AV_ERR_OK: {
OH_AVBuffer *buffer = OH_AudioCodec_GetInputBuffer(encoder_, info.bufferIndex);
CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "Input buffer is null");
// Fill this buffer only before PushInputData. Ownership returns to the codec after submission.
info.buffer = buffer;
return buffer;
}
case AV_ERR_TRY_AGAIN_LATER: {
AVCODEC_SAMPLE_LOGE("Get input buffer timeout.");
return nullptr;
}
default: {
return nullptr;
}
}
return nullptr;
} Fill in the complete input data before calling this API. This sample uses microphone recording (AudioCapturer) as the PCM data source. Recorded data is first written to an app-defined PCM cache. Once enough data is available for an input buffer, it is copied to an encoder input buffer and submitted to the encoder. The following example shows the synchronous input thread:
void Recorder::AudioEncInputSyncThread()
{
while (true) {
CHECK_AND_BREAK_LOG(isStarted_, "Audio encoder sync thread out");
// Stop submitting new input after receiving EOS. The output thread continues draining encoded samples.
if (isEos_.load()) {
AVCODEC_SAMPLE_LOGI("Audio input thread received EOS signal, stopping input");
break;
}
// Wait until the cache contains a full frame. Incomplete audio data causes incorrect durations and timestamps.
if (!WaitForAudioInputFrame(100ms)) {
if (!isStarted_ || isEos_.load()) {
break;
}
continue;
}
// Request a codec input buffer only when enough data is available, reducing the risk of failing to return an acquired buffer promptly due to insufficient cached data.
CodecBufferInfo bufferInfo;
bufferInfo.bufferIndex = std::numeric_limits<uint32_t>::max();
auto buffer = audioEncoder_->GetInputBuffer(bufferInfo, TIMEOUT_US);
if (buffer == nullptr) {
if (bufferInfo.bufferIndex != std::numeric_limits<uint32_t>::max()) {
// If Query succeeds but Get fails, the app may still hold the index. End the session so that the codec reclaims it.
AbortRecording("Synchronous audio input buffer retrieval failed");
break;
}
AVCODEC_SAMPLE_LOGW("Get input buffer timeout, retry");
continue;
}
if (!SubmitAudioInputFrame(bufferInfo, buffer, true)) {
break;
}
}
} In the preceding example, attr.flags indicates the type of the buffer flag.
At the end, set flags to AVCODEC_BUFFER_FLAGS_EOS. In this sample, when recording ends, an input buffer is obtained, filled with zero-length data, marked with EOS, and submitted to the encoder.
int32_t AudioEncoder::NotifyEndOfStream()
{
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Encoder is null");
// Use an empty input buffer to carry EOS. Submit it even if there is no media data so that the encoder outputs the trailing data.
CodecBufferInfo bufferInfo;
auto buffer = GetInputBuffer(bufferInfo, TIMEOUT_US);
if (buffer == nullptr) {
AVCODEC_SAMPLE_LOGW("GetInputBuffer for EOS failed");
return AVCODEC_SAMPLE_ERR_ERROR;
}
// EOS contains no valid audio bytes. The buffer returns to codec ownership immediately after PushInputBuffer is called.
bufferInfo.attr.size = 0;
bufferInfo.attr.flags = AVCODEC_BUFFER_FLAGS_EOS;
int32_t ret = OH_AVBuffer_SetBufferAttr(buffer, &bufferInfo.attr);
if (ret != AV_ERR_OK) {
AVCODEC_SAMPLE_LOGE("SetBufferAttr for EOS failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_ERROR;
}
bufferInfo.buffer = buffer;
ret = OH_AudioCodec_PushInputBuffer(encoder_, bufferInfo.bufferIndex);
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR,
"PushInputBuffer for EOS failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_OK;
} | Value | Description |
|---|---|
| AVCODEC_BUFFER_FLAGS_NONE | Indicates a common frame. |
| AVCODEC_BUFFER_FLAGS_EOS | Indicates an end-of-stream frame. |
| AVCODEC_BUFFER_FLAGS_CODEC_DATA | Indicates that the buffer contains codec-specific data. |
Obtain encoded output: call OH_AudioCodec_QueryOutputBuffer() to query an available output buffer. If the input produces multiple encoded frames, obtain output buffers repeatedly to retrieve all encoded data:
int32_t AudioEncoder::GetOutputBuffer(CodecBufferInfo &info, int64_t timeoutUs)
{
info.buffer = nullptr;
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Encoder is null");
// One input batch may produce multiple output frames. Keep obtaining and returning them until no output is available or the codec returns EOS.
OH_AVErrCode ret = OH_AudioCodec_QueryOutputBuffer(encoder_, &info.bufferIndex, timeoutUs);
if (ret == AV_ERR_TRY_AGAIN_LATER) {
// Timeout: There is not enough input data to encode a frame, or the timeout period is too short.
AVCODEC_SAMPLE_LOGW("Get output buffer timeout.");
return AVCODEC_SAMPLE_ERR_AGAIN; // continue;
}
if (ret != AV_ERR_OK) {
AVCODEC_SAMPLE_LOGE("query output buffer failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_ERROR; // break;
}
OH_AVBuffer *outputBuf = OH_AudioCodec_GetOutputBuffer(encoder_, info.bufferIndex);
if (outputBuf == nullptr) {
// A successful query transfers the index from the codec to the app. Return it on error paths as well.
const int32_t freeRet = OH_AudioCodec_FreeOutputBuffer(encoder_, info.bufferIndex);
AVCODEC_SAMPLE_LOGE("Get output buffer failed, free ret: %{public}d", freeRet);
return AVCODEC_SAMPLE_ERR_ERROR; // break;
}
ret = OH_AVBuffer_GetBufferAttr(outputBuf, &info.attr);
if (ret != AV_ERR_OK) {
const int32_t freeRet = OH_AudioCodec_FreeOutputBuffer(encoder_, info.bufferIndex);
AVCODEC_SAMPLE_LOGE("Get output buffer attr failed, ret: %{public}d, free ret: %{public}d", ret, freeRet);
return AVCODEC_SAMPLE_ERR_ERROR; // break;
}
info.buffer = outputBuf;
if (info.attr.flags & AVCODEC_BUFFER_FLAGS_EOS) {
AVCODEC_SAMPLE_LOGI("Out buffer EOS flag detected");
return AVCODEC_SAMPLE_ERR_OK;
}
return AVCODEC_SAMPLE_ERR_OK;
} After retrieving the encoded stream, promptly call OH_AudioCodec_FreeOutputBuffer() to release the output buffer. In this sample, the encoded stream is written to the muxer. To multiplex it into an audio file, see Media Data Multiplexing. The following example shows the synchronous output thread:
void Recorder::AudioEncOutputSyncThread()
{
while (true) {
CHECK_AND_BREAK_LOG(isStarted_, "Audio encoder output thread out");
CodecBufferInfo bufferInfo;
int32_t errCode = audioEncoder_->GetOutputBuffer(bufferInfo, TIMEOUT_US);
if (errCode == AVCODEC_SAMPLE_ERR_OK) {
AVCODEC_SAMPLE_LOGI("AVCODEC_SAMPLE_ERR_OK");
} else if (errCode == AVCODEC_SAMPLE_ERR_END || errCode == AVCODEC_SAMPLE_ERR_ERROR) {
AVCODEC_SAMPLE_LOGW("Audio output thread received END or ERROR");
break;
} else {
AVCODEC_SAMPLE_LOGW("Get output buffer timeout, retry");
continue;
}
audioEncContext_->outputFrameCount++;
AVCODEC_SAMPLE_LOGW(
"Audio Out buffer sync count: %{public}u, size: %{public}d, flag: %{public}u, pts: %{public}" PRId64,
audioEncContext_->outputFrameCount, bufferInfo.attr.size, bufferInfo.attr.flags, bufferInfo.attr.pts);
const bool isEosBuffer = (bufferInfo.attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0;
int32_t writeRet = AVCODEC_SAMPLE_ERR_OK;
if (!isEosBuffer && bufferInfo.attr.size > 0) {
// Muxer writes do not take ownership of the buffer. Explicitly return the original index after writing to both targets.
writeRet = muxer_->WriteSample(muxer_->GetAudioTrackId(), bufferInfo.buffer, bufferInfo.attr);
if (writeRet == AVCODEC_SAMPLE_ERR_OK && secondaryMuxer_ != nullptr) {
writeRet = secondaryMuxer_->WriteSample(secondaryMuxer_->GetAudioTrackId(), bufferInfo.buffer,
bufferInfo.attr);
}
}
int32_t ret = audioEncoder_->FreeOutputData(bufferInfo.bufferIndex);
CHECK_AND_BREAK_LOG(ret == AVCODEC_SAMPLE_ERR_OK, "Encoder output thread out");
if (writeRet != AVCODEC_SAMPLE_ERR_OK) {
AbortRecording("Write encoded audio sample failed");
break;
}
if (isEosBuffer) {
isAudioEos_.store(true);
videoEosCond_.notify_all();
AVCODEC_SAMPLE_LOGI("Audio EOS processed, exiting output thread");
break;
}
}
// In synchronous mode, the video thread still triggers release. If audio ends first, video is still using the muxer.
AVCODEC_SAMPLE_LOGI("Audio output thread exited, waiting for video thread to trigger release");
} (Optional) Call OH_AudioCodec_Reset() to reset the encoder.
After OH_AudioCodec_Reset() is called, the encoder returns to the initialized state. The input and output buffers obtained before the reset cannot be used. You must call OH_AudioCodec_Configure() to reconfigure the encoder and then call OH_AudioCodec_Start() to start encoding again. Obtain the input and output buffers again after the encoder is started.
int32_t AudioEncoder::Reset()
{
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Encoder is null");
// Reset the encoder. Call Configure() again to configure it, and Start() to start it.
int32_t ret = OH_AudioCodec_Reset(encoder_);
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR, "Reset failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_OK;
} (Optional) Call OH_AudioCodec_Stop() to stop the encoder.
After the encoder is stopped, you can call OH_AudioCodec_Start() to start it again. The input and output buffers obtained before the decoder is stopped cannot be reused. You must obtain them again after the decoder is started.
int32_t AudioEncoder::Stop()
{
CHECK_AND_RETURN_RET_LOG(encoder_ != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "Encoder is null");
int ret = Flush();
CHECK_AND_RETURN_RET_LOG(ret == AVCODEC_SAMPLE_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR,
"Flush failed, ret: %{public}d", ret);
ret = OH_AudioCodec_Stop(encoder_);
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR, "Stop failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_OK;
} Call OH_AudioCodec_Destroy() to destroy the encoder instance and release resources.
Do not destroy the encoder repeatedly.
int32_t AudioEncoder::Release()
{
if (encoder_ != nullptr) {
// Flush and stop the encoder before destruction. Do not destroy it more than once.
Flush();
Stop();
OH_AudioCodec_Destroy(encoder_);
encoder_ = nullptr;
}
return AVCODEC_SAMPLE_ERR_OK;
}