harmony 鸿蒙Using OHAudio for Audio Recording

  • 2023-10-30
  • 浏览 (464)

Using OHAudio for Audio Recording

OHAudio is a set of native APIs introduced in API version 10. These APIs are normalized in design and support both common and low-latency audio channels.

Prerequisites

To use the playback or recording capability of OHAudio, you must first import the corresponding header files.

To use APIs for audio recording, import <native_audiostreambuilder.h> and <native_audiocapturer.h>.

Audio Stream Builder

OHAudio provides the OH_AudioStreamBuilder class, which complies with the builder design pattern and is used to build audio streams. You need to specify OH_AudioStream_Type based on your service scenarios.

OH_AudioStream_Type can be set to either of the following:

  • AUDIOSTREAM_TYPE_RENDERER
  • AUDIOSTREAM_TYPE_CAPTURER

The following code snippet shows how to use OH_AudioStreamBuilder_Create to create a builder:

OH_AudioStreamBuilder* builder;
OH_AudioStreamBuilder_Create(&builder, streamType);

After the audio service is complete, call OH_AudioStreamBuilder_Destroy to destroy the builder.

OH_AudioStreamBuilder_Destroy(builder);

How to Develop

Read OHAudio for the API reference.

The following walks you through how to implement simple recording:

  1. Create an audio stream builder.

    OH_AudioStreamBuilder* builder;
    OH_AudioStreamBuilder_Create(&builder, AUDIOSTREAM_TYPE_CAPTURER);
    
  2. Set audio stream parameters.

    After creating the builder for audio recording, set the parameters required.

    OH_AudioStreamBuilder_SetSamplingRate(builder, rate);
    OH_AudioStreamBuilder_SetChannelCount(builder, channelCount);
    OH_AudioStreamBuilder_SetSampleFormat(builder, format);
    OH_AudioStreamBuilder_SetEncodingType(builder, encodingType);
    OH_AudioStreamBuilder_SetCapturerInfo(builder, sourceType);
    

    Note that the audio data to record is written through callbacks. You must call OH_AudioStreamBuilder_SetCapturerCallback to implement the callbacks. For details about the declaration of the callback functions, see OH_AudioCapturer_Callbacks.

  3. Set the callback functions.

    OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
    
  4. Create an audio capturer instance.

    OH_AudioCapturer* audioCapturer;
    OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
    
  5. Use the audio capturer.

    You can use the APIs listed below to control the audio streams.

|API |Description | |————————————————————|————| |OH_AudioStream_Result OH_AudioCapturer_Start(OH_AudioCapturer* capturer)|Starts the audio capturer. | |OH_AudioStream_Result OH_AudioCapturer_Pause(OH_AudioCapturer* capturer)|Pauses the audio capturer. | |OH_AudioStream_Result OH_AudioCapturer_Stop(OH_AudioCapturer* capturer)|Stops the audio capturer. | |OH_AudioStream_Result OH_AudioCapturer_Flush(OH_AudioCapturer* capturer)|Flushes obtained audio data.| |OH_AudioStream_Result OH_AudioCapturer_Release(OH_AudioCapturer* capturer)|Releases the audio capturer instance.|

  1. Destroy the audio stream builder.

    When the builder is no longer used, release related resources.

    OH_AudioStreamBuilder_Destroy(builder);
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Development

harmony 鸿蒙Audio Decoding

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Audio Encoding

harmony 鸿蒙Audio Input Device Management

harmony 鸿蒙Audio Output Device Management

harmony 鸿蒙Audio Playback Concurrency Policy

harmony 鸿蒙Audio Playback Development

0  赞