HarmonyOS API:@ohos.multimedia.audio (音频管理)

joytrian
发布于 2023-4-4 15:57
浏览
0收藏

版本:v3.1 Beta

@ohos.multimedia.audio (音频管理)

更新时间: 2023-03-17 18:27


音频管理提供管理音频的一些基础能力,包括对音频音量、音频设备的管理,以及对音频数据的采集和渲染等。


该模块提供以下音频相关的常用功能:


说明

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import audio from '@ohos.multimedia.audio';

audio.getAudioManager

getAudioManager(): AudioManager

获取音频管理器。

系统能力: SystemCapability.Multimedia.Audio.Core

返回值:

类型

说明

​AudioManager​

音频管理类。

示例:

let audioManager = audio.getAudioManager();

audio.createAudioRenderer8+

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

获取音频渲染器。使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Renderer

参数:

参数名

类型

必填

说明

options

​AudioRendererOptions​

配置渲染器。

callback

AsyncCallback<​​AudioRenderer​​>

音频渲染器对象。

示例:

import featureAbility from '@ohos.ability.featureAbility';
import fileio from '@ohos.fileio';
import audio from '@ohos.multimedia.audio';

let audioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
  channels: audio.AudioChannel.CHANNEL_1,
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

let audioRendererInfo = {
  content: audio.ContentType.CONTENT_TYPE_SPEECH,
  usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
  rendererFlags: 0
}

let audioRendererOptions = {
  streamInfo: audioStreamInfo,
  rendererInfo: audioRendererInfo
}

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

audio.createAudioRenderer8+

createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer>

获取音频渲染器。使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Renderer

参数:

参数名

类型

必填

说明

options

​AudioRendererOptions​

配置渲染器。

返回值:

类型

说明

Promise<​​AudioRenderer​​>

音频渲染器对象。

示例:

import featureAbility from '@ohos.ability.featureAbility';
import fileio from '@ohos.fileio';
import audio from '@ohos.multimedia.audio';

let audioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
  channels: audio.AudioChannel.CHANNEL_1,
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

let audioRendererInfo = {
  content: audio.ContentType.CONTENT_TYPE_SPEECH,
  usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION,
  rendererFlags: 0
}

let audioRendererOptions = {
  streamInfo: audioStreamInfo,
  rendererInfo: audioRendererInfo
}

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

audio.createAudioCapturer8+

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

获取音频采集器。使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Capturer

需要权限: ohos.permission.MICROPHONE

参数:

参数名

类型

必填

说明

options

​AudioCapturerOptions​

配置音频采集器。

callback

AsyncCallback<​​AudioCapturer​​>

音频采集器对象。

示例:

import audio from '@ohos.multimedia.audio';
let audioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
  channels: audio.AudioChannel.CHANNEL_2,
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

let audioCapturerInfo = {
  source: audio.SourceType.SOURCE_TYPE_MIC,
  capturerFlags: 0
}

let audioCapturerOptions = {
  streamInfo: audioStreamInfo,
  capturerInfo: audioCapturerInfo
}

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

audio.createAudioCapturer8+

createAudioCapturer(options: AudioCapturerOptions): Promise<AudioCapturer>

获取音频采集器。使用promise 方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Capturer

需要权限: ohos.permission.MICROPHONE

参数:

参数名

类型

必填

说明

options

​AudioCapturerOptions​

配置音频采集器。

返回值:

类型

说明

Promise<​​AudioCapturer​​>

音频采集器对象

示例:

import audio from '@ohos.multimedia.audio';

let audioStreamInfo = {
  samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
  channels: audio.AudioChannel.CHANNEL_2,
  sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE,
  encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW
}

let audioCapturerInfo = {
  source: audio.SourceType.SOURCE_TYPE_MIC,
  capturerFlags: 0
}

let audioCapturerOptions = {
  streamInfo: audioStreamInfo,
  capturerInfo: audioCapturerInfo
}

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

AudioVolumeType

枚举,音频流类型。

系统能力: SystemCapability.Multimedia.Audio.Volume

名称

说明

VOICE_CALL8+

0

语音电话。

RINGTONE

2

铃声。

MEDIA

3

媒体。

VOICE_ASSISTANT8+

9

语音助手。

InterruptMode9+

枚举,焦点模型。

系统能力: SystemCapability.Multimedia.Audio.Interrupt

名称

说明

INDEPENDENT_MODE

1

独立焦点模式。

DeviceFlag

枚举,可获取的设备种类。

系统能力: SystemCapability.Multimedia.Audio.Device

名称

说明

OUTPUT_DEVICES_FLAG

1

输出设备。

INPUT_DEVICES_FLAG

2

输入设备。

ALL_DEVICES_FLAG

3

所有设备。

DeviceRole

枚举,设备角色。

系统能力: SystemCapability.Multimedia.Audio.Device

名称

说明

INPUT_DEVICE

1

输入设备角色。

OUTPUT_DEVICE

2

输出设备角色。

DeviceType

枚举,设备类型。

系统能力: SystemCapability.Multimedia.Audio.Device

名称

说明

INVALID

0

无效设备。

EARPIECE

1

听筒。

SPEAKER

2

扬声器。

WIRED_HEADSET

3

有线耳机,带麦克风。

WIRED_HEADPHONES

4

有线耳机,无麦克风。

BLUETOOTH_SCO

7

蓝牙设备SCO(Synchronous Connection Oriented)连接。

BLUETOOTH_A2DP

8

蓝牙设备A2DP(Advanced Audio Distribution Profile)连接。

MIC

15

麦克风。

USB_HEADSET

22

USB耳机,带麦克风。

DEFAULT9+

1000

默认设备类型。

AudioRingMode

枚举,铃声模式。

系统能力: SystemCapability.Multimedia.Audio.Communication

名称

说明

RINGER_MODE_SILENT

0

静音模式。

RINGER_MODE_VIBRATE

1

震动模式。

RINGER_MODE_NORMAL

2

响铃模式。

AudioSampleFormat8+

枚举,音频采样格式。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

SAMPLE_FORMAT_INVALID

-1

无效格式。

SAMPLE_FORMAT_U8

0

无符号8位整数。

SAMPLE_FORMAT_S16LE

1

带符号的16位整数,小尾数。

SAMPLE_FORMAT_S24LE

2

带符号的24位整数,小尾数。

由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。

SAMPLE_FORMAT_S32LE

3

带符号的32位整数,小尾数。

由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。

SAMPLE_FORMAT_F32LE9+

4

带符号的32位浮点数,小尾数。

由于系统限制,该采样格式仅部分设备支持,请根据实际情况使用。

AudioChannel8+

枚举, 音频声道。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

CHANNEL_1

0x1 << 0

单声道。

CHANNEL_2

0x1 << 1

双声道。

AudioSamplingRate8+

枚举,音频采样率,具体设备支持的采样率规格会存在差异。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

SAMPLE_RATE_8000

8000

采样率为8000。

SAMPLE_RATE_11025

11025

采样率为11025。

SAMPLE_RATE_12000

12000

采样率为12000。

SAMPLE_RATE_16000

16000

采样率为16000。

SAMPLE_RATE_22050

22050

采样率为22050。

SAMPLE_RATE_24000

24000

采样率为24000。

SAMPLE_RATE_32000

32000

采样率为32000。

SAMPLE_RATE_44100

44100

采样率为44100。

SAMPLE_RATE_48000

48000

采样率为48000。

SAMPLE_RATE_64000

64000

采样率为64000。

SAMPLE_RATE_96000

96000

采样率为96000。

AudioEncodingType8+

枚举,音频编码类型。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

ENCODING_TYPE_INVALID

-1

无效。

ENCODING_TYPE_RAW

0

PCM编码。

ContentType

枚举,音频内容类型。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

CONTENT_TYPE_UNKNOWN

0

未知类型。

CONTENT_TYPE_SPEECH

1

语音。

CONTENT_TYPE_MUSIC

2

音乐。

CONTENT_TYPE_MOVIE

3

电影。

CONTENT_TYPE_SONIFICATION

4

加密类型。

CONTENT_TYPE_RINGTONE8+

5

铃声。

StreamUsage

枚举,音频流使用类型。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

STREAM_USAGE_UNKNOWN

0

未知类型。

STREAM_USAGE_MEDIA

1

音频。

STREAM_USAGE_VOICE_COMMUNICATION

2

语音通信。

STREAM_USAGE_VOICE_ASSISTANT9+

3

语音播报。

STREAM_USAGE_NOTIFICATION_RINGTONE

6

通知铃声。

AudioState8+

枚举,音频状态。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

STATE_INVALID

-1

无效状态。

STATE_NEW

0

创建新实例状态。

STATE_PREPARED

1

准备状态。

STATE_RUNNING

2

可运行状态。

STATE_STOPPED

3

停止状态。

STATE_RELEASED

4

释放状态。

STATE_PAUSED

5

暂停状态。

AudioRendererRate8+

枚举,音频渲染速度。

系统能力: SystemCapability.Multimedia.Audio.Renderer

名称

说明

RENDER_RATE_NORMAL

0

正常速度。

RENDER_RATE_DOUBLE

1

2倍速。

RENDER_RATE_HALF

2

0.5倍数。

InterruptType

枚举,中断类型。

系统能力: SystemCapability.Multimedia.Audio.Renderer

名称

说明

INTERRUPT_TYPE_BEGIN

1

音频播放中断事件开始。

INTERRUPT_TYPE_END

2

音频播放中断事件结束。

InterruptHint

枚举,中断提示。

系统能力: SystemCapability.Multimedia.Audio.Renderer

名称

说明

INTERRUPT_HINT_NONE8+

0

无提示。

INTERRUPT_HINT_RESUME

1

提示音频恢复。

INTERRUPT_HINT_PAUSE

2

提示音频暂停。

INTERRUPT_HINT_STOP

3

提示音频停止。

INTERRUPT_HINT_DUCK

4

提示音频躲避。(躲避:音量减弱,而不会停止)

INTERRUPT_HINT_UNDUCK8+

5

提示音量恢复。

AudioStreamInfo8+

音频流信息。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

类型

必填

说明

samplingRate

​AudioSamplingRate​

音频文件的采样率。

channels

​AudioChannel​

音频文件的通道数。

sampleFormat

​AudioSampleFormat​

音频采样格式。

encodingType

​AudioEncodingType​

音频编码格式。

AudioRendererInfo8+

音频渲染器信息。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

类型

必填

说明

content

​ContentType​

媒体类型。

usage

​StreamUsage​

音频流使用类型。

rendererFlags

number

音频渲染器标志。

AudioRendererOptions8+

音频渲染器选项信息。

系统能力: SystemCapability.Multimedia.Audio.Renderer

名称

类型

必填

说明

streamInfo

​AudioStreamInfo​

表示音频流信息。

rendererInfo

​AudioRendererInfo​

表示渲染器信息。

DeviceChangeAction

描述设备连接状态变化和设备信息。

系统能力: SystemCapability.Multimedia.Audio.Device

名称

类型

必填

说明

type

​DeviceChangeType​

设备连接状态变化。

deviceDescriptors

​AudioDeviceDescriptors​

设备信息。

DeviceChangeType

枚举,设备连接状态变化。

系统能力: SystemCapability.Multimedia.Audio.Device

名称

说明

CONNECT

0

设备连接。

DISCONNECT

1

断开设备连接。

AudioCapturerOptions8+

音频采集器选项信息。

系统能力: 以下各项对应的系统能力均为SystemCapability.Multimedia.Audio.Capturer

名称

类型

必填

说明

streamInfo

​AudioStreamInfo​

表示音频流信息。

capturerInfo

​AudioCapturerInfo​

表示采集器信息。

AudioCapturerInfo8+

描述音频采集器信息。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

类型

必填

说明

source

​SourceType​

音源类型。

capturerFlags

number

音频采集器标志。

SourceType8+

枚举,音源类型。

系统能力: SystemCapability.Multimedia.Audio.Core

名称

说明

SOURCE_TYPE_INVALID

-1

无效的音频源。

SOURCE_TYPE_MIC

0

Mic音频源。

SOURCE_TYPE_VOICE_RECOGNITION9+

1

语音识别源。

SOURCE_TYPE_VOICE_COMMUNICATION

7

语音通话场景的音频源。

AudioScene8+

枚举,音频场景。

系统能力: SystemCapability.Multimedia.Audio.Communication

名称

说明

AUDIO_SCENE_DEFAULT

0

默认音频场景。

AUDIO_SCENE_VOICE_CHAT

3

语音聊天模式。

AudioManager

管理音频音量和音频设备。在调用AudioManager的接口前,需要先通过​​getAudioManager​​创建实例。

setAudioParameter

setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void

音频参数设置,使用callback方式异步返回结果。

本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。

需要权限: ohos.permission.MODIFY_AUDIO_SETTINGS

系统能力: SystemCapability.Multimedia.Audio.Core

参数:

参数名

类型

必填

说明

key

string

被设置的音频参数的键。

value

string

被设置的音频参数的值。

callback

AsyncCallback<void>

回调返回设置成功或失败。

示例:

audioManager.setAudioParameter('key_example', 'value_example', (err) => {
  if (err) {
    console.error(`Failed to set the audio parameter. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate a successful setting of the audio parameter.');
});

setAudioParameter

setAudioParameter(key: string, value: string): Promise<void>

音频参数设置,使用Promise方式异步返回结果。

本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。

需要权限: ohos.permission.MODIFY_AUDIO_SETTINGS

系统能力: SystemCapability.Multimedia.Audio.Core

参数:

参数名

类型

必填

说明

key

string

被设置的音频参数的键。

value

string

被设置的音频参数的值。

返回值:

类型

说明

Promise<void>

Promise回调返回设置成功或失败。

示例:

audioManager.setAudioParameter('key_example', 'value_example').then(() => {
  console.info('Promise returned to indicate a successful setting of the audio parameter.');
});

getAudioParameter

getAudioParameter(key: string, callback: AsyncCallback<string>): void

获取指定音频参数值,使用callback方式异步返回结果。

本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。

系统能力: SystemCapability.Multimedia.Audio.Core

参数:

参数名

类型

必填

说明

key

string

待获取的音频参数的键。

callback

AsyncCallback<string>

回调返回获取的音频参数的值。

示例:

audioManager.getAudioParameter('key_example', (err, value) => {
  if (err) {
    console.error(`Failed to obtain the value of the audio parameter. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the value of the audio parameter is obtained ${value}.`);
});

getAudioParameter

getAudioParameter(key: string): Promise<string>

获取指定音频参数值,使用Promise方式异步返回结果。

本接口的使用场景为根据硬件设备支持能力扩展音频配置。在不同的设备平台上,所支持的音频参数会存在差异。示例代码内使用样例参数,实际支持的音频配置参数见具体设备平台的资料描述。

系统能力: SystemCapability.Multimedia.Audio.Core

参数:

参数名

类型

必填

说明

key

string

待获取的音频参数的键。

返回值:

类型

说明

Promise<string>

Promise回调返回获取的音频参数的值。

示例:

audioManager.getAudioParameter('key_example').then((value) => {
  console.info(`Promise returned to indicate that the value of the audio parameter is obtained ${value}.`);
});

getAudioScene8+

getAudioScene(callback: AsyncCallback<AudioScene>): void

获取音频场景模式,使用callback方式返回异步结果。

系统能力: SystemCapability.Multimedia.Audio.Communication

参数:

参数名

类型

必填

说明

callback

AsyncCallback<​​AudioScene​​>

用于返回音频场景模式的回调。

示例:

audioManager.getAudioScene((err, value) => {
  if (err) {
    console.error(`Failed to obtain the audio scene mode. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the audio scene mode is obtained ${value}.`);
});

getAudioScene8+

getAudioScene(): Promise<AudioScene>

获取音频场景模式,使用Promise方式返回异步结果。

系统能力: SystemCapability.Multimedia.Audio.Communication

返回值:

类型

说明

Promise<​​AudioScene​​>

用于返回音频场景模式的回调。

示例:

audioManager.getAudioScene().then((value) => {
  console.info(`Promise returned to indicate that the audio scene mode is obtained ${value}.`);
}).catch ((err) => {
  console.error(`Failed to obtain the audio scene mode ${err}`);
});

setVolume

setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void

设置指定流的音量,使用callback方式异步返回结果。

需要权限: ohos.permission.ACCESS_NOTIFICATION_POLICY

仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

volume

number

音量等级,可设置范围通过getMinVolume和getMaxVolume获取。

callback

AsyncCallback<void>

回调表示成功还是失败。

示例:

audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err) => {
  if (err) {
    console.error(`Failed to set the volume. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate a successful volume setting.');
});

setVolume

setVolume(volumeType: AudioVolumeType, volume: number): Promise<void>

设置指定流的音量,使用Promise方式异步返回结果。

需要权限: ohos.permission.ACCESS_NOTIFICATION_POLICY

仅设置铃声(即volumeType为AudioVolumeType.RINGTONE)在静音和非静音状态切换时需要该权限。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

volume

number

音量等级,可设置范围通过getMinVolume和getMaxVolume获取。

返回值:

类型

说明

Promise<void>

Promise回调表示成功还是失败。

示例:

audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(() => {
  console.info('Promise returned to indicate a successful volume setting.');
});

getVolume

getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

获取指定流的音量,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

callback

AsyncCallback<number>

回调返回音量大小。

示例:

audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the volume. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate that the volume is obtained.');
});

getVolume

getVolume(volumeType: AudioVolumeType): Promise<number>

获取指定流的音量,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

返回值:

类型

说明

Promise<number>

Promise回调返回音量大小。

示例:

audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => {
  console.info(`Promise returned to indicate that the volume is obtained ${value} .`);
});

getMinVolume

getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

获取指定流的最小音量,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

callback

AsyncCallback<number>

回调返回最小音量。

示例:

audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the minimum volume. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the minimum volume is obtained. ${value}`);
});

getMinVolume

getMinVolume(volumeType: AudioVolumeType): Promise<number>

获取指定流的最小音量,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

返回值:

类型

说明

Promise<number>

Promise回调返回最小音量。

示例:

audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => {
  console.info(`Promised returned to indicate that the minimum volume is obtained. ${value}`);
});

getMaxVolume

getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void

获取指定流的最大音量,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

callback

AsyncCallback<number>

回调返回最大音量大小。

示例:

audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the maximum volume. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the maximum volume is obtained. ${value}`);
});

getMaxVolume

getMaxVolume(volumeType: AudioVolumeType): Promise<number>

获取指定流的最大音量,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

返回值:

类型

说明

Promise<number>

Promise回调返回最大音量大小。

示例:

audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data) => {
  console.info('Promised returned to indicate that the maximum volume is obtained.');
});

mute

mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void

设置指定音量流静音,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

mute

boolean

静音状态,true为静音,false为非静音。

callback

AsyncCallback<void>

回调表示成功还是失败。

示例:

audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => {
  if (err) {
    console.error(`Failed to mute the stream. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate that the stream is muted.');
});

mute

mute(volumeType: AudioVolumeType, mute: boolean): Promise<void>

设置指定音量流静音,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

mute

boolean

静音状态,true为静音,false为非静音。

返回值:

类型

说明

Promise<void>

Promise回调表示成功还是失败。

示例:

audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => {
  console.info('Promise returned to indicate that the stream is muted.');
});

isMute

isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void

获取指定音量流是否被静音,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

callback

AsyncCallback<boolean>

回调返回流静音状态,true为静音,false为非静音。

示例:

audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the mute status. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the mute status of the stream is obtained. ${value}`);
});

isMute

isMute(volumeType: AudioVolumeType): Promise<boolean>

获取指定音量流是否被静音,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

返回值:

类型

说明

Promise<boolean>

Promise回调返回流静音状态,true为静音,false为非静音。

示例:

audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => {
  console.info(`Promise returned to indicate that the mute status of the stream is obtained ${value}.`);
});

isActive

isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void

获取指定音量流是否为活跃状态,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

callback

AsyncCallback<boolean>

回调返回流的活跃状态,true为活跃,false为不活跃。

示例:

audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the active status of the stream. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the active status of the stream is obtained ${value}.`);
});

isActive

isActive(volumeType: AudioVolumeType): Promise<boolean>

获取指定音量流是否为活跃状态,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Volume

参数:

参数名

类型

必填

说明

volumeType

​AudioVolumeType​

音量流类型。

返回值:

类型

说明

Promise<boolean>

Promise回调返回流的活跃状态,true为活跃,false为不活跃。

示例:

audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => {
  console.info(`Promise returned to indicate that the active status of the stream is obtained ${value}.`);
});

setRingerMode

setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void

设置铃声模式,使用callback方式异步返回结果。

需要权限: ohos.permission.ACCESS_NOTIFICATION_POLICY

仅在静音和非静音状态切换时需要该权限。

系统能力: SystemCapability.Multimedia.Audio.Communication

参数:

参数名

类型

必填

说明

mode

​AudioRingMode​

音频铃声模式。

callback

AsyncCallback<void>

回调返回设置成功或失败。

示例:

audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => {
  if (err) {
    console.error(`Failed to set the ringer mode. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate a successful setting of the ringer mode.');
});

setRingerMode

setRingerMode(mode: AudioRingMode): Promise<void>

设置铃声模式,使用Promise方式异步返回结果。

需要权限: ohos.permission.ACCESS_NOTIFICATION_POLICY

仅在静音和非静音状态切换时需要该权限。

系统能力: SystemCapability.Multimedia.Audio.Communication

参数:

参数名

类型

必填

说明

mode

​AudioRingMode​

音频铃声模式。

返回值:

类型

说明

Promise<void>

Promise回调返回设置成功或失败。

示例:

audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => {
  console.info('Promise returned to indicate a successful setting of the ringer mode.');
});

getRingerMode

getRingerMode(callback: AsyncCallback<AudioRingMode>): void

获取铃声模式,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Communication

参数:

参数名

类型

必填

说明

callback

AsyncCallback<​​AudioRingMode​​>

回调返回系统的铃声模式。

示例:

audioManager.getRingerMode((err, value) => {
  if (err) {
    console.error(`Failed to obtain the ringer mode. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the ringer mode is obtained ${value}.`);
});

getRingerMode

getRingerMode(): Promise<AudioRingMode>

获取铃声模式,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Communication

返回值:

类型

说明

Promise<​​AudioRingMode​​>

Promise回调返回系统的铃声模式。

示例:

audioManager.getRingerMode().then((value) => {
  console.info(`Promise returned to indicate that the ringer mode is obtained ${value}.`);
});

getDevices

getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void

获取音频设备列表,使用callback方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

deviceFlag

​DeviceFlag​

设备类型的flag。

callback

AsyncCallback<​​AudioDeviceDescriptors​​>

回调,返回设备列表。

示例:

audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value) => {
  if (err) {
    console.error(`Failed to obtain the device list. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate that the device list is obtained.');
});

getDevices

getDevices(deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors>

获取音频设备列表,使用Promise方式异步返回结果。

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

deviceFlag

​DeviceFlag​

设备类型的flag。

返回值:

类型

说明

Promise<​​AudioDeviceDescriptors​​>

Promise回调返回设备列表。

示例:

audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
  console.info('Promise returned to indicate that the device list is obtained.');
});

setMicrophoneMute

setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void

设置麦克风静音状态,使用callback方式异步返回结果。

需要权限: ohos.permission.MICROPHONE

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

mute

boolean

待设置的静音状态,true为静音,false为非静音。

callback

AsyncCallback<void>

回调返回设置成功或失败。

示例:

audioManager.setMicrophoneMute(true, (err) => {
  if (err) {
    console.error(`Failed to mute the microphone. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate that the microphone is muted.');
});

setMicrophoneMute

setMicrophoneMute(mute: boolean): Promise<void>

设置麦克风静音状态,使用Promise方式异步返回结果。

需要权限: ohos.permission.MICROPHONE

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

mute

boolean

待设置的静音状态,true为静音,false为非静音。

返回值:

类型

说明

Promise<void>

Promise回调返回设置成功或失败。

示例:

audioManager.setMicrophoneMute(true, (err) => {
  if (err) {
    console.error(`Failed to mute the microphone. ${err}`);
    return;
  }
  console.info('Callback invoked to indicate that the microphone is muted.');
});

isMicrophoneMute

isMicrophoneMute(callback: AsyncCallback<boolean>): void

获取麦克风静音状态,使用callback方式异步返回结果。

需要权限: ohos.permission.MICROPHONE

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

callback

AsyncCallback<boolean>

回调返回系统麦克风静音状态,true为静音,false为非静音。

示例:

audioManager.setMicrophoneMute(true).then(() => {
  console.info('Promise returned to indicate that the microphone is muted.');
});

isMicrophoneMute

isMicrophoneMute(): Promise<boolean>

获取麦克风静音状态,使用Promise方式异步返回结果。

需要权限: ohos.permission.MICROPHONE

系统能力: SystemCapability.Multimedia.Audio.Device

返回值:

类型

说明

Promise<boolean>

Promise回调返回系统麦克风静音状态,true为静音,false为非静音。

示例:

audioManager.isMicrophoneMute((err, value) => {
  if (err) {
    console.error(`Failed to obtain the mute status of the microphone. ${err}`);
    return;
  }
  console.info(`Callback invoked to indicate that the mute status of the microphone is obtained ${value}.`);
});

on('deviceChange')

on(type: 'deviceChange', callback: Callback<DeviceChangeAction>): void

设备更改。音频设备连接状态变化。

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

type

string

订阅的事件的类型。支持事件:'deviceChange'

callback

Callback<​​DeviceChangeAction​​>

获取设备更新详情。

示例:

audioManager.on('deviceChange', (deviceChanged) => {
  console.info(`device change type : ${deviceChanged.type} `);
  console.info(`device descriptor size : ${deviceChanged.deviceDescriptors.length} `);
  console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceRole} `);
  console.info(`device change descriptor : ${deviceChanged.deviceDescriptors[0].deviceType} `);
});

off('deviceChange')

off(type: 'deviceChange', callback?: Callback<DeviceChangeAction>): void

取消订阅音频设备连接变化事件。

系统能力: SystemCapability.Multimedia.Audio.Device

参数:

参数名

类型

必填

说明

type

string

订阅的事件的类型。支持事件:'deviceChange'

callback

Callback<​​DeviceChangeAction​​>

获取设备更新详情。

示例:

audioManager.off('deviceChange', (deviceChanged) => {
  console.info('Should be no callback.');
});

on('interrupt')

on(type: 'interrupt', interrupt: AudioInterrupt, callback: Callback<InterruptAction>): void

请求焦点并开始监听音频打断事件(当应用程序的音频被另一个播放事件中断,回调通知此应用程序)。

用于监听焦点变化。为无音频流的场景(未曾创建AudioRenderer对象),比如FM、语音唤醒等提供焦点变化监听功能。

系统能力: SystemCapability.Multimedia.Audio.Renderer

参数:

参数名

类型

必填

说明

type

string

音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。

interrupt

AudioInterrupt

音频打断事件类型的参数。

callback

Callback<​​InterruptAction​​>

音频打断事件回调方法。

示例:

let interAudioInterrupt = {
  streamUsage:2,
  contentType:0,
  pauseWhenDucked:true
};
audioManager.on('interrupt', interAudioInterrupt, (InterruptAction) => {
  if (InterruptAction.actionType === 0) {
    console.info('An event to gain the audio focus starts.');
    console.info(`Focus gain event: ${InterruptAction} `);
  }
  if (InterruptAction.actionType === 1) {
    console.info('An audio interruption event starts.');
    console.info(`Audio interruption event: ${InterruptAction} `);
  }
});

off('interrupt')(deprecated)

off(type: 'interrupt', interrupt: AudioInterrupt, callback?: Callback<InterruptAction>): void

取消监听音频打断事件(删除监听事件,取消打断)。

说明

从 API version 7 开始支持,从 API version 9 开始废弃。

系统能力: SystemCapability.Multimedia.Audio.Renderer

参数:

参数名

类型

必填

说明

type

string

音频打断事件回调类型,支持的事件为:'interrupt'(多应用之间第二个应用会打断第一个应用,触发该事件)。

interrupt

AudioInterrupt

音频打断事件类型的参数。

callback

Callback<​​InterruptAction​​>

音频打断事件回调方法。

示例:

let interAudioInterrupt = {
  streamUsage:2,
  contentType:0,
  pauseWhenDucked:true
};
audioManager.off('interrupt', interAudioInterrupt, (InterruptAction) => {
  if (InterruptAction.actionType === 0) {
      console.info('An event to release the audio focus starts.');
      console.info(`Focus release event: ${InterruptAction} `);
  }
});


文章转载自:​​https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-audio-0000001478181621-V3?catalogVersion=V3#ZH-CN_TOPIC_0000001478181621__audiomanager​

已于2023-4-4 15:57:00修改
收藏
回复
举报
回复
    相关推荐