1# Audio Decoding 2 3<!--Kit: AVCodec Kit--> 4<!--Subsystem: Multimedia--> 5<!--Owner: @mr-chencxy--> 6<!--Designer: @dpy2650---> 7<!--Tester: @baotianhao--> 8<!--Adviser: @zengyawen--> 9 10You can call native APIs to perform audio decoding, which decodes media data into PCM streams. 11 12For details about the supported decoding capabilities, see [AVCodec Supported Formats](avcodec-support-formats.md#audio-decoding). 13 14**When to Use** 15 16- Audio playback 17 18 Decode audio and transmit the data to the speaker for playing. 19- Audio rendering 20 21 Decode audio and transmit the data to the audio processing module for audio rendering. 22- Audio editing 23 24 Decode audio and transmit the data for audio editing (for example, adjusting the playback speed of a channel). Audio editing is performed based on PCM streams. 25> **NOTE** 26> 27> Streams generated in the MP3 audio encoding process cannot be directly decoded through the MP3 audio decoding process. The following process is recommended: PCM stream -> MP3 audio encoding -> multiplexing -> demultiplexing -> MP3 audio decoding. 28 29## Development Guidelines 30 31Read [AudioCodec](../../reference/apis-avcodec-kit/_audio_codec.md) for the API reference. 32 33Refer to the code snippet below to complete the entire audio decoding process, including creating a decoder, setting decoding parameters (such as the sample rate, bit rate, and audio channel count), and starting, refreshing, resetting, and destroying the decoder. 34 35During application development, you must call the APIs in the defined sequence. Otherwise, an exception or undefined behavior may occur. 36 37The figure below shows the call relationship of audio decoding. 38 39- The dotted line indicates an optional operation. 40 41- The solid line indicates a mandatory operation. 42 43 44 45### Linking the Dynamic Libraries in the CMake Script 46 47```cmake 48target_link_libraries(sample PUBLIC libnative_media_codecbase.so) 49target_link_libraries(sample PUBLIC libnative_media_core.so) 50target_link_libraries(sample PUBLIC libnative_media_acodec.so) 51``` 52> **NOTE** 53> 54> The word **sample** in the preceding code snippet is only an example. Use the actual project directory name. 55 56### How to Develop 57 581. Add the header files. 59 60 ```cpp 61 #include <multimedia/player_framework/native_avcodec_audiocodec.h> 62 #include <multimedia/native_audio_channel_layout.h> 63 #include <multimedia/player_framework/native_avcapability.h> 64 #include <multimedia/player_framework/native_avcodec_base.h> 65 #include <multimedia/player_framework/native_avformat.h> 66 #include <multimedia/player_framework/native_avbuffer.h> 67 ``` 68 692. Create a decoder instance. In the code snippet below, **OH_AVCodec *** is the pointer to the decoder instance created. 70 71 You can create a decoder by MIME type or codec name. 72 73 Method 1: Create a decoder by MIME type. 74 ```cpp 75 // Specify whether encoding is used. The value false means decoding. 76 bool isEncoder = false; 77 // Create a decoder by MIME type. 78 OH_AVCodec *audioDec_ = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, isEncoder); 79 ``` 80 Method 2: Create a decoder by codec name. 81 ```cpp 82 // Create a decoder by name. 83 OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false); 84 const char *name = OH_AVCapability_GetName(capability); 85 OH_AVCodec *audioDec_ = OH_AudioCodec_CreateByName(name); 86 ``` 87 Add the header files and namespace. 88 ```cpp 89 #include <mutex> 90 #include <queue> 91 // Namespace of the C++ standard library. 92 using namespace std; 93 ``` 94 The sample code is as follows: 95 ```cpp 96 // Initialize the queues. 97 class ADecBufferSignal { 98 public: 99 std::mutex inMutex_; 100 std::mutex outMutex_; 101 std::mutex startMutex_; 102 std::condition_variable inCond_; 103 std::condition_variable outCond_; 104 std::condition_variable startCond_; 105 std::queue<uint32_t> inQueue_; 106 std::queue<uint32_t> outQueue_; 107 std::queue<OH_AVBuffer *> inBufferQueue_; 108 std::queue<OH_AVBuffer *> outBufferQueue_; 109 }; 110 ADecBufferSignal *signal_; 111 ``` 112 1133. Call **OH_AudioCodec_RegisterCallback()** to register callback functions. 114 115 Register the **OH_AVCodecCallback** struct that defines the following callback function pointers: 116 117 - **OH_AVCodecOnError**, a callback used to report a codec operation error 118 - **OH_AVCodecOnStreamChanged**, a callback used to report stream information changes, including changes in the sample rate, audio channel count, and audio sample format. The decoding formats that can detect these changes include <!--RP5--><!--RP5End-->AAC, FLAC, MP3, and VORBIS. (This callback is supported since API version 15.) 119 - **OH_AVCodecOnNeedInputBuffer**, a callback used to report input data required, which means that the decoder is ready for receiving data 120 - **OH_AVCodecOnNewOutputBuffer**, a callback used to report output data generated, which means that decoding is complete 121 122 You need to process the callback functions to ensure that the decoder runs properly. 123 124 > **NOTE** 125 > 126 > You are not advised to perform time-consuming operations in the callback. 127 128 ```cpp 129 // Implement the OH_AVCodecOnError callback function. 130 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData) 131 { 132 (void)codec; 133 (void)errorCode; 134 (void)userData; 135 } 136 // Implement the OH_AVCodecOnStreamChanged callback function. 137 static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData) 138 { 139 (void)codec; 140 (void)userData; 141 // Callback processing after the decoding output parameters are changed. The application performs processing as required. 142 int32_t sampleRate; 143 int32_t channelCount; 144 int32_t sampleFormat; 145 if (OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, &sampleRate)) { 146 // Check whether the sample rate changes and perform processing as required. 147 } 148 if (OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, &channelCount)) { 149 // Check whether the number of audio channels changes and perform processing as required. 150 } 151 if (OH_AVFormat_GetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, &sampleFormat)) { 152 // Check whether the audio sample format changes and perform processing as required. 153 } 154 } 155 // Implement the OH_AVCodecOnNeedInputBuffer callback function. 156 static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData) 157 { 158 (void)codec; 159 ADecBufferSignal *signal = static_cast<ADecBufferSignal *>(userData); 160 unique_lock<mutex> lock(signal->inMutex_); 161 signal->inQueue_.push(index); 162 signal->inBufferQueue_.push(data); 163 signal->inCond_.notify_all(); 164 // The input stream is sent to inBufferQueue_. 165 } 166 // Implement the OH_AVCodecOnNewOutputBuffer callback function. 167 static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData) 168 { 169 (void)codec; 170 ADecBufferSignal *signal = static_cast<ADecBufferSignal *>(userData); 171 unique_lock<mutex> lock(signal->outMutex_); 172 signal->outQueue_.push(index); 173 signal->outBufferQueue_.push(data); 174 signal->outCond_.notify_all(); 175 // The index of the output buffer is sent to outQueue_. 176 // The decoded data is sent to outBufferQueue_. 177 } 178 ``` 179 Configure the callback information. 180 ```cpp 181 signal_ = new ADecBufferSignal(); 182 OH_AVCodecCallback cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable}; 183 // Set the asynchronous callbacks. 184 int32_t ret = OH_AudioCodec_RegisterCallback(audioDec_, cb_, signal_); 185 if (ret != AV_ERR_OK) { 186 // Handle exceptions. 187 } 188 ``` 189 1904. (Optional) Call **OH_AudioCodec_SetDecryptionConfig** to set the decryption configuration. 191 192 Call this API after the media key system information is obtained but before **Prepare()** is called. For details about how to obtain such information, see step 4 in [Media Data Demultiplexing](audio-video-demuxer.md). 193 194 For details about DRM APIs, see [DRM](../../reference/apis-drm-kit/capi-drm.md). 195 196 Add the header files. 197 198 ```c++ 199 #include <multimedia/drm_framework/native_mediakeysystem.h> 200 #include <multimedia/drm_framework/native_mediakeysession.h> 201 #include <multimedia/drm_framework/native_drm_err.h> 202 #include <multimedia/drm_framework/native_drm_common.h> 203 ``` 204 Link the dynamic libraries in the CMake script. 205 206 ``` cmake 207 target_link_libraries(sample PUBLIC libnative_drm.so) 208 ``` 209 210 The sample code is as follows: 211 ```c++ 212 // Create a media key system based on the media key system information. The following uses com.clearplay.drm as an example. 213 MediaKeySystem *system = nullptr; 214 int32_t ret = OH_MediaKeySystem_Create("com.clearplay.drm", &system); 215 if (system == nullptr) { 216 printf("create media key system failed"); 217 return; 218 } 219 220 // Create a media key session. 221 MediaKeySession *session = nullptr; 222 DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO; 223 ret = OH_MediaKeySystem_CreateMediaKeySession(system, &contentProtectionLevel, &session); 224 if (ret != DRM_OK) { 225 // If the creation fails, refer to the DRM interface document and check logs. 226 printf("create media key session failed."); 227 return; 228 } 229 if (session == nullptr) { 230 printf("media key session is nullptr."); 231 return; 232 } 233 // Generate a media key request and set the response to the media key request. 234 // Set the decryption configuration, that is, set the decryption session and secure channel flag to the decoder. (Currently, the secure channel is not supported for audio decryption and therefore the secure channel flag should be set to false.) 235 bool secureAudio = false; 236 ret = OH_AudioCodec_SetDecryptionConfig(audioDec_, session, secureAudio); 237 ``` 238 2395. Call **OH_AudioCodec_Configure()** to configure the decoder. 240 241 Key values of configuration options are described as follows: 242 243 | Key | Description | AAC | FLAC| Vorbis | MPEG (MP3) | G711mu | AMR (AMR-NB and AMR-WB) | APE | G711a | 244 | ---------------------------- | :--------------: | :--------------------------------: | :--: | :--------------------------------: | :--: | :-----------------: | :-------------------------------: | :--: | :----------------------: | 245 | OH_MD_KEY_AUD_SAMPLE_RATE | Sample rate. | Mandatory | Mandatory| Mandatory | Mandatory| Mandatory | Mandatory | Mandatory| Mandatory | 246 | OH_MD_KEY_AUD_CHANNEL_COUNT | Audio channel count. | Mandatory | Mandatory| Mandatory | Mandatory| Mandatory | Mandatory | Mandatory| Mandatory | 247 | OH_MD_KEY_MAX_INPUT_SIZE | Maximum input size. | Optional | Optional| Optional | Optional| Optional | Optional | Optional| Optional | 248 | OH_MD_KEY_AAC_IS_ADTS | ADTS or not. | Optional (defaults to **1**) | - | - | - | - | - | - | - | 249 | OH_MD_KEY_AUDIO_SAMPLE_FORMAT | Output audio stream format. | Optional (SAMPLE_S16LE, SAMPLE_F32LE)| Optional| Optional (SAMPLE_S16LE, SAMPLE_F32LE)| Optional| Optional (default: SAMPLE_S16LE)| Optional (SAMPLE_S16LE, SAMPLE_F32LE)| Optional| Optional (default: SAMPLE_S16LE)| 250 | OH_MD_KEY_BITRATE | Bit rate. | Optional | Optional| Optional | Optional| Optional | Optional | Optional| Optional | 251 | OH_MD_KEY_IDENTIFICATION_HEADER | ID header. | - | - | Mandatory (Either this parameter or **MD_KEY_CODEC_CONFIG** must be set.) | - | - | - | - | - | 252 | OH_MD_KEY_SETUP_HEADER | Setup header. | - | - | Mandatory (Either this parameter or **MD_KEY_CODEC_CONFIG** must be set.) | - | - | - | - | - | 253 | OH_MD_KEY_CODEC_CONFIG | Codec-specific data.| Optional | - | Mandatory (Either this parameter or the combination of **MD_KEY_IDENTIFICATION_HEADER** and **MD_KEY_SETUP_HEADER** must be selected.) | - | - | - | Optional| - | 254 255 The sample below lists the value range of each audio decoding type. 256 257 | Audio Decoding Type| Sample Rate (Hz) | Audio Channel Count| 258 | ----------- | ---------------------------------------------------------------------------------------------- | :----: | 259 | AAC | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 | 1–8 | 260 | FLAC | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 192000 | 1–8 | 261 | Vorbis | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 176400, 192000| 1–8 | 262 | MPEG (MP3) | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 | 1–2 | 263 | G711mu | 8000 | 1 | 264 | AMR (amrnb) | 8000 | 1 | 265 | AMR (amrwb) | 16000 | 1 | 266 | APE | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000, 176400, 192000| 1–2 | 267 | G711a | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 | 1–6 | 268 <!--RP4--> 269 <!--RP4End--> 270 271 Starting from API version 20, you can query the [sample rate range](../../reference/apis-avcodec-kit/_a_v_capability.md#oh_avcapability_getaudiosupportedsamplerateranges). The following audio decoding types support decoding of any sample rate within their range (available after API version 20): 272 273 | Audio Decoding Type| Sample Rate (Hz) | 274 | ----------- | --------------- | 275 | FLAC | 8000 – 384000 | 276 | Vorbis | 8000 – 192000 | 277 | APE | 1 – 2147483647 | 278 279 ```cpp 280 // (Mandatory) Configure the audio sample rate. 281 constexpr uint32_t DEFAULT_SAMPLERATE = 44100; 282 // (Optional) Configure the audio bit rate. 283 constexpr uint32_t DEFAULT_BITRATE = 32000; 284 // (Mandatory) Configure the audio channel count. 285 constexpr uint32_t DEFAULT_CHANNEL_COUNT = 2; 286 // (Optional) Configure the maximum input length. 287 constexpr uint32_t DEFAULT_MAX_INPUT_SIZE = 1152; 288 // Configure whether to use ADTS decoding (optional for AAC decoding). 289 constexpr uint32_t DEFAULT_AAC_TYPE = 1; 290 OH_AVFormat *format = OH_AVFormat_Create(); 291 // Set the format. 292 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, DEFAULT_SAMPLERATE); 293 OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, DEFAULT_BITRATE); 294 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, DEFAULT_CHANNEL_COUNT); 295 OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE); 296 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, DEFAULT_AAC_TYPE); 297 // Configure the decoder. 298 int32_t ret = OH_AudioCodec_Configure(audioDec_, format); 299 if (ret != AV_ERR_OK) { 300 // Handle exceptions. 301 } 302 ``` 303 3046. Call **OH_AudioCodec_Prepare()** to prepare internal resources for the decoder. 305 306 ```cpp 307 int32_t ret = OH_AudioCodec_Prepare(audioDec_); 308 if (ret != AV_ERR_OK) { 309 // Handle exceptions. 310 } 311 ``` 312 3137. Call **OH_AudioCodec_Start()** to start the decoder. 314 315 Add the header file. 316 ```c++ 317 #include <fstream> 318 ``` 319 320 The sample code is as follows: 321 ```c++ 322 ifstream inputFile_; 323 ofstream outFile_; 324 325 // Set the input file path based on the actual situation. 326 const char* inputFilePath = "/"; 327 // Set the output file path based on the actual situation. 328 const char* outputFilePath = "/"; 329 // Open the path of the binary file to be decoded. 330 inputFile_.open(inputFilePath, ios::in | ios::binary); 331 // Set the path of the output file. 332 outFile_.open(outputFilePath, ios::out | ios::binary); 333 // Start decoding. 334 int32_t ret = OH_AudioCodec_Start(audioDec_); 335 if (ret != AV_ERR_OK) { 336 // Handle exceptions. 337 } 338 ``` 339 3408. (Optional) Call **OH_AVCencInfo_SetAVBuffer()** to set the Common Encryption Scheme (CENC) information. 341 342 If the content being played is DRM encrypted and [demultiplexing](audio-video-demuxer.md) is performed by the upper-layer application, call **OH_AVCencInfo_SetAVBuffer()** to set the CENC information to the AVBuffer so that the media data can be decrypted in the AVBuffer. 343 344 Add the header file. 345 346 ```c++ 347 #include <multimedia/player_framework/native_cencinfo.h> 348 ``` 349 Link the dynamic libraries in the CMake script. 350 351 ``` cmake 352 target_link_libraries(sample PUBLIC libnative_media_avcencinfo.so) 353 ``` 354 355 The sample code is as follows: 356 ```c++ 357 auto buffer = signal_->inBufferQueue_.front(); 358 uint32_t keyIdLen = DRM_KEY_ID_SIZE; 359 uint8_t keyId[] = { 360 0xd4, 0xb2, 0x01, 0xe4, 0x61, 0xc8, 0x98, 0x96, 361 0xcf, 0x05, 0x22, 0x39, 0x8d, 0x09, 0xe6, 0x28}; 362 uint32_t ivLen = DRM_KEY_IV_SIZE; 363 uint8_t iv[] = { 364 0xbf, 0x77, 0xed, 0x51, 0x81, 0xde, 0x36, 0x3e, 365 0x52, 0xf7, 0x20, 0x4f, 0x72, 0x14, 0xa3, 0x95}; 366 uint32_t encryptedBlockCount = 0; 367 uint32_t skippedBlockCount = 0; 368 uint32_t firstEncryptedOffset = 0; 369 uint32_t subsampleCount = 1; 370 DrmSubsample subsamples[1] = { {0x10, 0x16} }; 371 // Create a CencInfo instance. 372 OH_AVCencInfo *cencInfo = OH_AVCencInfo_Create(); 373 if (cencInfo == nullptr) { 374 // Handle exceptions. 375 } 376 // Set the decryption algorithm. 377 OH_AVErrCode errNo = OH_AVCencInfo_SetAlgorithm(cencInfo, DRM_ALG_CENC_AES_CTR); 378 if (errNo != AV_ERR_OK) { 379 // Handle exceptions. 380 } 381 // Set KeyId and Iv. 382 errNo = OH_AVCencInfo_SetKeyIdAndIv(cencInfo, keyId, keyIdLen, iv, ivLen); 383 if (errNo != AV_ERR_OK) { 384 // Handle exceptions. 385 } 386 // Set the sample information. 387 errNo = OH_AVCencInfo_SetSubsampleInfo(cencInfo, encryptedBlockCount, skippedBlockCount, firstEncryptedOffset, 388 subsampleCount, subsamples); 389 if (errNo != AV_ERR_OK) { 390 // Handle exceptions. 391 } 392 // Set the mode. KeyId, Iv, and SubSamples have been set. 393 errNo = OH_AVCencInfo_SetMode(cencInfo, DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET); 394 if (errNo != AV_ERR_OK) { 395 // Handle exceptions. 396 } 397 // Set CencInfo to the AVBuffer. 398 errNo = OH_AVCencInfo_SetAVBuffer(cencInfo, buffer); 399 if (errNo != AV_ERR_OK) { 400 // Handle exceptions. 401 } 402 // Destroy the CencInfo instance. 403 errNo = OH_AVCencInfo_Destroy(cencInfo); 404 if (errNo != AV_ERR_OK) { 405 // Handle exceptions. 406 } 407 ``` 408 4099. Call **OH_AudioCodec_PushInputBuffer()** to write the data to decode. 410 411 You should fill in complete input data before calling this API. 412 413 To indicate the End of Stream (EOS), pass in the **AVCODEC_BUFFER_FLAGS_EOS** flag. 414 415 ```c++ 416 uint32_t index = signal_->inQueue_.front(); 417 auto buffer = signal_->inBufferQueue_.front(); 418 int32_t size; 419 int64_t pts; 420 // size is the length of each frame of the data to decode. pts is the timestamp of each frame and is used to indicate when the audio should be played. 421 // The values of size and pts are obtained from an audio and video resource file or data stream to decode. 422 // In the case of an audio and video resource file, the values are obtained from the buffer in the demultiplexed OH_AVDemuxer_ReadSampleBuffer. 423 // In the case of a data stream, the values are obtained from the data stream provider. 424 // In this example, the values of size and pts are obtained from the test file. 425 inputFile_.read(reinterpret_cast<char *>(&size), sizeof(size)); 426 inputFile_.read(reinterpret_cast<char *>(&pts), sizeof(pts)); 427 inputFile_.read((char *)OH_AVBuffer_GetAddr(buffer), size); 428 OH_AVCodecBufferAttr attr = {0}; 429 if (inputFile_.eof()) { 430 attr.size = 0; 431 attr.flags = AVCODEC_BUFFER_FLAGS_EOS; 432 } else { 433 attr.size = size; 434 attr.flags = AVCODEC_BUFFER_FLAGS_NONE; 435 } 436 attr.pts = pts; 437 OH_AVBuffer_SetBufferAttr(buffer, &attr); 438 int32_t ret = OH_AudioCodec_PushInputBuffer(audioDec_, index); 439 if (ret != AV_ERR_OK) { 440 // Handle exceptions. 441 } 442 ``` 443 44410. Call **OH_AudioCodec_FreeOutputBuffer()** to release the decoded data. 445 446 Once you have retrieved the decoded PCM stream, call **OH_AudioCodec_FreeOutputBuffer()** to free up the data. 447 448 ```c++ 449 uint32_t index = signal_->outQueue_.front(); 450 OH_AVBuffer *data = signal_->outBufferQueue_.front(); 451 if (data == nullptr) { 452 // Handle exceptions. 453 } 454 // Obtain the buffer attributes. 455 OH_AVCodecBufferAttr attr = {0}; 456 int32_t ret = OH_AVBuffer_GetBufferAttr(data, &attr); 457 if (ret != AV_ERR_OK) { 458 // Handle exceptions. 459 } 460 // Write the decoded data (specified by data) to the output file. 461 outFile_.write(reinterpret_cast<char *>(OH_AVBuffer_GetAddr(data)), attr.size); 462 ret = OH_AudioCodec_FreeOutputBuffer(audioDec_, index); 463 if (ret != AV_ERR_OK) { 464 // Handle exceptions. 465 } 466 if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS) { 467 // End. 468 } 469 ``` 470 <!--RP3--> 471 <!--RP3End--> 472 47311. (Optional) Call **OH_AudioCodec_Flush()** to refresh the decoder. 474 475 After **OH_AudioCodec_Flush()** is called, the decoder remains in the running state, but the current queue is cleared and the buffer storing the decoded data is freed. 476 477 To continue decoding, you must call **OH_AudioCodec_Start()** again. 478 479 You need to call **OH_AudioCodec_Start()** in the following cases: 480 481 * The EOS of the file is reached. 482 * An error with **OH_AudioCodec_IsValid** set to **true** (indicating that the execution can continue) occurs. 483 484 ```c++ 485 // Refresh the decoder. 486 int32_t ret = OH_AudioCodec_Flush(audioDec_); 487 if (ret != AV_ERR_OK) { 488 // Handle exceptions. 489 } 490 // Start decoding again. 491 ret = OH_AudioCodec_Start(audioDec_); 492 if (ret != AV_ERR_OK) { 493 // Handle exceptions. 494 } 495 ``` 496 49712. (Optional) Call **OH_AudioCodec_Reset()** to reset the decoder. 498 499 After **OH_AudioCodec_Reset()** is called, the decoder returns to the initialized state. To continue decoding, you must call **OH_AudioCodec_Configure()** and then **OH_AudioCodec_Start()**. 500 501 ```c++ 502 // Reset the decoder. 503 int32_t ret = OH_AudioCodec_Reset(audioDec_); 504 if (ret != AV_ERR_OK) { 505 // Handle exceptions. 506 } 507 // Reconfigure the decoder. 508 ret = OH_AudioCodec_Configure(audioDec_, format); 509 if (ret != AV_ERR_OK) { 510 // Handle exceptions. 511 } 512 ``` 513 51413. Call **OH_AudioCodec_Stop()** to stop the decoder. 515 516 After the decoder is stopped, you can call **OH_AudioCodec_Start()** to start it again. If you have passed specific data in the previous **OH_AudioCodec_Start()** for the codec, you must pass it again. 517 518 ```c++ 519 // Stop the decoder. 520 int32_t ret = OH_AudioCodec_Stop(audioDec_); 521 if (ret != AV_ERR_OK) { 522 // Handle exceptions. 523 } 524 ``` 525 52614. Call **OH_AudioCodec_Destroy()** to destroy the decoder instance and release resources. 527 528 > **NOTE** 529 > 530 > You only need to call this API once. 531 532 ```c++ 533 // Call OH_AudioCodec_Destroy to destroy the decoder. 534 int32_t ret = OH_AudioCodec_Destroy(audioDec_); 535 if (ret != AV_ERR_OK) { 536 // Handle exceptions. 537 } else { 538 audioDec_ = NULL; // The decoder cannot be destroyed repeatedly. 539 } 540 ``` 541 542