openharmony 鸿蒙 using-right-streamusage-and-sourcetype

2025-06-12 浏览 (1)

Selecting the Appropriate Audio Stream Types

Audio stream types are crucial for determining the mode of audio playback and recording. For audio playback streams, the stream type is determined by StreamUsage. For audio recording streams, the stream type is determined by SourceType. These types pose a significant impact on volume control, audio focus management, and input/output device selection.

To ensure that the audio behavior meets expectations and delivers a superior user experience, you need to select an appropriate audio stream type based on the use cases and requirements.

This topic describes typical audio stream types and their use cases and their effects on audio services. It also describes how to set the audio stream type when you use different methods to implement audio playback and recording.

Typical Audio Stream Types and Use Cases

Stream Types for Audio Playback

The following table describes the typical stream types used for audio playback. The type is specified by StreamUsage.

Stream TypeUse Case
STREAM_USAGE_MUSICMusic playback and other media scenarios, for example, using SoundPool to play short sounds.
STREAM_USAGE_MOVIEPlaying various video content such as short videos, movies, and TV series.
STREAM_USAGE_AUDIOBOOKPlaying audiobooks, news, and podcasts.
STREAM_USAGE_GAMEIn-game background scores and character dubbing, ensuring uninterrupted background music. It is recommended that you use STREAM_USAGE_VOICE_COMMUNICATION for in-game voice.
STREAM_USAGE_NAVIGATIONVoice broadcast in navigation scenarios.
STREAM_USAGE_VOICE_MESSAGEPlaying voice short messages.
STREAM_USAGE_VOICE_COMMUNICATIONVoIP voice calls.
STREAM_USAGE_ALARMPlaying alarms.
STREAM_USAGE_RINGTONEPlaying ringtones for VoIP calls.
STREAM_USAGE_NOTIFICATIONPlaying notification tones and prompt tones.

Stream Types for Audio Recording

The following table describes the typical stream types used for audio recording. The type is specified by SourceType.

Stream TypeUse Case
SOURCE_TYPE_MICCommon audio recording.
SOURCE_TYPE_RECOGNITION9+Voice recognition.
SOURCE_TYPE_PLAYBACK_CAPTURE(Deprecated in API version 12) Recording raw audio data sent by other applications to the system for playback.
Audio Kit does not provide APIs for internal recording. You can use AVScreenCapture to perform internal recording.
SOURCE_TYPE_VOICE_COMMUNICATIONVoIP voice calls.
SOURCE_TYPE_VOICE_MESSAGERecording voice short messages.
SOURCE_TYPE_CAMCORDER13+Camera recording.
SOURCE_TYPE_UNPROCESSD14+Obtaining pure audio data captured by the microphone (without any system processing).

Effects of Audio Stream Types on Audio Services

Different stream types affect user experience in volume control and system performance in adjusting the audio focus and selecting input/output devices. In addition, the system configures corresponding optimization processing strategies for the captured audio data based on the recording stream type. Therefore, the recording stream type affects the audio recording quality. For example, using SOURCE_TYPE_MIC instead of SOURCE_TYPE_VOICE_COMMUNICATION in a VoIP call scenario may disable noise reduction and ambient sound elimination optimizations, leading to a poor VoIP call experience. You are advised to select the appropriate audio stream type based on the service scenario.

Volume Control

The audio playback stream type (specified by StreamUsage) determines the volume type (specified by AudioVolumeType) to which the audio stream belongs. Each volume type (such as MEDIA, RINGTONE, ALARM, and VOICE_CALL) has an independent volume value, which can be adjusted independently on the UI.

The table below lists the mappings between the typical audio playback stream types and volume types.

Stream TypeVolume Type
MUSIC, MOVIE, AUDIOBOOK, GAMEMEDIA
RINGTONE, NOTIFICATIONRINGTONE
VOICE_COMMUNICATIONVOICE_CALL
ALARMALARM

Adjusting the Audio Focus

The audio stream type plays a key role in audio focus management. Different types of audio streams have different default priorities and processing modes for the audio focus.

When an application starts audio playback or recording, the system automatically applies for focus based on the audio stream type. This may interrupt or duck other audio streams. For details about the audio focus, see Introduction to Audio Focus and Audio Session.

This section describes only how the typical audio stream types affect the audio focus.

  • When a Navigation stream starts playing, the Music stream that is playing will be ducked. After the Navigation stream ends, the volume of the Music stream will be restored.

  • When a Movie stream starts playing, the Music stream that is playing will be stopped. When the Movie stream ends, the Music stream will not be automatically resumed, and the corresponding application will not receive any resume notification.

  • When a VoiceCommunication stream starts, the Music stream that is playing will be paused. When the VoiceCommunication stream ends, the music application will receive a notification to resume the playback.

  • The Music stream and Game stream can be mixed and played concurrently without affecting each other.

  • When a VoiceMessage stream starts, the Music stream that is playing will be paused. When the VoiceMessage stream ends, the music application will receive a notification to resume the playback.

Selecting Input/Output Devices

The system selects an input/output device based on the audio stream type.

The following lists the input/output devices selected for the typical audio stream types.

  • For a MUSIC stream, the default output device is a speaker.

  • For a VoiceCommunication stream, the default input device is a microphone, and the default output device is an earpiece.

  • For an Alarm stream, the default output device is a speaker. If a Bluetooth headset is connected before an ALARM stream is played, the audio is output from both the speaker and Bluetooth headset.

If the default input/output devices do not meet service requirements, you can call related APIs to change the devices. When using AudioRenderer to develop audio playback, you can call setDefaultOutputDevice to set the default sound device.

Setting the Audio Stream Type

The application can implement audio playback or recording by using a plurality of methods. Therefore, manners of setting the audio stream type are also different.

Common methods for setting the audio playback stream type are as follows:

Common methods for setting the audio recording stream type are as follows:

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Audio Kit

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Overview

harmony 鸿蒙Audio Monitoring

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Global Audio Input Device Management

harmony 鸿蒙Introduction to Audio Kit

harmony 鸿蒙Audio Latency Management

harmony 鸿蒙Responding to Audio Output Device Changes

harmony 鸿蒙Global Audio Output Device Management

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/ARMYlm