DEV Community

HarmonyOS
HarmonyOS

Posted on

After setting the audio stream in AudioRenderer, the output device does not meet expectations.

Read the original article:After setting the audio stream in AudioRenderer, the output device does not meet expectations.

After setting the audio stream in AudioRenderer, the output device does not meet expectations

Problem Description

export class AudioRenderPlayer {
  private renderModel: audio.AudioRenderer | undefined = undefined;
  private audioStreamInfo: audio.AudioStreamInfo = {
    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, // Sampling rate
    channels: audio.AudioChannel.CHANNEL_1, // Channel
    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format
    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format
  }
  private audioRendererInfo: audio.AudioRendererInfo = {
    usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, // Audio Stream Usage Type
    rendererFlags: 0 // Audio Renderer Flag
  }
  private audioRendererOptions: audio.AudioRendererOptions = {
    streamInfo: this.audioStreamInfo,
    rendererInfo: this.audioRendererInfo
  }
}
Enter fullscreen mode Exit fullscreen mode

By configuring the audio rendering parameters through the above code to create an AudioRenderer instance later, it is expected to use the speakers for playback, but the sound actually comes from the earpiece. Why is this happening?

Background Knowledge

The audio stream type is a key attribute that defines how audio data is played and recorded. For playback streams, the type is determined by StreamUsage. The audio stream type has a decisive influence on the selection of an output device.

The audio stream type in the problem code is STREAM_USAGE_VOICE_COMMUNICATION. From the audio stream usage type, we can know that the playback stream type of STREAM_USAGE_VOICE_COMMUNICATION is a voice message, and the volume type should belong to VOICE_CALL.

Solution

The volume type of the STREAM_USAGE_VOICE_COMMUNICATION audio stream is call volume, and the default output device is the earpiece, so the audio sound will be emitted from the earpiece.

Revision Suggestions

You can try changing the audio stream type to STREAM_USAGE_MUSIC, which will play from the speaker by default. Modify the code as follows:

let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Configure according to the business scenario. Refer to StreamUsage.
  rendererFlags: 0 // Audio renderer flags.
};
Enter fullscreen mode Exit fullscreen mode

If the default input/output device does not meet the usage requirements, the application can also call the relevant interface to actively modify the default output device. It is important to note that this interface is only applicable in scenarios where the StreamUsage is set to voice message (STREAM_USAGE_VOICE_MESSAGE), VoIP voice call (STREAM_USAGE_VOICE_COMMUNICATION), or VoIP video call (STREAM_USAGE_VIDEO_COMMUNICATION).

Verification Result

This solution was verified using DevEco Studio 6.0.0 Release.

Written by Recep Sadullah Yegin

Top comments (0)