openharmony 鸿蒙 arkts-apis-audio-f

2026-08-25 浏览 (1)

Functions

NOTE

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { audio } from '@kit.AudioKit';

audio.getAudioManager

getAudioManager(): AudioManager

Obtains an AudioManager instance.

Atomic service API: This API can be used in atomic services since API version 23.

System capability: SystemCapability.Multimedia.Audio.Core

Return value

TypeDescription
AudioManagerAudioManager instance.

Example

import { audio } from '@kit.AudioKit';

let audioManager = audio.getAudioManager();

audio.createAudioRenderer8+

createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback<AudioRenderer>): void

Creates an AudioRenderer instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Renderer

Parameters

NameTypeMandatoryDescription
optionsAudioRendererOptionsYesRenderer configurations.
callbackAsyncCallback<AudioRenderer>YesCallback used to return the result. If the operation is successful, err is undefined and data is the AudioRenderer instance obtained; otherwise, err is an error object.

Example

import { audio } from '@kit.AudioKit';

let audioStreamInfo: audio.AudioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate.
  channels: audio.AudioChannel.CHANNEL_2, // Channel.
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format.
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format.
};

let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario.
  rendererFlags: 0 // AudioRenderer flag.
};

let audioRendererOptions: audio.AudioRendererOptions = {
  streamInfo: audioStreamInfo,
  rendererInfo: audioRendererInfo
};

let audioRenderer: audio.AudioRenderer;

audio.createAudioRenderer(audioRendererOptions,(err, data) => {
  if (err) {
    console.error(`AudioRenderer Created: Error: ${err}`);
  } else {
    console.info('AudioRenderer Created: SUCCESS');
    audioRenderer = data;
  }
});

audio.createAudioRenderer8+

createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer>

Creates an AudioRenderer instance. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Renderer

Parameters

NameTypeMandatoryDescription
optionsAudioRendererOptionsYesRenderer configurations.

Return value

TypeDescription
Promise<AudioRenderer>Promise used to return the AudioRenderer instance.

Example

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let audioStreamInfo: audio.AudioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate.
  channels: audio.AudioChannel.CHANNEL_2, // Channel.
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format.
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format.
};

let audioRendererInfo: audio.AudioRendererInfo = {
  usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type: music. Set this parameter based on the service scenario.
  rendererFlags: 0 // AudioRenderer flag.
};

let audioRendererOptions: audio.AudioRendererOptions = {
  streamInfo: audioStreamInfo,
  rendererInfo: audioRendererInfo
};

let audioRenderer: audio.AudioRenderer;

audio.createAudioRenderer(audioRendererOptions).then((data) => {
  audioRenderer = data;
  console.info('AudioFrameworkRenderLog: AudioRenderer Created : SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`);
});

audio.createAudioCapturer8+

createAudioCapturer(options: AudioCapturerOptions, callback: AsyncCallback<AudioCapturer>): void

Creates an AudioCapturer instance. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.Audio.Capturer

Required permissions: ohos.permission.MICROPHONE

This permission is required when SourceType is set to SOURCE_TYPE_MIC, SOURCE_TYPE_VOICE_RECOGNITION, SOURCE_TYPE_VOICE_COMMUNICATION, SOURCE_TYPE_VOICE_MESSAGE, or SOURCE_TYPE_CAMCORDER.

Parameters

NameTypeMandatoryDescription
optionsAudioCapturerOptionsYesCapturer configurations.
callbackAsyncCallback<AudioCapturer>YesCallback used to return the result. If the operation is successful, err is undefined and data is the AudioCapturer instance obtained; otherwise, err is an error object. If the operation fails, an error object with one of the following error codes is returned:
Error code 6800301: indicates a parameter verification exception, permission verification exception, or system processing exception. For details, see system logs.
Error code 6800101: indicates that a mandatory parameter is null or the parameter type is incorrect.

Example

import { audio } from '@kit.AudioKit';

let audioStreamInfo: audio.AudioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate.
  channels: audio.AudioChannel.CHANNEL_2, // Channel.
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format.
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format.
};

let audioCapturerInfo: audio.AudioCapturerInfo = {
  source: audio.SourceType.SOURCE_TYPE_MIC, // Audio source type: microphone. Set this parameter based on the service scenario.
  capturerFlags: 0 // AudioCapturer flag.
};

let audioCapturerOptions: audio.AudioCapturerOptions = {
  streamInfo: audioStreamInfo,
  capturerInfo: audioCapturerInfo
};

let audioCapturer: audio.AudioCapturer;

audio.createAudioCapturer(audioCapturerOptions, (err, data) => {
  if (err) {
    console.error(`AudioCapturer Created : Error: ${err}`);
  } else {
    console.info('AudioCapturer Created : SUCCESS');
    audioCapturer = data;
  }
});

audio.createAudioCapturer8+

createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer>

Creates an AudioCapturer instance. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.Audio.Capturer

Required permissions: ohos.permission.MICROPHONE

This permission is required when SourceType is set to SOURCE_TYPE_MIC, SOURCE_TYPE_VOICE_RECOGNITION, SOURCE_TYPE_VOICE_COMMUNICATION, SOURCE_TYPE_VOICE_MESSAGE, or SOURCE_TYPE_CAMCORDER.

Parameters

NameTypeMandatoryDescription
optionsAudioCapturerOptionsYesCapturer configurations.

Return value

TypeDescription
Promise<AudioCapturer>Promise used to return the result. If the operation is successful, an AudioCapturer instance is returned; otherwise, an error object with either of the following error codes is returned:
Error code 6800301: indicates a parameter verification exception, permission verification exception, or system processing exception. For details, see system logs.
Error code 6800101: indicates that a mandatory parameter is null or the parameter type is incorrect.

Example

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let audioStreamInfo: audio.AudioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate.
  channels: audio.AudioChannel.CHANNEL_2, // Channel.
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format.
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format.
};

let audioCapturerInfo: audio.AudioCapturerInfo = {
  source: audio.SourceType.SOURCE_TYPE_MIC, // Audio source type: microphone. Set this parameter based on the service scenario.
  capturerFlags: 0 // AudioCapturer flag.
};

let audioCapturerOptions:audio.AudioCapturerOptions = {
  streamInfo: audioStreamInfo,
  capturerInfo: audioCapturerInfo
};

let audioCapturer: audio.AudioCapturer;

audio.createAudioCapturer(audioCapturerOptions).then((data) => {
  audioCapturer = data;
  console.info('AudioCapturer Created : SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`AudioCapturer Created : ERROR : ${err}`);
});

audio.createAudioLoopback20+

createAudioLoopback(mode: AudioLoopbackMode): Promise<AudioLoopback>

Creates an AudioLoopback instance. This API uses a promise to return the result.

Before using createAudioLoopback, call isAudioLoopbackSupported to check whether the system supports the audio loopback mode.

System capability: SystemCapability.Multimedia.Audio.Capturer

Required permissions: ohos.permission.MICROPHONE

Parameters

NameTypeMandatoryDescription
modeAudioLoopbackModeYesAudio loopback mode.

Return value

TypeDescription
Promise<AudioLoopback>Promise used to return the result. If the operation is successful, an AudioLoopback instance is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see Universal Error Codes and Audio Error Codes.

IDError Message
201Permission denied.
801Unsupported API.
6800101Parameter verification failed.
6800104Loopback mode is unsupported.

Example

import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';

let audioLoopback: audio.AudioLoopback;

audio.createAudioLoopback(audio.AudioLoopbackMode.HARDWARE).then((data) => {
  audioLoopback = data;
  console.info('AudioLoopback Created : SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`AudioLoopback Created : ERROR : ${err}`);
});

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-native-audiostreambuilder-h

openharmony 鸿蒙 capi-ohaudiosuite

openharmony 鸿蒙 js-apis-inner-multimedia-systemSoundPlayer

openharmony 鸿蒙 arkts-apis-audio-i

openharmony 鸿蒙 capi-ohaudio

openharmony 鸿蒙 capi-ohmidi

openharmony 鸿蒙 arkts-apis-audio-AudioManager

openharmony 鸿蒙 capi-ohaudiosuite-oh-audiosuite-spacerenderpositionparams

openharmony 鸿蒙 capi-ohaudio-oh-audiostreaminfo

openharmony 鸿蒙 errorcode-ringtone

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