From d005a6382829d971a7def0f32f988b453902558f Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Mon, 21 Feb 2022 17:26:43 +0800 Subject: [PATCH 1/9] add md --- zh-cn/application-dev/media/audio-renderer.md | 291 ++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 zh-cn/application-dev/media/audio-renderer.md diff --git a/zh-cn/application-dev/media/audio-renderer.md b/zh-cn/application-dev/media/audio-renderer.md new file mode 100644 index 00000000000..527e056a093 --- /dev/null +++ b/zh-cn/application-dev/media/audio-renderer.md @@ -0,0 +1,291 @@ +# AudioRenderer音频播放开发指南 + +--- + +## ***注意***: + + 1. 本文档适用于JavaScript。 + 2. 已经提出了对AudioRenderer接口的更改。 + 当更新的api接口被集成后,文档将被修改,并且应用程序必须与之适配。 + +--- + +## **摘要** + +本指南将向您展示如何使用AudioRenderer创建音频播放器应用程序。 +您可以使用本文档中提供的API在输出设备中播放音频文件,并管理播放任务。 + +## **音频播放框架** + +AudioRenderer接口是音频框架中最重要的组件之一。 + +### **音频播放:** + +AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 + +### **音频中断:** + +当较高优先级的流想要播放时,AudioRenderer 框架会中断较低优先级的流。 +例如,如果您在听音乐时有来电,则音乐播放作为较低优先级的流将被暂停。 +通过下面的示例代码,我们将详细了解 AudioInterrupt 的工作原理。 +
+请参阅 [**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 获取支持的音频流类型和格式的列表,例如 AudioSampleFormat、AudioChannel、AudioSampleFormat 和 AudioEncodingType。 + + +## **使用步骤** + +- 下面是一个如何使用AudioRenderer播放原始音频文件的示例。 + +1. 使用 **createAudioRenderer** 为 **AudioVolumeType** 创建一个AudioRenderer实例。 + 该对象可用于播放、控制和获取播放状态,以及接收回调通知。 + + ``` + const volType = audio.AudioVolumeType.MEDIA; // For music + const audioRenderer = audio.createAudioRenderer(volType); + ``` + +2. 使用 **on** 方法订阅音频中断事件。 + 当优先级更高或相等的 Stream-B 请求激活并使用输出设备时,Stream-A 被中断。 + 在某些情况下,框架会采取暂停和闪避(降低音量)等强制操作,并使用 **InterruptEvent** 通知应用程序。 在其他情况下,应用程序可以自行选择对 **InterruptEvent** 进行操作或忽略它。 当应用程序被强制操作中断时,它应该处理状态、更新用户界面等。 + + 在音频中断的情况下,应用程序可能会遇到写入失败,中断无法感知到这类情况。在写入音频数据之前可以使用 **audioRenderer.state** 方法检查渲染器状态,而中断可通过此监听获取到更多详细信息。 + + 中断事件信息: + + 1) **eventType:**中断是开始还是结束。 + + | 名称 | 描述 | + | -------------------- | ------------------ | + | INTERRUPT_TYPE_BEGIN | 表示中断已经开始。 | + | INTERRUPT_TYPE_END | 表示中断已经结束。 | + + 2) **forceType:**框架是否已经采取行动,或者是否建议应用程序采取行动。 + + | 名称 | 描述 | + | :-------------- | :------------------------- | + | INTERRUPT_FORCE | 音频状态已被框架更改。 | + | INTERRUPT_SHARE | 应用程序是否响应中断事件。 | + + 3) **hintType:**中断事件类型。 + + | 名称 | 描述 | + | :-------------------- | :----------------- | + | INTERRUPT_HINT_PAUSE | 暂停播放。 | + | INTERRUPT_HINT_RESUME | 恢复播放。 | + | INTERRUPT_HINT_STOP | 停止播放。 | + | INTERRUPT_HINT_DUCK | 降低播放流的音量。 | + | INTERRUPT_HINT_UNDUCK | 恢复播放流的音量。 | + + 4) **某些操作是完全强制或共享的**,这意味着它们由框架或应用程序执行。 + 例如,当音乐流正在进行时收到呼叫时,框架会强制音乐流暂停。调用结束后框架不会强制恢复音乐流.相反,它会提醒应用程序恢复播放。 + + | 名称 | 描述 | + | --------------------- | -------------------------------------------------------- | + | INTERRUPT_HINT_RESUME | forceType类型为:INTERRUPT_SHARE。它只能由应用程序完成。 | + | INTERRUPT_HINT_DUCK | forceType类型为:INTERRUPT_FORCE。它只能由框架来完成。 | + | INTERRUPT_HINT_UNDUCK | forceType类型为:INTERRUPT_FORCE。它只能由框架来完成。 | + + ``` + audioRenderer.on('interrupt', (interruptEvent) => { + console.info('InterruptEvent Received'); + console.info('InterruptType: ' + interruptEvent.eventType); + console.info('InterruptForceType: ' + interruptEvent.forceType); + console.info('AInterruptHint: ' + interruptEvent.hintType); + if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { + switch (interruptEvent.hintType) { + // 强制暂停:由框架采取行动。 + // 停止写入以避免数据丢失。 + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + isPlay = false; + break; + // 强制停止:由框架采取行动。 + // 停止写入以避免数据丢失。 + case audio.InterruptHint.INTERRUPT_HINT_STOP: + isPlay = false; + break; + // 降低音量: 由框架采取行动。 + // 仅需通知应用程序音量已减小。 + case audio.InterruptHint.INTERRUPT_HINT_DUCK: + break; + // 恢复音量: 由框架采取行动。 + // 仅需通知应用程序音量已恢复。 + case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: + break; + } + } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { + switch (interruptEvent.hintType) { + // 恢复:由应用采取行动。 + // 在需要的情况下,恢复强制暂停流。 + case audio.InterruptHint.INTERRUPT_HINT_RESUME: + startRenderer(); + break; + // 暂停:流已被中断。 + // 可以选择暂停或同时播放。 + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + isPlay = false; + pauseRenderer(); + break; + } + } + }); + ``` + +3. 准备播放器。在实例上调用**SetParams**。需要根据音频播放规范设置音频播放器参数。 + + ``` + async function prepareRenderer() { + // file_example_WAV_2MG.wav + var audioParams = { + format: audio.AudioSampleFormat.SAMPLE_S16LE, + channels: audio.AudioChannel.STEREO, + samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, + encoding: audio.AudioEncodingType.ENCODING_PCM, + }; + + let response = await audioRenderer.setParams(audioParams); + var state = audioRenderer.state; + if (state != audio.AudioState.STATE_PREPARED) { + console.info('Prepare renderer failed'); + return; + } + } + ``` + +4. 在AudioRenderer 实例调用 **start()** 函数来启动/恢复播放任务。 + 启动完成后,当音频播放器状态将为 STATE _RUNNING时,可以开始将数据写入缓冲区。 + + ``` + async function startRenderer() { + var state = audioRenderer.state; + // 状态应该是准备、暂停或停止。 + if (state != audio.AudioState.STATE_PREPARED || state != audio.AudioState.STATE_PAUSED || + state != audio.AudioState.STATE_STOPPED) { + console.info('Renderer is not in a correct state to start'); + return; + } + var started = await audioRenderer.start(); + if (started) { + isPlay = true; + console.info('Renderer started'); + } else { + console.error('Renderer start failed'); + return; + } + } + + ``` + +5. 调用 **write** 开始向缓冲区写入数据。 + 将要播放的音频数据读入缓冲区。 反复调用write方法写入数据。 + + ``` + async function writeBuffer(buf) { + var state = audioRenderer.state; + if (state != audio.AudioState.STATE_RUNNING) { + console.error('Renderer is not running, do not write'); + isPlay = false; + return; + } + let writtenbytes = await audioRenderer.write(buf); + + console.info('Actual written bytes: ' + writtenbytes); + if (writtenbytes < 0) { + console.error('Write buffer failed. check the state of renderer'); + } + } + + // 音频播放器合理值是最小缓冲区大小。其他大小也可接受。 + const bufferSize = await audioRenderer.getBufferSize(); + const path = '/data/file_example_WAV_2MG.wav'; + let ss = fileio.createStreamSync(path, 'r'); + const totalSize = 2146166; // file_example_WAV_2MG.wav + let rlen = 0; + let discardHeader = new ArrayBuffer(44); + ss.readSync(discardHeader); + rlen += 44; + + var id = setInterval(() => { + if (isPlay || isRelease) { + if (rlen >= totalSize || isRelease) { + ss.closeSync(); + stopRenderer(); + clearInterval(id); + } + let buf = new ArrayBuffer(bufferSize); + rlen += ss.readSync(buf); + console.info('Total bytes read from file: ' + rlen); + writeBuffer(buf); + } else { + console.info('check after next interval'); + } + } , 30); //根据音频文件格式设置间隔。 + ``` + +6. (可选)在 AudioRenderer 实例上调用 **pause()** 或 **stop()**。 + + ``` + async function pauseRenderer() { + var state = audioRenderer.state; + if (state != audio.AudioState.STATE_RUNNING) { + console.info('Renderer is not running'); + return; + } + var paused = await audioRenderer.pause(); + if (paused) { + console.info('Renderer paused'); + } else { + console.error('Renderer pause failed'); + } + } + + async function stopRenderer() { + var state = audioRenderer.state; + if (state != audio.AudioState.STATE_RUNNING || state != audio.AudioState.STATE_PAUSED) { + console.info('Renderer is not running or paused'); + return; + } + var stopped = await audioRenderer.stop(); + if (stopped) { + console.info('Renderer stopped'); + } else { + console.error('Renderer stop failed'); + } + } + ``` + +7. 播放任务完成后,调用AudioRenderer实例上的**release()**函数释放资源。 + AudioRenderer 可以使用大量的系统资源。因此,只要不再需要资源,就必须释放它们。为确保分配给它的任何系统资源都得到适当释放, 您应该始终调用**release()**。 + + ``` + async function releaseRenderer() { + if (state_ == RELEASED || state_ == NEW) { + console.info('Resourced already released'); + return; + } + var released = await audioRenderer.release(); + if (released) { + console.info('Renderer released'); + } else { + console.info('Renderer release failed'); + } + + } + ``` + +## **状态检查的重要性:** + +您还应该记住,AudioRenderer 是基于状态的。 +也就是说,AudioRenderer 有一个内部状态,在调用播放控制 API 时必须始终检查它,因为某些操作仅在音频播放器处于给定状态时才可接受。 +如果您在不正确的状态下执行操作,系统可能会抛出错误/异常或生成其他未定义的行为。 +在每个必要的操作之前,示例代码都会执行状态检查。 + +## **异步操作:** + +大多数AudioRenderer调用都是异步的。因此,UI线程不会被阻塞。 +对于每个API,框架都提供callback函数和promise函数。 +在本例中,为了简单起见,使用了promise函数。[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) +为回调和承诺提供参考。 + +## **其他 APIs 接口:** + +详见[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md)获取更有用的API,如getAudioTime、drain和getBufferSize。 \ No newline at end of file -- Gitee From eecef86d218a75c3b22f5f29a37e57f9e9c016e5 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Mon, 21 Feb 2022 17:46:00 +0800 Subject: [PATCH 2/9] add md --- .../media/system-sound-manager.md | 169 ++ .../reference/apis/js-apis-audio.md | 2006 ++++++++++++++++- 2 files changed, 2080 insertions(+), 95 deletions(-) create mode 100644 zh-cn/application-dev/media/system-sound-manager.md diff --git a/zh-cn/application-dev/media/system-sound-manager.md b/zh-cn/application-dev/media/system-sound-manager.md new file mode 100644 index 00000000000..113f6098f41 --- /dev/null +++ b/zh-cn/application-dev/media/system-sound-manager.md @@ -0,0 +1,169 @@ +# SoundManager开发指南 + +## **摘要** +本指南将展示如何使用SoundManager保存和检索铃声、告警和通知URI。 +另外文档还解释了如何使用铃声播放器API来播放铃声uri。 + +## **SoundManager框架** +SoundManager 接口由用于保存和检索铃声、告警和通知 uris 的接口 组成。 +SoundManager 还提供了铃声播放所需的其他接口。所有 uris 都将保存在设备的持久内存中。 + +请参考[**SystemSoundManager**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md##systemsoundmanager)的api支持的列表。 + + +## **使用**步骤 +- 下面是一个如何使用SoundManager保存和检索以及告警uri的例子 +1. 使用**getSystemSoundManager**获取一个SoundManager实例 + 此对象用于设置和获取告警的uri。 + ``` + const systemSoundManager = audio.getSystemSoundManager(); + ``` + +2. 使用**setSystemAlarmUri**设置系统告警uri + 应用程序应在使用此 API 时提供应用程序的上下文信息。 + + ``` + systemSoundManager.setSystemAlarmUri(context, '/data/media/alarm.wav', (err)=> { + if (err) { + console.error('Failed to setSystemAlarmUri'); + } else { + console.log('Callback invoked to indicate a successful system alarm uri setting.'); + } + }) + ``` + +3. 使用 **getSystemAlarmUri** 获取系统警报 uri + 应用程序应在使用此 API 时提供应用程序上下文信息。 + + ``` + systemSoundManager.getSystemAlarmUri(context, (err, alarmUri)=>{ + if (err) { + console.err('getSystemAlarmUri failed'); + } else { + console.log('getSystemAlarmUri success: ' + alarmUri); + } + }) + ``` + + +- 这是另一个如何使用 SoundManager 进行铃声 uri 播放的示例。 +1. 使用 **getSystemSoundManager**获取SoundManager实例。 + ``` + const systemSoundManager = audio.getSystemSoundManager(); + ``` + +2. 使用**setSystemRingtoneUri**设置系统铃声uri + 使用API时需要提供应用程序的上下文信息、文件uri以及铃声类型 [*RingtoneType*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#ringtonetype8)。 + + ``` + systemSoundManager.setSystemRingtoneUri(context, '/data/media/ringtone_sim1.wav', + audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err)=> { + if (err) { + console.error('Failed to setSystemRingtoneUri'); + } else { + console.log('Callback invoked to indicate a successful system ringtone uri setting.'); + } + }) + ``` + +3. 使用 **getSystemRingtonePlayer**获取铃声播放器实例\ + 使用API时需要提供应用程序的上下文信息、铃声类型[*RingtoneType*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#RingtoneType8)。 + + ``` + systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + } else { + console.log('getSystemRingtonePlayer success: '); + } + }); + ``` + +4. 使用**configure**配置铃声播放器\ + 使用API时需要提供铃声参数[*RingtoneOptions*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#ringtoneoptions8)。 + + ``` + let ringtoneOptions = { + volume: 1, + loop: false + }; + + ringtonePlayer.configure(ringtoneOptions, (err)=> { + if (err) { + console.error('Failed to configure ringtone options'); + } else { + console.log('Callback invoked to indicate a successful ringtone options configuration.'); + } + }); + ``` + +5. 使用**start**开始播放铃声\ + *注意*:如果在configure中*loop*为真,铃声将继续播放,直到其停止显式。 + ``` + ringtonePlayer.start((err)=> { + if (err) { + console.error('Failed to start playing ringtone'); + } else { + console.log('Ringtone start playing successfully.'); + } + }); + ``` + +6. 使用**stop**来停止正在播放的铃声。 + ``` + ringtonePlayer.stop((err)=> { + if (err) { + console.error('Failed to stop playing ringtone'); + } else { + console.log('Ringtone stop playing successfully.'); + } + }); + ``` + +7. 使用**release**释放铃声播放器资源。 + ``` + ringtonePlayer.release((err)=> { + if (err) { + console.error('Failed to release ringtone player resource'); + } else { + console.log('Release ringtone player resource successfully.'); + } + }); + ``` + +## **其他APIs** +ringtonePlayer对象提供了更多的api,比如; + +1. **state** + 只读变量,包含了铃声播放器当前所有的状态。 + + ``` + let playerState = ringtonePlayer.state + ``` +``` + +2. **getTitle** + 从文件元数据返回铃声uri的路径信息。 + +``` + ringtonePlayer.getTitle((err, title)=>{ + if (err) { + console.err('getTitle failed'); + } else { + console.log('getTitle success: ' + title); + } + }); + ``` + +3. **getAudioRendererInfo** + 获取音频渲染器信息。 + + ``` + ringtonePlayer.getAudioRendererInfo((err, rendererInfo)=>{ + if (err) { + console.err('getAudioRendererInfo failed'); + } else { + console.log('getAudioRendererInfo success'); + } + }); + ``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-audio.md b/zh-cn/application-dev/reference/apis/js-apis-audio.md index 4369e06d571..0a520ca4c56 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-audio.md +++ b/zh-cn/application-dev/reference/apis/js-apis-audio.md @@ -1,19 +1,25 @@ # 音频管理 +该模块提供以下功能:音频管理、音频播放、系统声音管理。 + ## 导入模块 ``` import audio from '@ohos.multimedia.audio'; ``` +## 权限 + +无 -## getAudioManager +## audioManager getAudioManager(): AudioManager 获取音频管理器。 **返回值:** + | 类型 | 说明 | | -------- | -------- | | [AudioManager](#audiomanager) | 音频管理类。 | @@ -23,6 +29,69 @@ getAudioManager(): AudioManager var audioManager = audio.getAudioManager(); ``` +## audioRenderer + +createAudioRenderer(volumeType: AudioVolumeType): AudioRenderer + +获取音频播放器。 + +**参数**: + +| 参数名 | 类型 | 必填 | 说明 | +| ---------- | --------------- | ---- | ----------- | +| volumeType | AudioVolumeType | 是 | 音频流类型. | + +**返回值**: + +| 类型 | 说明 | +| ------------- | ---------------- | +| AudioRenderer | 音频播放器对象。 | + +**示例:** + +``` +var audioStreamInfo = { + samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, + channels: audio.AudioChannel.CHANNEL_1, + sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, + encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW +} + +var audioRendererInfo = { + content: audio.ContentType.CONTENT_TYPE_SPEECH, + usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, + rendererFlags: 1 +} + +var audioRendererOptions = { + streamInfo: audioStreamInfo, + rendererInfo: audioRendererInfo +} + +let audioRenderer = await audio.createAudioRenderer(audioRendererOptions); +``` + +## systemSoundManager + +getSystemSoundManager(): SystemSoundManager + +获取 **SystemSoundManager**实例对象。 + +**参数** + +无 + +**返回值** + +| Type | Description | +| ------------------ | -------------- | +| SystemSoundManager | 系统声音管理器 | + +**示例:** + +``` +const systemSoundManager = audio.getSystemSoundManager(); +``` ## AudioVolumeType @@ -62,23 +131,13 @@ var audioManager = audio.getAudioManager(); | 名称 | 默认值 | 描述 | | -------------- | ------ | ------------------------------------------------------- | | INVALID | 0 | 无效设备。 | -| EARPIECE | 1 | 听筒。 | | SPEAKER | 2 | 扬声器。 | | WIRED_HEADSET | 3 | 有线耳机。 | | BLUETOOTH_SCO | 7 | 蓝牙设备SCO连接(Synchronous Connection Oriented)。 | | BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP连接(Advanced Audio Distribution Profile)。 | | MIC | 15 | 麦克风。 | -## ActiveDeviceType7+ - -枚举,活跃设备类型。 - -| 名称 | 默认值 | 描述 | -| ------------- | ------ | -------------------------------------------------- | -| SPEAKER | 2 | 扬声器。 | -| BLUETOOTH_SCO | 7 | 蓝牙设备SCO连接(Synchronous Connection Oriented)。 | - -## AudioRingMode7+ +## AudioRingMode7+ 枚举,铃声模式。 @@ -88,12 +147,207 @@ var audioManager = audio.getAudioManager(); | RINGER_MODE_VIBRATE | 1 | 震动模式 | | RINGER_MODE_NORMAL | 2 | 响铃模式 | +## AudioSampleFormat8+ + +枚举,音频采样格式。 + +| 名称 | 默认值 | 描述 | +| ------------- | ------ | ------------------------ | +| INVALID_WIDTH | -1 | 无效格式。 | +| SAMPLE_U8 | 1 | 无符号8位整数。 | +| SAMPLE_S16LE | 0 | 带符号的16位整数,小尾数 | +| SAMPLE_S24LE | 1 | 带符号的24位整数,小尾数 | +| SAMPLE_S32LE | 2 | 带符号的24位整数,小尾数 | + +## AudioChannel8+ + +枚举, 音频声道。 + +| 名称 | 默认值 | 描述 | +| --------- | -------- | -------- | +| CHANNEL_1 | 0x1 << 0 | 单声道。 | +| CHANNEL_2 | 0x1 << 1 | 双声道。 | + +## AudioSamplingRate8+ + +枚举,音频采样率。 + +| 名称 | 默认值 | 描述 | +| ----------------- | ------ | -------------- | +| 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+ + +枚举,音频编码类型。 + +| 名称 | 默认值 | 描述 | +| --------------------- | ------ | ---------- | +| ENCODING_TYPE_INVALID | -1 | 无效类型。 | +| ENCODING_TYPE_RAW | 0 | RAW类型。 | + +## ContentType8+ + +枚举,媒体类型。 + +| 名称 | 默认值 | 描述 | +| ------------------------- | ------ | ---------------- | +| CONTENT_TYPE_UNKNOWN | 0 | 表示类型为未知。 | +| CONTENT_TYPE_SPEECH | 1 | 表示类型为语音。 | +| CONTENT_TYPE_MUSIC | 2 | 表示类型为音乐。 | +| CONTENT_TYPE_MOVIE | 3 | 表示类型为电影。 | +| CONTENT_TYPE_SONIFICATION | 4 | 表示类型为加密。 | +| CONTENT_TYPE_RINGTONE | 5 | 表示类型为铃声。 | + +## StreamUsage8+ + +枚举,音频流使用类型。 + +| 名称 | 默认值 | 描述 | +| ---------------------------------- | ------ | -------------- | +| STREAM_USAGE_UNKNOWN | 0 | 表示未知。 | +| STREAM_USAGE_MEDIA | 1 | 表示音频。 | +| STREAM_USAGE_VOICE_COMMUNICATION | 2 | 表示语音通信。 | +| STREAM_USAGE_NOTIFICATION_RINGTONE | 3 | 表示通知铃声。 | + +## AudioState8+ + +枚举,音频状态。 + +| 名称 | 默认值 | 描述 | +| -------------- | ------ | ---------------- | +| STATE_INVALID | -1 | 无效状态。 | +| STATE_NEW | 0 | 创建新实例状态。 | +| STATE_PREPARED | 1 | 准备状态。 | +| STATE_RUNNING | 2 | 可运行状态。 | +| STATE_STOPPED | 3 | 停止状态。 | +| STATE_RELEASED | 4 | 释放状态。 | +| STATE_PAUSED | 5 | 暂停状态。 | + +## AudioRendererRate8+ + +枚举,渲染速度。 + +| 名称 | 默认值 | 描述 | +| ------------------ | ------ | ----------- | +| RENDER_RATE_NORMAL | 0 | 正常速度。 | +| RENDER_RATE_DOUBLE | 1 | 双倍速度。 | +| RENDER_RATE_HALF | 2 | 1/2的速度。 | + +## InterruptType8+ + +枚举,中断类型。 + +| 名称 | 默认值 | 描述 | +| -------------------- | ------ | ------------------ | +| INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断开始。 | +| INTERRUPT_TYPE_END | 2 | 音频播放中断结束。 | + +## InterruptForceType8+ + +枚举,强制打断类型。 + +| 名称 | 默认值 | 描述 | +| --------------- | ------ | ------------------------- | +| INTERRUPT_FORCE | 0 | 由系统采取强制操作。 | +| INTERRUPT_SHARE | 1 | App可以选择采取行动或忽略 | + +## InterruptHint8+ + + 枚举,中断提示。 + +| 名称 | 默认值 | 描述 | +| --------------------- | ------ | ---------------------------------------- | +| INTERRUPT_HINT_NONE | 0 | 无。 | +| INTERRUPT_HINT_RESUME | 1 | 恢复播放。 | +| INTERRUPT_HINT_PAUSE | 2 | 暂停/被暂停播放。 | +| INTERRUPT_HINT_STOP | 3 | 停止/被停止播放。 | +| INTERRUPT_HINT_DUCK | 4 | 音频躲避。(躲避:音量减弱,而不会停止) | +| INTERRUPT_HINT_UNDUCK | 5 | 恢复音量。 | -## AudioManager +## RingtoneType8+ + +枚举,铃声类型。 + +| 名称 | 默认值 | 描述 | +| ---------------------- | ------ | ------------------- | +| RINGTONE_TYPE_DEFAULT | 0 | 表示默认类型。 | +| RINGTONE_TYPE_MULTISIM | 1 | 表示Multi-sim类型。 | + +## AudioStreamInfo8+ + +描述音频流信息。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------------ | ----------------------------------------- | ---- | -------------------------- | +| samplingRate | [AudioSamplingRate](#AudioRendererRate8+) | 是 | 表示播放音频文件的采样率。 | +| channels | [AudioChannel](#AudioChannel8+) | 是 | 表示播放音频文件的通道数。 | +| sampleFormat | [AudioSampleFormat](#AudioSampleFormat8+) | 是 | 表示音频采样格式。 | +| encodingType | [AudioEncodingType](#AudioEncodingType8+) | 是 | 表示音频编码格式。 | + +## AudioRendererInfo8+ + +描述音频渲染器信息。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------------- | ----------------------------- | ---- | -------------------- | +| contentType | [ContentType](#ContentType8+) | 是 | 表示媒体类型。 | +| usage | [StreamUsage](#StreamUsage) | 是 | 表示音频流使用类型。 | +| rendererFlags | number | 是 | 表示音频渲染器标志。 | + +## InterruptEvent8+ + +描述播放中断时应用程序接收的中断事件。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| --------- | ------------------------------------------- | ---- | ------------------------------------------ | +| eventType | [InterruptType](#InterruptType8+) | 是 | 表示指示中断是开始还是结束。 | +| forceType | [InterruptForceType](#InterruptForceType8+) | 是 | 表示指示操作是由系统执行还是由应用程序执行 | +| hintType | [InterruptHint](#InterruptHint8+) | 是 | 表示系统已采取或应用程序将采取的操作。 | + +## VolumeEvent8+ + +描述应用程序在音量发生更改时接收的事件。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ---------- | ----------------------------------- | ---- | -------------------------------------------------------- | +| volumeType | [AudioVolumeType](#AudioVolumeType) | 是 | 音量流类型。 | +| volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | +| updateUi | boolean | 是 | 在Ui中显示音量变化。 | + +## RingtoneOptions8+ + +描述铃声选项。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------- | ---- | -------------- | +| volume | number | 是 | 表示铃声大小。 | +| loop | boolean | 是 | 表示是否循环。 | + +# AudioManager 管理音频音量和音频设备。 -### setVolume +## audioManager.setVolume setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback<void>): void @@ -107,10 +361,13 @@ setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback&l | volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | | callback | AsyncCallback<void> | 是 | 回调表示成功还是失败。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err)=>{ if (err) { console.error('Failed to set the volume. ${err.message}'); @@ -120,7 +377,7 @@ audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10, (err)=>{ }) ``` -### setVolume +## audioManager.setVolume setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> @@ -142,13 +399,12 @@ setVolume(volumeType: AudioVolumeType, volume: number): Promise<void> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setVolume(audio.AudioVolumeType.MEDIA, 10).then(()=> console.log('Promise returned to indicate a successful volume setting.'); ) ``` -### getVolume +## audioManager.getVolume getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void @@ -161,10 +417,13 @@ getVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): v | volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | | callback | AsyncCallback<number> | 是 | 回调返回音量大小。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the volume. ${err.message}'); @@ -174,7 +433,7 @@ audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => { }) ``` -### getVolume +## audioManager.getVolume getVolume(volumeType: AudioVolumeType): Promise<number> @@ -195,13 +454,12 @@ getVolume(volumeType: AudioVolumeType): Promise<number> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getVolume(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the volume is obtained.' + value); ) ``` -### getMinVolume +## audioManager.getMinVolume getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void @@ -214,10 +472,13 @@ getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>) | volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | | callback | AsyncCallback<number> | 是 | 回调返回最小音量。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the minimum volume. ${err.message}'); @@ -227,7 +488,7 @@ audioManager.getMinVolume(audio.AudioVolumeType.MEDIA, (err, value) => { }) ``` -### getMinVolume +## audioManager.getMinVolume getMinVolume(volumeType: AudioVolumeType): Promise<number> @@ -248,13 +509,12 @@ getMinVolume(volumeType: AudioVolumeType): Promise<number> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getMinVolume(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promised returned to indicate that the minimum volume is obtained.' + value); ) ``` -### getMaxVolume +## audioManager.getMaxVolume getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>): void @@ -267,10 +527,13 @@ getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback<number>) | volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | | callback | AsyncCallback<number> | 是 | 回调返回最大音量大小。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the maximum volume. ${err.message}'); @@ -280,7 +543,7 @@ audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA, (err, value) => { }) ``` -### getMaxVolume +## audioManager.getMaxVolume getMaxVolume(volumeType: AudioVolumeType): Promise<number> @@ -301,13 +564,16 @@ getMaxVolume(volumeType: AudioVolumeType): Promise<number> **示例:** ``` -var audioManager = audio.getAudioManager(); -audioManager.getMaxVolume(audio.AudioVolumeType.MEDIA).then((data)=> - console.log('Promised returned to indicate that the maximum volume is obtained.'); -) +audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => { + if (err) { + console.error('Failed to mute the stream. ${err.message}'); + return; + } + console.log('Callback invoked to indicate that the stream is muted.'); +}) ``` -### mute7+ +## audioManager.mute mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void>): void @@ -321,20 +587,23 @@ mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback<void | mute | boolean | 是 | 静音状态,true为静音,false为非静音。 | | callback | AsyncCallback<void> | 是 | 回调表示成功还是失败。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); -audioManager.mute(audio.AudioVolumeType.MEDIA, true, (err) => { - if (err) { - console.error('Failed to mute the stream. ${err.message}'); - return; - } - console.log('Callback invoked to indicate that the stream is muted.'); +audioManager.getVolume(audio.AudioVolumeType.MEDIA, (err, value) => { + if (err) { + console.error('Failed to obtain the volume. ${err.message}'); + return; + } + console.log('Callback invoked to indicate that the volume is obtained.'); }) ``` -### mute7+ +## audioManager.mute mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> @@ -357,14 +626,13 @@ mute(volumeType: AudioVolumeType, mute: boolean): Promise<void> ``` -var audioManager = audio.getAudioManager(); audioManager.mute(audio.AudioVolumeType.MEDIA, true).then(() => console.log('Promise returned to indicate that the stream is muted.'); ) ``` -### isMute7+ +## audioManager.isMute isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void @@ -377,10 +645,13 @@ isMute(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): voi | volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | | callback | AsyncCallback<boolean> | 是 | 回调返回流静音状态,true为静音,false为非静音。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the mute status. ${err.message}'); @@ -391,7 +662,7 @@ audioManager.isMute(audio.AudioVolumeType.MEDIA, (err, value) => { ``` -### isMute7+ +## audioManager.isMute isMute(volumeType: AudioVolumeType): Promise<boolean> @@ -412,13 +683,12 @@ isMute(volumeType: AudioVolumeType): Promise<boolean> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isMute(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the mute status of the stream is obtained.' + value); ) ``` -### isActive7+ +## audioManager.isActive isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): void @@ -431,10 +701,13 @@ isActive(volumeType: AudioVolumeType, callback: AsyncCallback<boolean>): v | volumeType | [AudioVolumeType](#audiovolumetype) | 是 | 音量流类型。 | | callback | AsyncCallback<boolean> | 是 | 回调返回流的活跃状态,true为活跃,false为不活跃。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => { if (err) { console.error('Failed to obtain the active status of the stream. ${err.message}'); @@ -444,7 +717,7 @@ audioManager.isActive(audio.AudioVolumeType.MEDIA, (err, value) => { }) ``` -### isActive7+ +## audioManager.isActive isActive(volumeType: AudioVolumeType): Promise<boolean> @@ -465,13 +738,12 @@ isActive(volumeType: AudioVolumeType): Promise<boolean> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isActive(audio.AudioVolumeType.MEDIA).then((value) => console.log('Promise returned to indicate that the active status of the stream is obtained.' + value); ) ``` -### setRingerMode7+ +## audioManager.setRingerMode setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void @@ -481,13 +753,16 @@ setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | -| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | +| mode | [AudioRingMode](#audioringmode7+) | 是 | 音频铃声模式。 | | callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => { if (err) { console.error('Failed to set the ringer mode.​ ${err.message}'); @@ -497,7 +772,7 @@ audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL, (err) => { }) ``` -### setRingerMode7+ +## audioManager.setRingerMode setRingerMode(mode: AudioRingMode): Promise<void> @@ -507,7 +782,7 @@ setRingerMode(mode: AudioRingMode): Promise<void> | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | -| mode | [AudioRingMode](#audioringmode) | 是 | 音频铃声模式。 | +| mode | [AudioRingMode](#audioringmode7+) | 是 | 音频铃声模式。 | **返回值:** @@ -518,14 +793,13 @@ setRingerMode(mode: AudioRingMode): Promise<void> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setRingerMode(audio.AudioRingMode.RINGER_MODE_NORMAL).then(() => console.log('Promise returned to indicate a successful setting of the ringer mode.'); ) ``` -### getRingerMode7+ +## audioManager.getRingerMode getRingerMode(callback: AsyncCallback<AudioRingMode>): void @@ -535,12 +809,15 @@ getRingerMode(callback: AsyncCallback<AudioRingMode>): void | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | -| callback | AsyncCallback<[AudioRingMode](#audioringmode)> | 是 | 回调返回系统的铃声模式。 | +| callback | AsyncCallback<[AudioRingMode](#audioringmode7+)> | 是 | 回调返回系统的铃声模式。 | + +**返回值:** + +无 **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getRingerMode((err, value) => { if (err) { console.error('Failed to obtain the ringer mode.​ ${err.message}'); @@ -551,7 +828,7 @@ audioManager.getRingerMode((err, value) => { ``` -### getRingerMode7+ +## audioManager.getRingerMode getRingerMode(): Promise<AudioRingMode> @@ -561,18 +838,17 @@ getRingerMode(): Promise<AudioRingMode> | 类型 | 说明 | | -------- | -------- | -| Promise<[AudioRingMode](#audioringmode)> | Promise回调返回系统的铃声模式。 | +| Promise<[AudioRingMode](#audioringmode7+)> | Promise回调返回系统的铃声模式。 | **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getRingerMode().then((value) => console.log('Promise returned to indicate that the ringer mode is obtained.' + value); ) ``` -### setAudioParameter7+ +## audioManager.setAudioParameter setAudioParameter(key: string, value: string, callback: AsyncCallback<void>): void @@ -586,10 +862,13 @@ setAudioParameter(key: string, value: string, callback: AsyncCallback<void> | value | string | 是 | 被设置的音频参数的值。 | | callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => { if (err) { console.error('Failed to set the audio parameter. ${err.message}'); @@ -599,7 +878,7 @@ audioManager.setAudioParameter('PBits per sample', '8 bit', (err) => { }) ``` -### setAudioParameter7+ +## audioManager.setAudioParameter setAudioParameter(key: string, value: string): Promise<void> @@ -621,13 +900,12 @@ setAudioParameter(key: string, value: string): Promise<void> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setAudioParameter('PBits per sample', '8 bit').then(() => console.log('Promise returned to indicate a successful setting of the audio parameter.'); ) ``` -### getAudioParameter7+ +## audioManager.getAudioParameter getAudioParameter(key: string, callback: AsyncCallback<string>): void @@ -640,10 +918,13 @@ getAudioParameter(key: string, callback: AsyncCallback<string>): void | key | string | 是 | 待获取的音频参数的键。 | | callback | AsyncCallback<string> | 是 | 回调返回获取的音频参数的值。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getAudioParameter('PBits per sample', (err, value) => { if (err) { console.error('Failed to obtain the value of the audio parameter. ${err.message}'); @@ -653,7 +934,7 @@ audioManager.getAudioParameter('PBits per sample', (err, value) => { }) ``` -### getAudioParameter7+ +## audioManager.getAudioParameter getAudioParameter(key: string): Promise<string> @@ -674,13 +955,12 @@ getAudioParameter(key: string): Promise<string> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getAudioParameter('PBits per sample').then((value) => console.log('Promise returned to indicate that the value of the audio parameter is obtained.' + value); ) ``` -### getDevices +## audioManager.getDevices getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescriptors>): void @@ -693,9 +973,13 @@ getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback<AudioDeviceDescrip | deviceFlag | [DeviceFlag](#deviceflag) | 是 | 设备类型的flag。 | | callback | AsyncCallback<[AudioDeviceDescriptors](#audiodevicedescriptors)> | 是 | 回调,返回设备列表。 | +**返回值:** + +无 + **示例:** + ``` -var audioManager = audio.getAudioManager(); audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{ if (err) { console.error('Failed to obtain the device list. ${err.message}'); @@ -705,7 +989,7 @@ audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{ }) ``` -### getDevices +## audioManager.getDevices (deviceFlag: DeviceFlag): Promise<AudioDeviceDescriptors> @@ -726,13 +1010,12 @@ audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG, (err, value)=>{ **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data)=> console.log('Promise returned to indicate that the device list is obtained.'); ) ``` -### setDeviceActive7+ +## audioManager.setDeviceActive setDeviceActive(deviceType: DeviceType, active: boolean, callback: AsyncCallback<void>): void @@ -746,10 +1029,13 @@ setDeviceActive(deviceType: DeviceType, active: boolean, callback: AsyncCallback | active | boolean | 是 | 设备激活状态。 | | callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | +**返回值:** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true, (err)=> { if (err) { console.error('Failed to set the active status of the device. ${err.message}'); @@ -759,7 +1045,7 @@ audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true, (err)=> { }) ``` -### setDeviceActive7+ +## audioManager.setDeviceActive setDeviceActive(deviceType: DeviceType, active: boolean): Promise<void> @@ -782,13 +1068,12 @@ setDeviceActive(deviceType: DeviceType, active: boolean): Promise<void> ``` -var audioManager = audio.getAudioManager(); audioManager.setDeviceActive(audio.DeviceType.SPEAKER, true).then(()=> console.log('Promise returned to indicate that the device is set to the active status.'); ) ``` -### isDeviceActive7+ +## audioManager.isDeviceActive isDeviceActive(deviceType: DeviceType, callback: AsyncCallback<boolean>): void @@ -801,10 +1086,13 @@ isDeviceActive(deviceType: DeviceType, callback: AsyncCallback<boolean>): | deviceType | [DeviceType](#devicetype) | 是 | 音频设备类型。 | | callback | AsyncCallback<boolean> | 是 | 回调返回设备的激活状态。 | +**返回值** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isDeviceActive(audio.DeviceType.SPEAKER, (err, value) => { if (err) { console.error('Failed to obtain the active status of the device. ${err.message}'); @@ -815,7 +1103,7 @@ audioManager.isDeviceActive(audio.DeviceType.SPEAKER, (err, value) => { ``` -### isDeviceActive7+ +## audioManager.isDeviceActive isDeviceActive(deviceType: DeviceType): Promise<boolean> @@ -836,13 +1124,12 @@ isDeviceActive(deviceType: DeviceType): Promise<boolean> **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isDeviceActive(audio.DeviceType.SPEAKER).then((value) => console.log('Promise returned to indicate that the active status of the device is obtained.' + value); ) ``` -### setMicrophoneMute7+ +## audioManager.setMicrophoneMute setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void @@ -855,10 +1142,13 @@ setMicrophoneMute(mute: boolean, callback: AsyncCallback<void>): void | mute | boolean | 是 | 待设置的静音状态,true为静音,false为非静音。 | | callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | +**返回值** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.setMicrophoneMute(true, (err) => { if (err) { console.error('Failed to mute the microphone. ${err.message}'); @@ -868,7 +1158,7 @@ audioManager.setMicrophoneMute(true, (err) => { }) ``` -### setMicrophoneMute7+ +## audioManager.setMicrophoneMute setMicrophoneMute(mute: boolean): Promise<void> @@ -895,7 +1185,7 @@ audioManager.setMicrophoneMute(true).then(() => ) ``` -### isMicrophoneMute7+ +## audioManager.isMicrophoneMute isMicrophoneMute(callback: AsyncCallback<boolean>): void @@ -907,10 +1197,13 @@ isMicrophoneMute(callback: AsyncCallback<boolean>): void | -------- | -------- | -------- | -------- | | callback | AsyncCallback<boolean> | 是 | 回调返回系统麦克风静音状态,true为静音,false为非静音。 | +**返回值** + +无 + **示例:** ``` -var audioManager = audio.getAudioManager(); audioManager.isMicrophoneMute((err, value) => { if (err) { console.error('Failed to obtain the mute status of the microphone. ${err.message}'); @@ -920,7 +1213,7 @@ audioManager.isMicrophoneMute((err, value) => { }) ``` -### isMicrophoneMute7+ +## audioManager.isMicrophoneMute isMicrophoneMute(): Promise<boolean> @@ -936,25 +1229,1548 @@ isMicrophoneMute(): Promise<boolean> ``` -var audioManager = audio.getAudioManager(); audioManager.isMicrophoneMute().then((value) => console.log('Promise returned to indicate that the mute status of the microphone is obtained.', + value); ) ``` +## audioManager.on -## AudioDeviceDescriptor +on(type: 'volumeChange', callback: Callback): void8+ -描述音频设备。 + 监听系统音量更改事件, 使用Callback来获取音量更改事件。 + +**参数:** -| 名称 | 参数型 | 可读 | 可写 | 说明 | -| -------- | -------- | -------- | -------- | -------- | -| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 | -| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------------- | ---- | ------------------------ | +| type | string | 是 | 监听的音量改变事件类型。 | +| callback | Callback<[VolumeEvent](VolumeEvent8+)> | 是 | 音量改变事件回调方法。 | +**返回值:** + +无 + +**示例:** + +``` +audioManager.on('volumeChange', (volumeEvent) => { + console.log('VolumeType of stream: ' + volumeEvent.volumeType); + console.log('Volume level: ' + volumeEvent.volume); + console.log('Whether to updateUI: ' + volumeEvent.updateUi); +}) +``` + + + +## audioManager.on + +on(type: 'ringerModeChange', callback: Callback): void8+ + +监听铃声模式更改事件。使用callback来获得铃声模式更改。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------- | ---- | ------------------------------ | +| type | string | 是 | 要监听的事件的类型。 | +| callback | Callback<[AudioRingMode](#audioringmode7+)> | 是 | 用于获取更新的铃声模式的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +audioManager.on('ringerModeChange', (ringerMode) => { + console.log('Updated ringermode: ' + ringerMode); +}) +``` + +# AudioDeviceDescriptor + +描述音频设备。 ## AudioDeviceDescriptors | 名称 | 描述 | | -------- | -------- | | 设备属性数组 | AudioDeviceDescriptor的数组,只读。 | + +## audioDeviceDescriptor.deviceRole + +定义的设备角色( DeviceRole :只读)。 + +| 名称 | 参数型 | 可读 | 可写 | 说明 | +| :--------- | :------------------------ | :--- | :--- | ---------- | +| deviceRole | [DeviceRole](#devicerole) | 是 | 否 | 设备角色。 | + +## audioDeviceDescriptor.deviceType + +定义的设备类型( DeviceType:只读)。 + +| 名称 | 参数型 | 可读 | 可写 | 说明 | +| :--------- | :------------------------ | :--- | :--- | ---------- | +| deviceType | [DeviceType](#devicetype) | 是 | 否 | 设备类型。 | + +``` +function deviceProp(audioDeviceDescriptor, index, array) { + deviceRoleValue = audioDeviceDescriptor.deviceRole; + deviceTypeValue = audioDeviceDescriptor.deviceType; +} + +deviceRoleValue = null; +deviceTypeValue = null; +const promise = audioManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG); +promise.then(async function (audioDeviceDescriptors) { + console.info('getDevices OUTPUT_DEVICES_FLAG'); + audioDeviceDescriptors.forEach(deviceProp); + if (deviceTypeValue != null && deviceRoleValue != null){ + console.info('OUTPUT_DEVICES_FLAG : Pass'); + expect(true).assertTrue(); + } + else{ + console.error('OUTPUT_DEVICES_FLAG : fail'); + expect(false).assertTrue(); + } + }); + await promise; + done(); +}) +``` + +# AudioRenderer + +提供音频播放的API接口。 + +## audioRenderer.state + +readonly state: AudioState 8+ + +定义当前播放器的状态。 + +**参数:** + +| 名称 | 参数型 | 可读 | 可写 | 说明 | +| ----- | ---------- | ---- | ---- | ------------------ | +| state | AudioState | 是 | 否 | 音频播放器的状态。 | + +**示例:** + +``` +var state = audioRenderer.state; +``` + +## audioRenderer.getRendererInfo + +getRendererInfo(callback: AsyncCallback): void8+ + +获取当前被创建的音频播放器的信息,使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| :------- | :-------------------------------------------------------- | :--- | :------------------------- | +| callback | AsyncCallback<[AudioRendererInfo](#AudioRendererInfo8+)\> | 是 | 回调返回音频播放器的信息。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.getRendererInfo((err, rendererInfo)=>{ + console.log('Renderer GetRendererInfo:'); + console.log('Renderer content:' + rendererInfo.content); + console.log('Renderer usage:' + rendererInfo.usage); + console.log('Renderer flags:' + rendererInfo.rendererFlags); +}) +``` + + +## audioRenderer.getRendererInfo + +getParams(): Promise8+ + +获取当前被创建的音频播放器的信息,使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| -------------------------------------------------- | ------------------------------- | +| Promise<[AudioRendererInfo](#AudioRendererInfo8+)> | Promise用于返回音频播放器信息。 | + +**示例:** + +``` +let streamInfo = await audioRenderer.getStreamInfo(); +console.log('Renderer GetStreamInfo:'); +console.log('Renderer sampling rate:' + streamInfo.samplingRate); +console.log('Renderer channel:' + streamInfo.AudioChannel); +console.log('Renderer format:' + streamInfo.AudioSampleFormat); +console.log('Renderer encoding type:' + streamInfo.AudioEncodingType); +``` + +## audioRenderer.start + +start(callback: AsyncCallback): void8+ + +启动音频播放器。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 返回回调的结果。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.start((err)=>{ + if (err) { + console.error('Renderer start failed.'); + } else { + console.info('Renderer start success.'); + } +}) +``` + +## audioRenderer.start + +start(): Promise8+ + +启动音频播放器。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | promise方式返回异步结果。 | + +**示例:** + +``` +await audioRenderer.start(); +``` + +## audioRenderer.pause + +pause(callback: AsyncCallback): void8+ + +暂停音频播放器。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 返回回调的结果。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.pause((err)=>{ + if (err) { + console.error('Renderer pause failed'); + } else { + console.log('Renderer paused.'); + } +}) +``` + +## audioRenderer.pause + +pause(): Promise8+ + +暂停音频播放器。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | promise方式返回异步结果。 | + +**示例:** + +``` +await audioRenderer.pause(); +``` + +## audioRenderer.drain + +drain(callback: AsyncCallback): void8+ + +播放缓冲区是否已被耗尽。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 返回回调的结果。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.drain((err)=>{ + if (err) { + console.error('Renderer drain failed'); + } else { + console.log('Renderer drained.'); + } +}) +``` + + + +## audioRenderer.drain + +drain(): Promise8+ + +播放缓冲区是否已被耗尽。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | promise方式返回异步结果。 | + +**示例:** + +``` +await audioRenderer.drain(); +``` + +## audioRenderer.stop + +stop(callback: AsyncCallback): void8+ + +停止播放。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 返回回调的结果。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.stop((err)=>{ + if (err) { + console.error('Renderer stop failed'); + } else { + console.log('Renderer stopped.'); + } +}) +``` + +## audioRenderer.stop + +stop(): Promise8+ + +停止播放。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | promise方式返回异步结果。 | + +**示例:** + +``` +await audioRenderer.stop(); +``` + +## audioRenderer.release + +release(callback: AsyncCallback): void8+ + +释放音频播放器。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 返回回调的结果。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.release((err)=>{ + if (err) { + console.error('Renderer release failed'); + } else { + console.log('Renderer released.'); + } +}) +``` + +## audioRenderer.release + +release(): Promise8+ + +释放渲染器。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | promise方式返回异步结果。 | + +**示例:** + +``` +await audioRenderer.release(); +``` + +## audioRenderer.write + +write(buffer: ArrayBuffer, callback: AsyncCallback): void8+ + +写入缓冲区。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------- | ---- | --------------------------------------------------- | +| buffer | ArrayBuffer | 是 | 要写入缓冲区的数据。 | +| callback | AsyncCallback | 是 | 回调如果成功,返回写入的字节数,否则返回errorcode。 | + +**返回值:** + +无 + +**示例:** + +``` +let ss = fileio.createStreamSync(filePath, 'r'); +let buf = new ArrayBuffer(bufferSize); +ss.readSync(buf); +audioRenderer.write(buf, (err, writtenbytes)=>{ + if (writtenbytes < 0) { + console.error('write failed.'); + } else { + console.log('Actual written bytes: ' + writtenbytes); + } +}) +``` + +## audioRenderer.write + +write(buffer: ArrayBuffer): Promise8+ + +写入缓冲区。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | ---------------------------------------------------------- | +| Promise | promise回调如果成功,返回写入的字节数,否则返回errorcode。 | + +**Example** + +``` +let ss = fileio.createStreamSync(filePath, 'r'); +let buf = new ArrayBuffer(bufferSize); +ss.readSync(buf); +let writtenbytes = await audioRenderer.write(buf); +if (writtenbytes < 0) { + console.error('write failed.'); +} else { + console.log('Actual written bytes: ' + writtenbytes); +} +``` + +## audioRenderer.getAudioTime + +getAudioTime(callback: AsyncCallback): void8+ + +获取时间戳。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | ---------------- | +| callback | AsyncCallback | 是 | 回调返回时间戳。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.getAudioTime((err, timestamp)=>{ + console.log('Current timestamp: ' + timestamp); +}) +``` + +## audioRenderer.getAudioTime + +getAudioTime(): Promise8+ + +获取时间戳。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 描述 | +| --------------- | ----------------------- | +| Promise | Promise回调返回时间戳。 | + +**示例:** + +``` +let timestamp = await audioRenderer.getAudioTime(); +console.log('Current timestamp: ' + timestamp); +``` + +## audioRenderer.getBufferSize + +getBufferSize(callback: AsyncCallback): void8+ + +获取音频播放器的最小缓冲区大小。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | -------------------- | +| callback | AsyncCallback | 是 | 回调返回缓冲区大小。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.getBufferSize((err, bufferSize)=>{ + if (err) { + console.error('getBufferSize error'); + } +}) +let buf = new ArrayBuffer(bufferSize); +ss.readSync(buf); +``` + +## audioRenderer.getBufferSize + +getBufferSize(): Promise8+ + +获取音频播放器的最小缓冲区大小。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | --------------------------- | +| Promise | promise回调返回缓冲区大小。 | + +**示例:** + +``` +var bufferSize = await audioRenderer.getBufferSize(); +let buf = new ArrayBuffer(bufferSize); +ss.readSync(buf); +``` + +## audioRenderer.setRenderRate + +setRenderRate(rate: AudioRendererRate, callback: AsyncCallback): voidvoid8+ + +设置音频播放速率。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------- | ---- | ------------------------ | +| rate | [AudioRendererRate](#AudioRendererRate8+) | 是 | 播放的速率。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL, (err)=> { + if (err) { + console.error('Failed to set params'); + } else { + console.log('Callback invoked to indicate a successful render rate setting.'); + } +}) +``` + +## audioRenderer.setRenderRate + +setRenderRate(rate: AudioRendererRate): Promise8+ + +设置音频播放速率。使用promise方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ----------------------------------------- | ---- | ------------ | +| rate | [AudioRendererRate](#AudioRendererRate8+) | 是 | 播放的速率。 | + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise用于返回执行结果。 | + +**示例:** + +``` +await audioRenderer.setRenderRate(audio.AudioRendererRate.RENDER_RATE_NORMAL); +``` + +## audioRenderer.getRenderRate + +getRenderRate(callback: AsyncCallback): void8+ + +获取当前播放速率。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------------------------------- | ---- | ------------------ | +| callback | AsyncCallback<[AudioRendererRate](#AudioRendererRate8+)> | 是 | 回调返回播放速率。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.getRenderRate((err, renderrate)=>{ + console.log('getRenderRate: ' + renderrate); +}) +``` + +## audioRenderer.getRenderRate + +getRenderRate(): Promise8+ + +获取当前播放速率。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| -------------------------------------------------- | ------------------------- | +| Promise<[AudioRendererRate](#AudioRendererRate8+)> | Promise回调返回播放速率。 | + +**示例:** + +``` +let renderRate = await audioRenderer.getRenderRate(); +console.log('getRenderRate: ' + renderrate); +``` + +## audioRenderer.on + +on(type: 'interrupt', callback: Callback): void8+ + +监听音频中断事件。使用callback获取中断事件。中断事件被触发,音频播放被中断。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------------------- | ---- | ------------------------ | +| type | string | 是 | 被监听的播放事件的类型。 | +| callback | Callback<[InterruptEvent](#InterruptEvent8+)> | 是 | 被监听的中断事件的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +audioRenderer.on('interrupt', (interruptEvent) => { + if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { + switch (interruptEvent.hintType) { + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + console.log('Force paused. Stop writing'); + isPlay = false; + break; + case audio.InterruptHint.INTERRUPT_HINT_STOP: + console.log('Force stopped. Stop writing'); + isPlay = false; + break; + } + } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { + switch (interruptEvent.hintType) { + case audio.InterruptHint.INTERRUPT_HINT_RESUME: + console.log('Resume force paused renderer or ignore'); + startRenderer(); + break; + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + console.log('Choose to pause or ignore'); + pauseRenderer(); + break; + } + } +}) +``` + +# SystemSoundManager + +## systemSoundManager.setSystemRingtoneUri + +setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType, callback: AsyncCallback): void8+ + +设置系统铃声。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------- | ---- | ------------------------ | +| context | Context | 是 | 当前的应用程序上下文。 | +| uri | string | 是 | 待设置的铃声uri。 | +| type | [RingtoneType](#RingtoneType8+) | 是 | 待设置的铃声类型。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtonetone.wav', + audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err)=> { + if (err) { + console.error('Failed to setSystemRingtoneUri'); + } else { + console.log('Callback invoked to indicate a successful system ringtone URI setting.'); + } +}) +``` + +## systemSoundManager.setSystemRingtoneUri + +setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType): Promise8+ + +设置系统铃声。使用promise方式返回异步结果。 + +**参数** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------- | ---- | ---------------------- | +| context | Context | 是 | 当前的应用程序上下文。 | +| uri | string | 是 | 待设置的铃声uri。 | +| type | [RingtoneType](#RingtoneType8+) | 是 | 待设置的铃声类型。 | + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise用于返回执行结果。 | + +**示例:** + +``` +await systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtone.wav', audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +``` + +## systemSoundManager.getSystemRingtoneUri + +getSystemRingtoneUri(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ + +获取铃声uri。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------- | ---- | ------------------------ | +| context | Context | 是 | 当前的应用程序上下文。 | +| type | [RingtoneType](#RingtoneType8+) | 是 | 获取铃声类型。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtoneUri)=>{ + if (err) { + console.err('getSystemRingtoneUri failed'); + } else { + console.log('getSystemRingtoneUri success: ' + ringtoneUri); + } +}) +``` + + + +## systemSoundManager.getSystemRingtoneUri + +getSystemRingtoneUri(context: Context, type: RingtoneType): Promise8+ + +获取铃声uri。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | ---------------------- | +| Promise | Promise返回铃声的uri。 | + +**示例:** + +``` +let ringtoneUri = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +if (ringtoneUri == '/data/media/Believer60s.wav') { + console.log('getSystemRingtoneUri success: ' + uri); +} else { + console.log('getSystemRingtoneUri fail: ' + uri); +} +``` + + + +## systemSoundManager.getSystemRingtonePlayer + +getSystemRingtonePlayer(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ + +获取铃声播放器。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------ | ---- | ------------------------ | +| context | Context | 是 | 当前应用程序上下文。 | +| type | RingtoneType | 是 | 获取铃声类型。 | +| callback | AsyncCallback) | 是 | 回调用于获取铃声播放器。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + } else { + console.log('getSystemRingtonePlayer success: '); + } +}) +``` + + + +## systemSoundManager.getSystemRingtonePlayer + +getSystemRingtonePlayer(context: Context, type: RingtoneType): Promise8+ + +获取铃声播放器。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ----------------------- | ------------------------------- | +| Promise | Promise回调用于获取铃声播放器。 | + +**示例:** + +``` +let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +``` + + + +## systemSoundManager.setSystemNotificationUri + +setSystemNotificationUri(context: Context, uri: string, callback: AsyncCallback): void8+ + +设置的系统通知uri。使用callback方式返回异步结果。 + +**参数** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ------------------------ | +| context | Context | Yes | 当前应用程序上下文。 | +| uri | string | Yes | 设置的系统通知uri。 | +| callback | AsyncCallback | Yes | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'), (err)=> { + if (err) { + console.error('Failed to setSystemNotificationUri'); + } else { + console.log('Callback invoked to indicate a successful system notification URI setting.'); + } +}) +``` + + + +## systemSoundManager.setSystemNotificationUri + +setSystemNotificationUri(context: Context, uri: string): Promise8+ + +设置的系统通知uri。使用promise方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------- | ---- | -------------------- | +| context | Context | 是 | 当前应用程序上下文。 | +| uri | string | 是 | 设置的系统通知uri。 | + +**返回值:** + +| 类型 | 说明 | +| ------------- | ----------------------------- | +| Promise | Promise回调用于返回执行结果。 | + +**示例:** + +``` +await systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'); +``` + + + +## systemSoundManager.getSystemNotificationUri + +getSystemNotificationUri(context: Context, callback: AsyncCallback): void8+ + +获取通知uri。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | -------------------- | +| context | Context | 是 | 当前应用程序上下文。 | +| callback | AsyncCallback | 是 | 获取通知uri。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemNotificationUri(null, (err, notificationUri)=>{ + if (err) { + console.err('getSystemNotificationUri failed'); + } else { + console.log('getSystemNotificationUri success: ' + notificationUri); + } +}) +``` + + + +## systemSoundManager.getSystemNotificationUri + +getSystemNotificationUri(context: Context): Promise8+ + +获取通知uri。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | -------------------------------- | +| Promise | Promise回调用于返回系统通知uri。 | + +**示例:** + +``` +let notificationUri = await systemSoundManager.getSystemNotificationUri(null); +console.log('getSystemNotificationUri : ' + uri); +``` + + + +## systemSoundManager.setSystemAlarmUri + +setSystemAlarmUri(context: Context, uri: string, callback: AsyncCallback): void8+ + +设置系统告警uri。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ------------------------ | +| context | Context | 是 | 当前应用程序上下文。 | +| uri | string | 是 | 设置的系统告警uri。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'), (err)=> { + if (err) { + console.error('Failed to setSystemAlarmUri'); + } else { + console.log('Callback invoked to indicate a successful system alarm URI setting.'); + } +}) +``` + + + +## systemSoundManager.setSystemAlarmUri + +setSystemAlarmUri(context: Context, uri: string): Promise8+ + +设置系统告警uri。使用promise方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------- | ---- | -------------------- | +| context | Context | 是 | 当前应用程序上下文。 | +| uri | string | 是 | 设置的系统告警uri。 | + +**返回值:** + +| Type | Description | +| ------------- | ----------------------------- | +| Promise | Promise回调用于返回执行结果。 | + +**示例:** + +``` +await systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'); +``` + + + +## systemSoundManager.getSystemAlarmUri + +getSystemAlarmUri(context: Context, callback: AsyncCallback): void8+ + +获取报警uri。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | -------------------- | +| context | Context | 是 | 当前应用程序上下文。 | +| callback | AsyncCallback | 是 | 获取报警uri。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemAlarmUri(null, (err, alarmUri)=>{ + if (err) { + console.err('getSystemAlarmUri failed'); + } else { + console.log('getSystemAlarmUri success: ' + alarmUri); + } +}) +``` + + + +## systemSoundManager.getSystemAlarmUri + +getSystemAlarmUri(context: Context): Promise8+ + +获取报警uri。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | -------------------------------- | +| Promise | Promise回调用于返回系统报警uri。 | + +**示例:** + +``` +let alarmUri = await systemSoundManager.getSystemAlarmUri(null); +console.log('getSystemAlarmUri success: ' + alarmUri); +``` + +# RingtonePlayer + +## ringtonePlayer.state + +只读状态:AudioState 8+ + +定义当前铃声播放器状态。 + +| 参数名 | 类型 | 可读 | 可写 | 说明 | +| ------ | ---------- | ---- | ---- | ---------------- | +| state | AudioState | 是 | 否 | 铃声播放器状态。 | + +**示例:** + +``` +systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + return; + } else { + console.log('getSystemRingtonePlayer success'); + } +}); + +var state = ringtonePlayer.state; +``` + +## ringtonePlayer.getTitle + +getTitle(callback: AsyncCallback): void8+ + +获取铃声名称。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | ------------------------ | +| callback | AsyncCallback | 是 | 回调用于返回铃声的名称。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + return; + } else { + console.log('getSystemRingtonePlayer success'); + } +}); + +ringtonePlayer.getTitle((err, title)=>{ + if (err) { + console.err('getTitle failed'); + } else { + console.log('getTitle success: ' + title); + } +}) +``` + + + +## ringtonePlayer.getTitle + +getTitle(): Promise8+ + +获取铃声名称。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| --------------- | ------------------------------- | +| Promise | Promise回调用于返回铃声的名称。 | + +**示例:** + +``` +let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +let title = await ringtonePlayer.getTitle(); +``` + + + +## ringtonePlayer.getAudioRendererInfo + +getAudioRendererInfo(callback: AsyncCallback): void8+ + +获取音频播放器信息。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------------------------------------------- | ---- | ---------------------------- | +| callback | AsyncCallback<[AudioRendererInfo](#AudioRendererInfo8+)> | 是 | 回调用于返回音频播放器信息。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + return; + } else { + console.log('getSystemRingtonePlayer success: '); + } +}); + +ringtonePlayer.getAudioRendererInfo((err, rendererInfo)=>{ + if (err) { + console.err('getAudioRendererInfo failed'); + } else { + console.log('getAudioRendererInfo success'); + } +}); +``` + + + +## ringtonePlayer.getAudioRendererInfo + +getAudioRendererInfo(): Promise8+ + +获取音频播放器信息。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| -------------------------------------------------- | ------------------------------- | +| Promise<[AudioRendererInfo](#AudioRendererInfo8+)> | Promise回调返回音频播放器信息。 | + +**示例:** + +``` +let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +let rendererInfo = await ringtonePlayer.getAudioRendererInfo(); +``` + + + +## ringtonePlayer.configure + +configure(options: RingtoneOptions, callback: AsyncCallback): void8+ + +配置铃声选项。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------ | +| options | [RingtoneOptions](#RingtoneOptions8+) | 是 | 铃声配置选项。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ + if (err) { + console.err('getSystemRingtonePlayer failed'); + return; + } else { + console.log('getSystemRingtonePlayer success: '); + } +}); + +let ringtoneOptions = { + volume: 1, + loop: false +}; + +ringtonePlayer.configure(ringtoneOptions, (err)=> { + if (err) { + console.error('Failed to configure ringtone options'); + } else { + console.log('Callback invoked to indicate a successful ringtone options configuration.'); + } +}) +``` + + + +## ringtonePlayer.configure + +configure(options: RingtoneOptions): Promise8+ + +配置铃声选项。使用promise方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | -------------- | +| options | [RingtoneOptions](#RingtoneOptions8+) | 是 | 铃声配置选项。 | + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise回调返回执行结果。 | + +**示例:** + +``` +let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); +let ringtoneOptions = { + volume: 1, + loop: false +}; + +await ringtonePlayer.configure(ringtoneOptions); +``` + + + +## ringtonePlayer.start + +start(callback: AsyncCallback): void8+ + +开始播放铃声。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ------------------------ | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例** + +``` +ringtonePlayer.start((err)=> { + if (err) { + console.error('Failed to start playing ringtone'); + } else { + console.log('Ringtone start playing successfully.'); + } +}) +``` + + + +## ringtonePlayer.start + +start(): Promise8+ + +开始播放铃声。使用promise方式返回异步结果。 + +**参数** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise回调返回执行结果。 | + +**示例:** + +``` +await ringtonePlayer.start(); +``` + + + +## ringtonePlayer.stop + +stop(callback: AsyncCallback): void8+ + +停止播放铃声。使用callback方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ------------------------ | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +ringtonePlayer.stop((err)=> { + if (err) { + console.error('Failed to stop playing ringtone'); + } else { + console.log('Ringtone stop playing successfully.'); + } +}) +``` + + + +## ringtonePlayer.stop + +stop(): Promise8+ + +停止播放铃声。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise回调返回执行结果。 | + +**示例:** + +``` +await ringtonePlayer.stop(); +``` + + + +## ringtonePlayer.release + +release(callback: AsyncCallback): void8+ + +释放铃声播放器资源。使用promise方式返回异步结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------- | ---- | ------------------------ | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | + +**返回值:** + +无 + +**示例:** + +``` +ringtonePlayer.release((err)=> { + if (err) { + console.error('Failed to release ringtone player resource'); + } else { + console.log('Release ringtone player resource successfully.'); + } +}) +``` + + + +## ringtonePlayer.release + +release(): Promise8+ + +释放铃声播放器资源。使用promise方式返回异步结果。 + +**参数:** + +无 + +**返回值:** + +| 类型 | 说明 | +| ------------- | ------------------------- | +| Promise | Promise回调返回执行结果。 | + +**示例:** + +``` +await ringtonePlayer.release(); +``` + -- Gitee From 9fc2045ea6bb1b66f498a175a1df1928f4324f7d Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Tue, 22 Feb 2022 17:17:31 +0800 Subject: [PATCH 3/9] add audiorenderer.md --- zh-cn/application-dev/media/audio-renderer.md | 106 +++++++++--------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/zh-cn/application-dev/media/audio-renderer.md b/zh-cn/application-dev/media/audio-renderer.md index 527e056a093..0294f735d5c 100644 --- a/zh-cn/application-dev/media/audio-renderer.md +++ b/zh-cn/application-dev/media/audio-renderer.md @@ -5,8 +5,6 @@ ## ***注意***: 1. 本文档适用于JavaScript。 - 2. 已经提出了对AudioRenderer接口的更改。 - 当更新的api接口被集成后,文档将被修改,并且应用程序必须与之适配。 --- @@ -36,14 +34,31 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 - 下面是一个如何使用AudioRenderer播放原始音频文件的示例。 -1. 使用 **createAudioRenderer** 为 **AudioVolumeType** 创建一个AudioRenderer实例。 +1. 使用 **createAudioRenderer** 创建一个 AudioRenderer 实例。播放器参数可以在**audioRendererOptions**中设置。 该对象可用于播放、控制和获取播放状态,以及接收回调通知。 ``` - const volType = audio.AudioVolumeType.MEDIA; // For music - const audioRenderer = audio.createAudioRenderer(volType); + var audioStreamInfo = { + samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, + channels: audio.AudioChannel.CHANNEL_1, + sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, + encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW + } + + var audioRendererInfo = { + content: audio.ContentType.CONTENT_TYPE_SPEECH, + usage: audio.StreamUsage.STREAM_USAGE_VOICE_COMMUNICATION, + rendererFlags: 1 + } + + var audioRendererOptions = { + streamInfo: audioStreamInfo, + rendererInfo: audioRendererInfo + } + + let audioRenderer = await audio.createAudioRenderer(audioRendererOptions); ``` - + 2. 使用 **on** 方法订阅音频中断事件。 当优先级更高或相等的 Stream-B 请求激活并使用输出设备时,Stream-A 被中断。 在某些情况下,框架会采取暂停和闪避(降低音量)等强制操作,并使用 **InterruptEvent** 通知应用程序。 在其他情况下,应用程序可以自行选择对 **InterruptEvent** 进行操作或忽略它。 当应用程序被强制操作中断时,它应该处理状态、更新用户界面等。 @@ -130,56 +145,35 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 }); ``` -3. 准备播放器。在实例上调用**SetParams**。需要根据音频播放规范设置音频播放器参数。 +3. 在 AudioRenderer 实例上调用 **start()** 函数来启动/恢复播放任务。\ + 启动完成后,渲染器状态将为 STATE_RUNNING。 然后您可以开始写入缓冲区。 ``` - async function prepareRenderer() { - // file_example_WAV_2MG.wav - var audioParams = { - format: audio.AudioSampleFormat.SAMPLE_S16LE, - channels: audio.AudioChannel.STEREO, - samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, - encoding: audio.AudioEncodingType.ENCODING_PCM, - }; - - let response = await audioRenderer.setParams(audioParams); + async function startRenderer() { var state = audioRenderer.state; - if (state != audio.AudioState.STATE_PREPARED) { - console.info('Prepare renderer failed'); - return; - } - } - ``` - -4. 在AudioRenderer 实例调用 **start()** 函数来启动/恢复播放任务。 - 启动完成后,当音频播放器状态将为 STATE _RUNNING时,可以开始将数据写入缓冲区。 - - ``` - async function startRenderer() { - var state = audioRenderer.state; - // 状态应该是准备、暂停或停止。 + // state should be prepared, paused or stopped. if (state != audio.AudioState.STATE_PREPARED || state != audio.AudioState.STATE_PAUSED || state != audio.AudioState.STATE_STOPPED) { console.info('Renderer is not in a correct state to start'); return; } - var started = await audioRenderer.start(); - if (started) { - isPlay = true; + + await audioRenderer.start(); + + state = audioRenderer.state; + if (state == audio.AudioState.STATE_RUNNING) { console.info('Renderer started'); } else { console.error('Renderer start failed'); - return; } } - ``` -5. 调用 **write** 开始向缓冲区写入数据。 +4. 调用 **write** 开始向缓冲区写入数据。 将要播放的音频数据读入缓冲区。 反复调用write方法写入数据。 ``` - async function writeBuffer(buf) { + async function writeBuffer(buf) { var state = audioRenderer.state; if (state != audio.AudioState.STATE_RUNNING) { console.error('Renderer is not running, do not write'); @@ -194,7 +188,7 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 } } - // 音频播放器合理值是最小缓冲区大小。其他大小也可接受。 + // Reasonable minimum buffer size for renderer. However, the renderer can accept other read sizes as well. const bufferSize = await audioRenderer.getBufferSize(); const path = '/data/file_example_WAV_2MG.wav'; let ss = fileio.createStreamSync(path, 'r'); @@ -218,10 +212,10 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 } else { console.info('check after next interval'); } - } , 30); //根据音频文件格式设置间隔。 + } , 30); // interval to be set based on audio file format ``` - -6. (可选)在 AudioRenderer 实例上调用 **pause()** 或 **stop()**。 + +5. (可选)在 AudioRenderer 实例上调用 **pause()** 或 **stop()**。 ``` async function pauseRenderer() { @@ -230,8 +224,11 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 console.info('Renderer is not running'); return; } - var paused = await audioRenderer.pause(); - if (paused) { + + await audioRenderer.pause(); + + state = audioRenderer.state; + if (state == audio.AudioState.STATE_PAUSED) { console.info('Renderer paused'); } else { console.error('Renderer pause failed'); @@ -244,8 +241,11 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 console.info('Renderer is not running or paused'); return; } - var stopped = await audioRenderer.stop(); - if (stopped) { + + await audioRenderer.stop(); + + state = audioRenderer.state; + if (state == audio.AudioState.STATE_STOPPED) { console.info('Renderer stopped'); } else { console.error('Renderer stop failed'); @@ -253,17 +253,20 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 } ``` -7. 播放任务完成后,调用AudioRenderer实例上的**release()**函数释放资源。 +6. 播放任务完成后,调用AudioRenderer实例上的**release()**函数释放资源。 AudioRenderer 可以使用大量的系统资源。因此,只要不再需要资源,就必须释放它们。为确保分配给它的任何系统资源都得到适当释放, 您应该始终调用**release()**。 - + ``` - async function releaseRenderer() { + async function releaseRenderer() { if (state_ == RELEASED || state_ == NEW) { console.info('Resourced already released'); return; } - var released = await audioRenderer.release(); - if (released) { + + await audioRenderer.release(); + + state = audioRenderer.state; + if (state == STATE_RELEASED) { console.info('Renderer released'); } else { console.info('Renderer release failed'); @@ -277,7 +280,6 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 您还应该记住,AudioRenderer 是基于状态的。 也就是说,AudioRenderer 有一个内部状态,在调用播放控制 API 时必须始终检查它,因为某些操作仅在音频播放器处于给定状态时才可接受。 如果您在不正确的状态下执行操作,系统可能会抛出错误/异常或生成其他未定义的行为。 -在每个必要的操作之前,示例代码都会执行状态检查。 ## **异步操作:** -- Gitee From cd8a9004b174fd54cf1d2e9cf308e1ad4df08077 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Wed, 2 Mar 2022 10:41:56 +0800 Subject: [PATCH 4/9] md --- zh-cn/application-dev/media/audio-renderer.md | 111 +-- .../media/system-sound-manager.md | 169 ---- .../reference/apis/js-apis-audio.md | 797 ------------------ 3 files changed, 56 insertions(+), 1021 deletions(-) delete mode 100644 zh-cn/application-dev/media/system-sound-manager.md diff --git a/zh-cn/application-dev/media/audio-renderer.md b/zh-cn/application-dev/media/audio-renderer.md index 0294f735d5c..390ae77c0ac 100644 --- a/zh-cn/application-dev/media/audio-renderer.md +++ b/zh-cn/application-dev/media/audio-renderer.md @@ -61,9 +61,9 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 2. 使用 **on** 方法订阅音频中断事件。 当优先级更高或相等的 Stream-B 请求激活并使用输出设备时,Stream-A 被中断。 - 在某些情况下,框架会采取暂停和闪避(降低音量)等强制操作,并使用 **InterruptEvent** 通知应用程序。 在其他情况下,应用程序可以自行选择对 **InterruptEvent** 进行操作或忽略它。 当应用程序被强制操作中断时,它应该处理状态、更新用户界面等。 + 在某些情况下,框架会采取暂停和降低音量等强制操作,并使用 **InterruptEvent** 通知应用程序。 在其他情况下,应用程序可以自行选择对 **InterruptEvent** 做出响应。 当应用程序被强制操作中断时,它应该处理状态、更新用户界面等。 - 在音频中断的情况下,应用程序可能会遇到写入失败,中断无法感知到这类情况。在写入音频数据之前可以使用 **audioRenderer.state** 方法检查渲染器状态,而中断可通过此监听获取到更多详细信息。 + 在音频中断的情况下,应用程序可能会遇到写入失败,不感知、不处理中断的应用程序在写入音频数据之前可以使用 **audioRenderer.state** 方法检查播放器状态,而中断可通过此监听获取到更多详细信息。 中断事件信息: @@ -79,7 +79,7 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 | 名称 | 描述 | | :-------------- | :------------------------- | | INTERRUPT_FORCE | 音频状态已被框架更改。 | - | INTERRUPT_SHARE | 应用程序是否响应中断事件。 | + | INTERRUPT_SHARE | 应用程序可以决定是否响应。 | 3) **hintType:**中断事件类型。 @@ -91,7 +91,7 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 | INTERRUPT_HINT_DUCK | 降低播放流的音量。 | | INTERRUPT_HINT_UNDUCK | 恢复播放流的音量。 | - 4) **某些操作是完全强制或共享的**,这意味着它们由框架或应用程序执行。 + 4) **某些操作是完全强制或共享的**,分别代表,强制表示框架执行,共享表示应用执行。 例如,当音乐流正在进行时收到呼叫时,框架会强制音乐流暂停。调用结束后框架不会强制恢复音乐流.相反,它会提醒应用程序恢复播放。 | 名称 | 描述 | @@ -102,50 +102,51 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 ``` audioRenderer.on('interrupt', (interruptEvent) => { - console.info('InterruptEvent Received'); - console.info('InterruptType: ' + interruptEvent.eventType); - console.info('InterruptForceType: ' + interruptEvent.forceType); - console.info('AInterruptHint: ' + interruptEvent.hintType); - if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { - switch (interruptEvent.hintType) { - // 强制暂停:由框架采取行动。 - // 停止写入以避免数据丢失。 - case audio.InterruptHint.INTERRUPT_HINT_PAUSE: - isPlay = false; - break; - // 强制停止:由框架采取行动。 - // 停止写入以避免数据丢失。 - case audio.InterruptHint.INTERRUPT_HINT_STOP: - isPlay = false; - break; - // 降低音量: 由框架采取行动。 - // 仅需通知应用程序音量已减小。 - case audio.InterruptHint.INTERRUPT_HINT_DUCK: - break; - // 恢复音量: 由框架采取行动。 - // 仅需通知应用程序音量已恢复。 - case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: - break; - } - } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { - switch (interruptEvent.hintType) { - // 恢复:由应用采取行动。 - // 在需要的情况下,恢复强制暂停流。 - case audio.InterruptHint.INTERRUPT_HINT_RESUME: - startRenderer(); - break; - // 暂停:流已被中断。 - // 可以选择暂停或同时播放。 - case audio.InterruptHint.INTERRUPT_HINT_PAUSE: - isPlay = false; - pauseRenderer(); - break; - } - } - }); - ``` - -3. 在 AudioRenderer 实例上调用 **start()** 函数来启动/恢复播放任务。\ + console.info('InterruptEvent Received'); + console.info('InterruptType: ' + interruptEvent.eventType); + console.info('InterruptForceType: ' + interruptEvent.forceType); + console.info('AInterruptHint: ' + interruptEvent.hintType); + + if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_FORCE) { + switch (interruptEvent.hintType) { + // Force Pause: Action was taken by framework. + // Halt the write calls to avoid data loss. + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + isPlay = false; + break; + // Force Stop: Action was taken by framework. + // Halt the write calls to avoid data loss. + case audio.InterruptHint.INTERRUPT_HINT_STOP: + isPlay = false; + break; + // Force Duck: Action was taken by framework, + // just notifying the app that volume has been reduced. + case audio.InterruptHint.INTERRUPT_HINT_DUCK: + break; + // Force Unduck: Action was taken by framework, + // just notifying the app that volume has been restored. + case audio.InterruptHint.INTERRUPT_HINT_UNDUCK: + break; + } + } else if (interruptEvent.forceType == audio.InterruptForceType.INTERRUPT_SHARE) { + switch (interruptEvent.hintType) { + // Share Resume: Action is to be taken by App. + // Resume the force paused stream if required. + case audio.InterruptHint.INTERRUPT_HINT_RESUME: + startRenderer(); + break; + // Share Pause: Stream has been interrupted, + // It can choose to pause or play concurrently. + case audio.InterruptHint.INTERRUPT_HINT_PAUSE: + isPlay = false; + pauseRenderer(); + break; + } + } + }); +``` + +3. 在 AudioRenderer 实例上调用 **start()** 函数来启动/恢复播放任务。 启动完成后,渲染器状态将为 STATE_RUNNING。 然后您可以开始写入缓冲区。 ``` @@ -169,7 +170,7 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 } ``` -4. 调用 **write** 开始向缓冲区写入数据。 +4. 调用 **write** 开始向缓冲区写入数据。 将要播放的音频数据读入缓冲区。 反复调用write方法写入数据。 ``` @@ -253,7 +254,7 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 } ``` -6. 播放任务完成后,调用AudioRenderer实例上的**release()**函数释放资源。 +6. 播放任务完成后,调用AudioRenderer实例上的**release()** 函数释放资源。 AudioRenderer 可以使用大量的系统资源。因此,只要不再需要资源,就必须释放它们。为确保分配给它的任何系统资源都得到适当释放, 您应该始终调用**release()**。 ``` @@ -277,16 +278,16 @@ AudioRenderer框架提供了用于播放音频文件和控制播放的接口。 ## **状态检查的重要性:** -您还应该记住,AudioRenderer 是基于状态的。 -也就是说,AudioRenderer 有一个内部状态,在调用播放控制 API 时必须始终检查它,因为某些操作仅在音频播放器处于给定状态时才可接受。 +您还应该记住,AudioRenderer 是基于状态的。 +也就是说,AudioRenderer 有一个内部状态,在调用播放控制 API 时必须始终检查它,因为某些操作仅在音频播放器处于给定状态时才可接受。 如果您在不正确的状态下执行操作,系统可能会抛出错误/异常或生成其他未定义的行为。 ## **异步操作:** 大多数AudioRenderer调用都是异步的。因此,UI线程不会被阻塞。 -对于每个API,框架都提供callback函数和promise函数。 -在本例中,为了简单起见,使用了promise函数。[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) -为回调和承诺提供参考。 + +对于每个API,框架都提供callback函数和promise函数。 +在本例中,为了简单起见,使用了promise函数。[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md)为callback和promise提供参考。 ## **其他 APIs 接口:** diff --git a/zh-cn/application-dev/media/system-sound-manager.md b/zh-cn/application-dev/media/system-sound-manager.md deleted file mode 100644 index 113f6098f41..00000000000 --- a/zh-cn/application-dev/media/system-sound-manager.md +++ /dev/null @@ -1,169 +0,0 @@ -# SoundManager开发指南 - -## **摘要** -本指南将展示如何使用SoundManager保存和检索铃声、告警和通知URI。 -另外文档还解释了如何使用铃声播放器API来播放铃声uri。 - -## **SoundManager框架** -SoundManager 接口由用于保存和检索铃声、告警和通知 uris 的接口 组成。 -SoundManager 还提供了铃声播放所需的其他接口。所有 uris 都将保存在设备的持久内存中。 - -请参考[**SystemSoundManager**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md##systemsoundmanager)的api支持的列表。 - - -## **使用**步骤 -- 下面是一个如何使用SoundManager保存和检索以及告警uri的例子 -1. 使用**getSystemSoundManager**获取一个SoundManager实例 - 此对象用于设置和获取告警的uri。 - ``` - const systemSoundManager = audio.getSystemSoundManager(); - ``` - -2. 使用**setSystemAlarmUri**设置系统告警uri - 应用程序应在使用此 API 时提供应用程序的上下文信息。 - - ``` - systemSoundManager.setSystemAlarmUri(context, '/data/media/alarm.wav', (err)=> { - if (err) { - console.error('Failed to setSystemAlarmUri'); - } else { - console.log('Callback invoked to indicate a successful system alarm uri setting.'); - } - }) - ``` - -3. 使用 **getSystemAlarmUri** 获取系统警报 uri - 应用程序应在使用此 API 时提供应用程序上下文信息。 - - ``` - systemSoundManager.getSystemAlarmUri(context, (err, alarmUri)=>{ - if (err) { - console.err('getSystemAlarmUri failed'); - } else { - console.log('getSystemAlarmUri success: ' + alarmUri); - } - }) - ``` - - -- 这是另一个如何使用 SoundManager 进行铃声 uri 播放的示例。 -1. 使用 **getSystemSoundManager**获取SoundManager实例。 - ``` - const systemSoundManager = audio.getSystemSoundManager(); - ``` - -2. 使用**setSystemRingtoneUri**设置系统铃声uri - 使用API时需要提供应用程序的上下文信息、文件uri以及铃声类型 [*RingtoneType*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#ringtonetype8)。 - - ``` - systemSoundManager.setSystemRingtoneUri(context, '/data/media/ringtone_sim1.wav', - audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err)=> { - if (err) { - console.error('Failed to setSystemRingtoneUri'); - } else { - console.log('Callback invoked to indicate a successful system ringtone uri setting.'); - } - }) - ``` - -3. 使用 **getSystemRingtonePlayer**获取铃声播放器实例\ - 使用API时需要提供应用程序的上下文信息、铃声类型[*RingtoneType*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#RingtoneType8)。 - - ``` - systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - } else { - console.log('getSystemRingtonePlayer success: '); - } - }); - ``` - -4. 使用**configure**配置铃声播放器\ - 使用API时需要提供铃声参数[*RingtoneOptions*](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md#ringtoneoptions8)。 - - ``` - let ringtoneOptions = { - volume: 1, - loop: false - }; - - ringtonePlayer.configure(ringtoneOptions, (err)=> { - if (err) { - console.error('Failed to configure ringtone options'); - } else { - console.log('Callback invoked to indicate a successful ringtone options configuration.'); - } - }); - ``` - -5. 使用**start**开始播放铃声\ - *注意*:如果在configure中*loop*为真,铃声将继续播放,直到其停止显式。 - ``` - ringtonePlayer.start((err)=> { - if (err) { - console.error('Failed to start playing ringtone'); - } else { - console.log('Ringtone start playing successfully.'); - } - }); - ``` - -6. 使用**stop**来停止正在播放的铃声。 - ``` - ringtonePlayer.stop((err)=> { - if (err) { - console.error('Failed to stop playing ringtone'); - } else { - console.log('Ringtone stop playing successfully.'); - } - }); - ``` - -7. 使用**release**释放铃声播放器资源。 - ``` - ringtonePlayer.release((err)=> { - if (err) { - console.error('Failed to release ringtone player resource'); - } else { - console.log('Release ringtone player resource successfully.'); - } - }); - ``` - -## **其他APIs** -ringtonePlayer对象提供了更多的api,比如; - -1. **state** - 只读变量,包含了铃声播放器当前所有的状态。 - - ``` - let playerState = ringtonePlayer.state - ``` -``` - -2. **getTitle** - 从文件元数据返回铃声uri的路径信息。 - -``` - ringtonePlayer.getTitle((err, title)=>{ - if (err) { - console.err('getTitle failed'); - } else { - console.log('getTitle success: ' + title); - } - }); - ``` - -3. **getAudioRendererInfo** - 获取音频渲染器信息。 - - ``` - ringtonePlayer.getAudioRendererInfo((err, rendererInfo)=>{ - if (err) { - console.err('getAudioRendererInfo failed'); - } else { - console.log('getAudioRendererInfo success'); - } - }); - ``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-audio.md b/zh-cn/application-dev/reference/apis/js-apis-audio.md index 0a520ca4c56..0b74285c3f7 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-audio.md +++ b/zh-cn/application-dev/reference/apis/js-apis-audio.md @@ -1977,800 +1977,3 @@ audioRenderer.on('interrupt', (interruptEvent) => { }) ``` -# SystemSoundManager - -## systemSoundManager.setSystemRingtoneUri - -setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType, callback: AsyncCallback): void8+ - -设置系统铃声。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------- | ---- | ------------------------ | -| context | Context | 是 | 当前的应用程序上下文。 | -| uri | string | 是 | 待设置的铃声uri。 | -| type | [RingtoneType](#RingtoneType8+) | 是 | 待设置的铃声类型。 | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtonetone.wav', - audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err)=> { - if (err) { - console.error('Failed to setSystemRingtoneUri'); - } else { - console.log('Callback invoked to indicate a successful system ringtone URI setting.'); - } -}) -``` - -## systemSoundManager.setSystemRingtoneUri - -setSystemRingtoneUri(context: Context, uri: string, type: RingtoneType): Promise8+ - -设置系统铃声。使用promise方式返回异步结果。 - -**参数** - -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------------------------------- | ---- | ---------------------- | -| context | Context | 是 | 当前的应用程序上下文。 | -| uri | string | 是 | 待设置的铃声uri。 | -| type | [RingtoneType](#RingtoneType8+) | 是 | 待设置的铃声类型。 | - -**返回值:** - -| 类型 | 说明 | -| ------------- | ------------------------- | -| Promise | Promise用于返回执行结果。 | - -**示例:** - -``` -await systemSoundManager.setSystemRingtoneUri(null, '/data/media/Ringtone.wav', audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -``` - -## systemSoundManager.getSystemRingtoneUri - -getSystemRingtoneUri(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ - -获取铃声uri。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------- | ---- | ------------------------ | -| context | Context | 是 | 当前的应用程序上下文。 | -| type | [RingtoneType](#RingtoneType8+) | 是 | 获取铃声类型。 | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtoneUri)=>{ - if (err) { - console.err('getSystemRingtoneUri failed'); - } else { - console.log('getSystemRingtoneUri success: ' + ringtoneUri); - } -}) -``` - - - -## systemSoundManager.getSystemRingtoneUri - -getSystemRingtoneUri(context: Context, type: RingtoneType): Promise8+ - -获取铃声uri。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| --------------- | ---------------------- | -| Promise | Promise返回铃声的uri。 | - -**示例:** - -``` -let ringtoneUri = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -if (ringtoneUri == '/data/media/Believer60s.wav') { - console.log('getSystemRingtoneUri success: ' + uri); -} else { - console.log('getSystemRingtoneUri fail: ' + uri); -} -``` - - - -## systemSoundManager.getSystemRingtonePlayer - -getSystemRingtonePlayer(context: Context, type: RingtoneType, callback: AsyncCallback): void8+ - -获取铃声播放器。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------ | ---- | ------------------------ | -| context | Context | 是 | 当前应用程序上下文。 | -| type | RingtoneType | 是 | 获取铃声类型。 | -| callback | AsyncCallback) | 是 | 回调用于获取铃声播放器。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - } else { - console.log('getSystemRingtonePlayer success: '); - } -}) -``` - - - -## systemSoundManager.getSystemRingtonePlayer - -getSystemRingtonePlayer(context: Context, type: RingtoneType): Promise8+ - -获取铃声播放器。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| ----------------------- | ------------------------------- | -| Promise | Promise回调用于获取铃声播放器。 | - -**示例:** - -``` -let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -``` - - - -## systemSoundManager.setSystemNotificationUri - -setSystemNotificationUri(context: Context, uri: string, callback: AsyncCallback): void8+ - -设置的系统通知uri。使用callback方式返回异步结果。 - -**参数** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------- | ---- | ------------------------ | -| context | Context | Yes | 当前应用程序上下文。 | -| uri | string | Yes | 设置的系统通知uri。 | -| callback | AsyncCallback | Yes | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'), (err)=> { - if (err) { - console.error('Failed to setSystemNotificationUri'); - } else { - console.log('Callback invoked to indicate a successful system notification URI setting.'); - } -}) -``` - - - -## systemSoundManager.setSystemNotificationUri - -setSystemNotificationUri(context: Context, uri: string): Promise8+ - -设置的系统通知uri。使用promise方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------- | ---- | -------------------- | -| context | Context | 是 | 当前应用程序上下文。 | -| uri | string | 是 | 设置的系统通知uri。 | - -**返回值:** - -| 类型 | 说明 | -| ------------- | ----------------------------- | -| Promise | Promise回调用于返回执行结果。 | - -**示例:** - -``` -await systemSoundManager.setSystemNotificationUri(null, '/data/media/Notification.wav'); -``` - - - -## systemSoundManager.getSystemNotificationUri - -getSystemNotificationUri(context: Context, callback: AsyncCallback): void8+ - -获取通知uri。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------- | ---- | -------------------- | -| context | Context | 是 | 当前应用程序上下文。 | -| callback | AsyncCallback | 是 | 获取通知uri。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemNotificationUri(null, (err, notificationUri)=>{ - if (err) { - console.err('getSystemNotificationUri failed'); - } else { - console.log('getSystemNotificationUri success: ' + notificationUri); - } -}) -``` - - - -## systemSoundManager.getSystemNotificationUri - -getSystemNotificationUri(context: Context): Promise8+ - -获取通知uri。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| --------------- | -------------------------------- | -| Promise | Promise回调用于返回系统通知uri。 | - -**示例:** - -``` -let notificationUri = await systemSoundManager.getSystemNotificationUri(null); -console.log('getSystemNotificationUri : ' + uri); -``` - - - -## systemSoundManager.setSystemAlarmUri - -setSystemAlarmUri(context: Context, uri: string, callback: AsyncCallback): void8+ - -设置系统告警uri。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------- | ---- | ------------------------ | -| context | Context | 是 | 当前应用程序上下文。 | -| uri | string | 是 | 设置的系统告警uri。 | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'), (err)=> { - if (err) { - console.error('Failed to setSystemAlarmUri'); - } else { - console.log('Callback invoked to indicate a successful system alarm URI setting.'); - } -}) -``` - - - -## systemSoundManager.setSystemAlarmUri - -setSystemAlarmUri(context: Context, uri: string): Promise8+ - -设置系统告警uri。使用promise方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------- | ---- | -------------------- | -| context | Context | 是 | 当前应用程序上下文。 | -| uri | string | 是 | 设置的系统告警uri。 | - -**返回值:** - -| Type | Description | -| ------------- | ----------------------------- | -| Promise | Promise回调用于返回执行结果。 | - -**示例:** - -``` -await systemSoundManager.setSystemAlarmUri(null, '/data/media/Alarm.wav'); -``` - - - -## systemSoundManager.getSystemAlarmUri - -getSystemAlarmUri(context: Context, callback: AsyncCallback): void8+ - -获取报警uri。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------- | ---- | -------------------- | -| context | Context | 是 | 当前应用程序上下文。 | -| callback | AsyncCallback | 是 | 获取报警uri。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemAlarmUri(null, (err, alarmUri)=>{ - if (err) { - console.err('getSystemAlarmUri failed'); - } else { - console.log('getSystemAlarmUri success: ' + alarmUri); - } -}) -``` - - - -## systemSoundManager.getSystemAlarmUri - -getSystemAlarmUri(context: Context): Promise8+ - -获取报警uri。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| --------------- | -------------------------------- | -| Promise | Promise回调用于返回系统报警uri。 | - -**示例:** - -``` -let alarmUri = await systemSoundManager.getSystemAlarmUri(null); -console.log('getSystemAlarmUri success: ' + alarmUri); -``` - -# RingtonePlayer - -## ringtonePlayer.state - -只读状态:AudioState 8+ - -定义当前铃声播放器状态。 - -| 参数名 | 类型 | 可读 | 可写 | 说明 | -| ------ | ---------- | ---- | ---- | ---------------- | -| state | AudioState | 是 | 否 | 铃声播放器状态。 | - -**示例:** - -``` -systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - return; - } else { - console.log('getSystemRingtonePlayer success'); - } -}); - -var state = ringtonePlayer.state; -``` - -## ringtonePlayer.getTitle - -getTitle(callback: AsyncCallback): void8+ - -获取铃声名称。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------- | ---- | ------------------------ | -| callback | AsyncCallback | 是 | 回调用于返回铃声的名称。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - return; - } else { - console.log('getSystemRingtonePlayer success'); - } -}); - -ringtonePlayer.getTitle((err, title)=>{ - if (err) { - console.err('getTitle failed'); - } else { - console.log('getTitle success: ' + title); - } -}) -``` - - - -## ringtonePlayer.getTitle - -getTitle(): Promise8+ - -获取铃声名称。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| --------------- | ------------------------------- | -| Promise | Promise回调用于返回铃声的名称。 | - -**示例:** - -``` -let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -let title = await ringtonePlayer.getTitle(); -``` - - - -## ringtonePlayer.getAudioRendererInfo - -getAudioRendererInfo(callback: AsyncCallback): void8+ - -获取音频播放器信息。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------------------------------------------- | ---- | ---------------------------- | -| callback | AsyncCallback<[AudioRendererInfo](#AudioRendererInfo8+)> | 是 | 回调用于返回音频播放器信息。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - return; - } else { - console.log('getSystemRingtonePlayer success: '); - } -}); - -ringtonePlayer.getAudioRendererInfo((err, rendererInfo)=>{ - if (err) { - console.err('getAudioRendererInfo failed'); - } else { - console.log('getAudioRendererInfo success'); - } -}); -``` - - - -## ringtonePlayer.getAudioRendererInfo - -getAudioRendererInfo(): Promise8+ - -获取音频播放器信息。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| -------------------------------------------------- | ------------------------------- | -| Promise<[AudioRendererInfo](#AudioRendererInfo8+)> | Promise回调返回音频播放器信息。 | - -**示例:** - -``` -let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -let rendererInfo = await ringtonePlayer.getAudioRendererInfo(); -``` - - - -## ringtonePlayer.configure - -configure(options: RingtoneOptions, callback: AsyncCallback): void8+ - -配置铃声选项。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------- | ---- | ------------------------ | -| options | [RingtoneOptions](#RingtoneOptions8+) | 是 | 铃声配置选项。 | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -systemSoundManager.getSystemRingtonePlayer(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT, (err, ringtonePlayer)=>{ - if (err) { - console.err('getSystemRingtonePlayer failed'); - return; - } else { - console.log('getSystemRingtonePlayer success: '); - } -}); - -let ringtoneOptions = { - volume: 1, - loop: false -}; - -ringtonePlayer.configure(ringtoneOptions, (err)=> { - if (err) { - console.error('Failed to configure ringtone options'); - } else { - console.log('Callback invoked to indicate a successful ringtone options configuration.'); - } -}) -``` - - - -## ringtonePlayer.configure - -configure(options: RingtoneOptions): Promise8+ - -配置铃声选项。使用promise方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------------------------------------- | ---- | -------------- | -| options | [RingtoneOptions](#RingtoneOptions8+) | 是 | 铃声配置选项。 | - -**返回值:** - -| 类型 | 说明 | -| ------------- | ------------------------- | -| Promise | Promise回调返回执行结果。 | - -**示例:** - -``` -let ringtonePlayer = await systemSoundManager.getSystemRingtoneUri(null, audio.RingtoneType.RINGTONE_TYPE_DEFAULT); -let ringtoneOptions = { - volume: 1, - loop: false -}; - -await ringtonePlayer.configure(ringtoneOptions); -``` - - - -## ringtonePlayer.start - -start(callback: AsyncCallback): void8+ - -开始播放铃声。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------- | ---- | ------------------------ | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例** - -``` -ringtonePlayer.start((err)=> { - if (err) { - console.error('Failed to start playing ringtone'); - } else { - console.log('Ringtone start playing successfully.'); - } -}) -``` - - - -## ringtonePlayer.start - -start(): Promise8+ - -开始播放铃声。使用promise方式返回异步结果。 - -**参数** - -无 - -**返回值:** - -| 类型 | 说明 | -| ------------- | ------------------------- | -| Promise | Promise回调返回执行结果。 | - -**示例:** - -``` -await ringtonePlayer.start(); -``` - - - -## ringtonePlayer.stop - -stop(callback: AsyncCallback): void8+ - -停止播放铃声。使用callback方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------- | ---- | ------------------------ | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -ringtonePlayer.stop((err)=> { - if (err) { - console.error('Failed to stop playing ringtone'); - } else { - console.log('Ringtone stop playing successfully.'); - } -}) -``` - - - -## ringtonePlayer.stop - -stop(): Promise8+ - -停止播放铃声。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| ------------- | ------------------------- | -| Promise | Promise回调返回执行结果。 | - -**示例:** - -``` -await ringtonePlayer.stop(); -``` - - - -## ringtonePlayer.release - -release(callback: AsyncCallback): void8+ - -释放铃声播放器资源。使用promise方式返回异步结果。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------- | ---- | ------------------------ | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | - -**返回值:** - -无 - -**示例:** - -``` -ringtonePlayer.release((err)=> { - if (err) { - console.error('Failed to release ringtone player resource'); - } else { - console.log('Release ringtone player resource successfully.'); - } -}) -``` - - - -## ringtonePlayer.release - -release(): Promise8+ - -释放铃声播放器资源。使用promise方式返回异步结果。 - -**参数:** - -无 - -**返回值:** - -| 类型 | 说明 | -| ------------- | ------------------------- | -| Promise | Promise回调返回执行结果。 | - -**示例:** - -``` -await ringtonePlayer.release(); -``` - -- Gitee From 580846df2e9b528ae97280993bc5909d2588d984 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Thu, 3 Mar 2022 09:45:26 +0800 Subject: [PATCH 5/9] md --- .../reference/apis/js-apis-audio.md | 149 +++++++++--------- 1 file changed, 71 insertions(+), 78 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-audio.md b/zh-cn/application-dev/reference/apis/js-apis-audio.md index 0b74285c3f7..c8802dfd140 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-audio.md +++ b/zh-cn/application-dev/reference/apis/js-apis-audio.md @@ -31,15 +31,15 @@ var audioManager = audio.getAudioManager(); ## audioRenderer -createAudioRenderer(volumeType: AudioVolumeType): AudioRenderer +createAudioRenderer(options: AudioRendererOptions): AudioRenderer 获取音频播放器。 **参数**: -| 参数名 | 类型 | 必填 | 说明 | -| ---------- | --------------- | ---- | ----------- | -| volumeType | AudioVolumeType | 是 | 音频流类型. | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------- | +| options | [AudioRendererOptions](#audiorendereroptions) | 是 | 配置播放器. | **返回值**: @@ -137,7 +137,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | BLUETOOTH_A2DP | 8 | 蓝牙设备A2DP连接(Advanced Audio Distribution Profile)。 | | MIC | 15 | 麦克风。 | -## AudioRingMode7+ +## AudioRingMode7+ 枚举,铃声模式。 @@ -147,19 +147,19 @@ const systemSoundManager = audio.getSystemSoundManager(); | RINGER_MODE_VIBRATE | 1 | 震动模式 | | RINGER_MODE_NORMAL | 2 | 响铃模式 | -## AudioSampleFormat8+ +## AudioSampleFormat8+ 枚举,音频采样格式。 -| 名称 | 默认值 | 描述 | -| ------------- | ------ | ------------------------ | -| INVALID_WIDTH | -1 | 无效格式。 | -| SAMPLE_U8 | 1 | 无符号8位整数。 | -| SAMPLE_S16LE | 0 | 带符号的16位整数,小尾数 | -| SAMPLE_S24LE | 1 | 带符号的24位整数,小尾数 | -| SAMPLE_S32LE | 2 | 带符号的24位整数,小尾数 | +| 名称 | 默认值 | 描述 | +| --------------------- | ------ | ------------------------ | +| SAMPLE_FORMAT_INVALID | -1 | 无效格式。 | +| SAMPLE_FORMAT_U8 | 0 | 无符号8位整数。 | +| SAMPLE_FORMAT_S16LE | 1 | 带符号的16位整数,小尾数 | +| SAMPLE_FORMAT_S24LE | 2 | 带符号的24位整数,小尾数 | +| SAMPLE_FORMAT_S32LE | 3 | 带符号的24位整数,小尾数 | -## AudioChannel8+ +## AudioChannel8+ 枚举, 音频声道。 @@ -168,7 +168,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | CHANNEL_1 | 0x1 << 0 | 单声道。 | | CHANNEL_2 | 0x1 << 1 | 双声道。 | -## AudioSamplingRate8+ +## AudioSamplingRate8+ 枚举,音频采样率。 @@ -186,7 +186,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | SAMPLE_RATE_64000 | 64000 | 采样率64000。 | | SAMPLE_RATE_96000 | 96000 | 采样率96000。 | -## AudioEncodingType8+ +## AudioEncodingType8+ 枚举,音频编码类型。 @@ -195,7 +195,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | ENCODING_TYPE_INVALID | -1 | 无效类型。 | | ENCODING_TYPE_RAW | 0 | RAW类型。 | -## ContentType8+ +## ContentType8+ 枚举,媒体类型。 @@ -208,7 +208,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | CONTENT_TYPE_SONIFICATION | 4 | 表示类型为加密。 | | CONTENT_TYPE_RINGTONE | 5 | 表示类型为铃声。 | -## StreamUsage8+ +## StreamUsage8+ 枚举,音频流使用类型。 @@ -219,7 +219,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | STREAM_USAGE_VOICE_COMMUNICATION | 2 | 表示语音通信。 | | STREAM_USAGE_NOTIFICATION_RINGTONE | 3 | 表示通知铃声。 | -## AudioState8+ +## AudioState8+ 枚举,音频状态。 @@ -233,7 +233,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | STATE_RELEASED | 4 | 释放状态。 | | STATE_PAUSED | 5 | 暂停状态。 | -## AudioRendererRate8+ +## AudioRendererRate8+ 枚举,渲染速度。 @@ -243,7 +243,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | RENDER_RATE_DOUBLE | 1 | 双倍速度。 | | RENDER_RATE_HALF | 2 | 1/2的速度。 | -## InterruptType8+ +## InterruptType8+ 枚举,中断类型。 @@ -252,7 +252,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | INTERRUPT_TYPE_BEGIN | 1 | 音频播放中断开始。 | | INTERRUPT_TYPE_END | 2 | 音频播放中断结束。 | -## InterruptForceType8+ +## InterruptForceType8+ 枚举,强制打断类型。 @@ -261,7 +261,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | INTERRUPT_FORCE | 0 | 由系统采取强制操作。 | | INTERRUPT_SHARE | 1 | App可以选择采取行动或忽略 | -## InterruptHint8+ +## InterruptHint8+ 枚举,中断提示。 @@ -274,53 +274,55 @@ const systemSoundManager = audio.getSystemSoundManager(); | INTERRUPT_HINT_DUCK | 4 | 音频躲避。(躲避:音量减弱,而不会停止) | | INTERRUPT_HINT_UNDUCK | 5 | 恢复音量。 | -## RingtoneType8+ +## AudioStreamInfo8+ -枚举,铃声类型。 +描述音频流信息。 -| 名称 | 默认值 | 描述 | -| ---------------------- | ------ | ------------------- | -| RINGTONE_TYPE_DEFAULT | 0 | 表示默认类型。 | -| RINGTONE_TYPE_MULTISIM | 1 | 表示Multi-sim类型。 | +**参数:** -## AudioStreamInfo8+ +| 参数名 | 类型 | 必填 | 说明 | +| ------------ | --------------------------------------- | ---- | -------------------------- | +| samplingRate | [AudioSamplingRate](#audiosamplingrate) | 是 | 表示播放音频文件的采样率。 | +| channels | [AudioChannel](#audiochannel) | 是 | 表示播放音频文件的通道数。 | +| sampleFormat | [AudioSampleFormat](#audiosampleformat) | 是 | 表示音频采样格式。 | +| encodingType | [AudioEncodingType](#audioencodingtype) | 是 | 表示音频编码格式。 | -描述音频流信息。 +## AudioRendererInfo8+ + +描述音频渲染器信息。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------------ | ----------------------------------------- | ---- | -------------------------- | -| samplingRate | [AudioSamplingRate](#AudioRendererRate8+) | 是 | 表示播放音频文件的采样率。 | -| channels | [AudioChannel](#AudioChannel8+) | 是 | 表示播放音频文件的通道数。 | -| sampleFormat | [AudioSampleFormat](#AudioSampleFormat8+) | 是 | 表示音频采样格式。 | -| encodingType | [AudioEncodingType](#AudioEncodingType8+) | 是 | 表示音频编码格式。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------------- | --------------------------- | ---- | -------------------- | +| contentType | [ContentType](#contenttype) | 是 | 表示媒体类型。 | +| usage | [StreamUsage](#streamusage) | 是 | 表示音频流使用类型。 | +| rendererFlags | number | 是 | 表示音频渲染器标志。 | -## AudioRendererInfo8+ +## AudioRendererOptions8+ 描述音频渲染器信息。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------------- | ----------------------------- | ---- | -------------------- | -| contentType | [ContentType](#ContentType8+) | 是 | 表示媒体类型。 | -| usage | [StreamUsage](#StreamUsage) | 是 | 表示音频流使用类型。 | -| rendererFlags | number | 是 | 表示音频渲染器标志。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------------ | --------------------------------------- | ---- | ---------------- | +| streamInfo | [AudioStreamInfo](#audiostreaminfo) | 是 | 表示音频流信息。 | +| rendererInfo | [AudioRendererInfo](#audiorendererinfo) | 是 | 表示播放器信息。 | -## InterruptEvent8+ +## InterruptEvent8+ 描述播放中断时应用程序接收的中断事件。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| --------- | ------------------------------------------- | ---- | ------------------------------------------ | -| eventType | [InterruptType](#InterruptType8+) | 是 | 表示指示中断是开始还是结束。 | -| forceType | [InterruptForceType](#InterruptForceType8+) | 是 | 表示指示操作是由系统执行还是由应用程序执行 | -| hintType | [InterruptHint](#InterruptHint8+) | 是 | 表示系统已采取或应用程序将采取的操作。 | +| 参数名 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------------- | ---- | ------------------------------------------ | +| eventType | [InterruptType](#interrupttype) | 是 | 表示指示中断是开始还是结束。 | +| forceType | [InterruptForceType](#interruptforcetype) | 是 | 表示指示操作是由系统执行还是由应用程序执行 | +| hintType | [InterruptHint](#interrupthint) | 是 | 表示系统已采取或应用程序将采取的操作。 | -## VolumeEvent8+ +## VolumeEvent8+ 描述应用程序在音量发生更改时接收的事件。 @@ -332,16 +334,7 @@ const systemSoundManager = audio.getSystemSoundManager(); | volume | number | 是 | 音量等级,可设置范围通过getMinVolume和getMaxVolume获取。 | | updateUi | boolean | 是 | 在Ui中显示音量变化。 | -## RingtoneOptions8+ -描述铃声选项。 - -**参数:** - -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------- | ---- | -------------- | -| volume | number | 是 | 表示铃声大小。 | -| loop | boolean | 是 | 表示是否循环。 | # AudioManager @@ -753,7 +746,7 @@ setRingerMode(mode: AudioRingMode, callback: AsyncCallback<void>): void | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | -| mode | [AudioRingMode](#audioringmode7+) | 是 | 音频铃声模式。 | +| mode | [AudioRingMode](#section14948916131018) | 是 | 音频铃声模式。 | | callback | AsyncCallback<void> | 是 | 回调返回设置成功或失败。 | **返回值:** @@ -1242,10 +1235,10 @@ on(type: 'volumeChange', callback: Callback): void8+ **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------------------------- | ---- | ------------------------ | -| type | string | 是 | 监听的音量改变事件类型。 | -| callback | Callback<[VolumeEvent](VolumeEvent8+)> | 是 | 音量改变事件回调方法。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------ | ---- | ------------------------ | +| type | string | 是 | 监听的音量改变事件类型。 | +| callback | Callback<[VolumeEvent](#volumevent)> | 是 | 音量改变事件回调方法。 | **返回值:** @@ -1352,9 +1345,9 @@ readonly state: AudioState 8+ **参数:** -| 名称 | 参数型 | 可读 | 可写 | 说明 | -| ----- | ---------- | ---- | ---- | ------------------ | -| state | AudioState | 是 | 否 | 音频播放器的状态。 | +| 名称 | 参数型 | 可读 | 可写 | 说明 | +| ----- | ------------------------- | ---- | ---- | ------------------ | +| state | [AudioState](#audiostate) | 是 | 否 | 音频播放器的状态。 | **示例:** @@ -1838,10 +1831,10 @@ setRenderRate(rate: AudioRendererRate, callback: AsyncCallback): voidvoid< **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------- | ---- | ------------------------ | -| rate | [AudioRendererRate](#AudioRendererRate8+) | 是 | 播放的速率。 | -| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------------- | ---- | ------------------------ | +| rate | [AudioRendererRate](#audiorendererrate) | 是 | 播放的速率。 | +| callback | AsyncCallback | 是 | 用于返回执行结果的回调。 | **返回值:** @@ -1919,9 +1912,9 @@ getRenderRate(): Promise8+ **返回值:** -| 类型 | 说明 | -| -------------------------------------------------- | ------------------------- | -| Promise<[AudioRendererRate](#AudioRendererRate8+)> | Promise回调返回播放速率。 | +| 类型 | 说明 | +| ------------------------------------------------ | ------------------------- | +| Promise<[AudioRendererRate](#audiorendererrate)> | Promise回调返回播放速率。 | **示例:** @@ -1938,10 +1931,10 @@ on(type: 'interrupt', callback: Callback): void8+ **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------------------------- | ---- | ------------------------ | -| type | string | 是 | 被监听的播放事件的类型。 | -| callback | Callback<[InterruptEvent](#InterruptEvent8+)> | 是 | 被监听的中断事件的回调。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------- | ---- | ------------------------ | +| type | string | 是 | 被监听的播放事件的类型。 | +| callback | Callback<[InterruptEvent](#interruptevent)> | 是 | 被监听的中断事件的回调。 | **返回值:** -- Gitee From e7f5b4ceca13f64fbc7a61ad7296f66c439555e0 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Fri, 4 Mar 2022 09:52:27 +0800 Subject: [PATCH 6/9] audioCapture.md --- zh-cn/application-dev/media/audio-capturer.md | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 zh-cn/application-dev/media/audio-capturer.md diff --git a/zh-cn/application-dev/media/audio-capturer.md b/zh-cn/application-dev/media/audio-capturer.md new file mode 100644 index 00000000000..fad46172b40 --- /dev/null +++ b/zh-cn/application-dev/media/audio-capturer.md @@ -0,0 +1,159 @@ +# AudioCapturer音频录制开发指南 + +--- +## ***注意***: + 1. 本文档适用于JavaScript。 +--- +## **摘要** +指南提供 JS 应用程序如何使用 AudioCapturer 来录制音频。 +应用程序可以使用本文档中提供的方法来录制原始音频文件。 + +## **音频录制框架** +AudioCapturer 接口是音频框架中最重要的组件之一。 +### **音频录制:** +AudioCapturer 框架提供了用于获取原始音频文件的方法。 + +## **使用步骤** +这是一个如何使用 AudioCapturer 获取原始音频文件的示例。 +1. 使用 **createAudioCapturer()** 创建一个 AudioCapturer 实例。 音频录制器参数可以在**audioCapturerOptions**中设置。 + 该实例可用于录制、控制和获取录制状态,以及注册通知回调。 + + ``` + var audioStreamInfo = { + samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, + channels: audio.AudioChannel.CHANNEL_1, + sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, + encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW + } + + var audioCapturerInfo = { + source: audio.SourceType.SOURCE_TYPE_MIC, + capturerFlags: 1 + } + + var audioCapturerOptions = { + streamInfo: audioStreamInfo, + capturerInfo: audioCapturerInfo + } + + let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions); + var state = audioRenderer.state; +``` + +2. (可选)使用 **on('stateChange')** API 订阅音频录制器状态更改事件。 + 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 + 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 + + ``` + audioCapturer.on('stateChange',(state) => { + console.info('AudioCapturerLog: Changed State to : ' + state) + switch (state) { + case audio.AudioState.STATE_PREPARED: + console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------'); + console.info('Audio State is : Prepared'); + break; + case audio.AudioState.STATE_RUNNING: + console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------'); + console.info('Audio State is : Running'); + break; + case audio.AudioState.STATE_STOPPED: + console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------'); + console.info('Audio State is : stopped'); + break; + case audio.AudioState.STATE_RELEASED: + console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------'); + console.info('Audio State is : released'); + break; + default: + console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------'); + console.info('Audio State is : invalid'); + break; + } + }); + + ``` + +3. 在 AudioCapturer 实例上调用 **start()** 方法来启动/恢复录制任务。 + 启动完成后,录制器状态将为 STATE_RUNNING。 然后应用程序可以开始读取缓冲区。 + + ``` + await audioCapturer.start(); + if (audioCapturer.state == audio.AudioState.STATE_RUNNING) { + console.info('AudioRecLog: Capturer started'); + } else { + console.info('AudioRecLog: Capturer start failed'); + } + + ``` + +4. 使用 **getBufferSize()** 方法获取要读取的最小缓冲区大小。 + ``` + var bufferSize = await audioCapturer.getBufferSize(); + console.info('AudioRecLog: buffer size: ' + bufferSize); + + ``` + +5. 读取录制器的音频数据并将其转换为字节流。 重复调用**read()** 方法读取数据,直到应用程序想要停止录制。 + + 以下示例显示如何将记录的数据写入文件。 + + ``` + import fileio from '@ohos.fileio'; + + const path = '/data/data/.pulse_dir/capture_js.wav'; + let fd = fileio.openSync(path, 0o102, 0o777); + if (fd !== null) { + console.info('AudioRecLog: file fd created'); + } + else{ + console.info('AudioRecLog: file fd create : FAILED'); + return; + } + + fd = fileio.openSync(path, 0o2002, 0o666); + if (fd !== null) { + console.info('AudioRecLog: file fd opened in append mode'); + } + + var numBuffersToCapture = 150; + while (numBuffersToCapture) { + var buffer = await audioCapturer.read(bufferSize, true); + if (typeof(buffer) == undefined) { + console.info('read buffer failed'); + } else { + var number = fileio.writeSync(fd, buffer); + console.info('AudioRecLog: data written: ' + number); + } + + numBuffersToCapture--; + } + ``` + +6. 录制完成后,调用 AudioCapturer 的 **stop()** 方法 停止录制。 + ``` + await audioCapturer.stop(); + if (audioCapturer.state == audio.AudioState.STATE_STOPPED) { + console.info('AudioRecLog: Capturer stopped'); + } else { + console.info('AudioRecLog: Capturer stop failed'); + } + ``` + +7. 录制任务完成后,调用AudioCapturer的**release()** 方法释放流资源。 + ``` + await audioCapturer.release(); + if (audioCapturer.state == audio.AudioState.STATE_RELEASED) { + console.info('AudioRecLog: Capturer released'); + } else { + console.info('AudioRecLog: Capturer release failed'); + } + ``` + +## **状态检查的重要性** +应用程序开发人员应该记住,AudioCapturer 是基于状态的。 + +也就是说,AudioCapturer 有一个内部状态,应用程序在调用录制器控制 API 时必须始终检查该状态,因为某些操作仅在录制器处于给定状态时才可接受。 +如果应用程序在录制器处于不正确状态时执行操作,系统可能会抛出错误/异常或生成其他未定义的行为。 + +## **其他方法** +更多有用的方法详见[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md),如**getAudioTime**、**getCapturerInfo**和**getStreamInfo**。 -- Gitee From 0c2ebbdf0ffab2b64feda424146740193dd859ca Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Fri, 4 Mar 2022 10:09:34 +0800 Subject: [PATCH 7/9] md --- zh-cn/application-dev/media/audio-capturer.md | 170 +++++++++--------- 1 file changed, 86 insertions(+), 84 deletions(-) diff --git a/zh-cn/application-dev/media/audio-capturer.md b/zh-cn/application-dev/media/audio-capturer.md index fad46172b40..80afce6b497 100644 --- a/zh-cn/application-dev/media/audio-capturer.md +++ b/zh-cn/application-dev/media/audio-capturer.md @@ -19,7 +19,7 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 该实例可用于录制、控制和获取录制状态,以及注册通知回调。 ``` - var audioStreamInfo = { + var audioStreamInfo = { samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, channels: audio.AudioChannel.CHANNEL_1, sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, @@ -38,122 +38,124 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions); var state = audioRenderer.state; -``` + ``` 2. (可选)使用 **on('stateChange')** API 订阅音频录制器状态更改事件。 - 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 - 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 + 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 + 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 ``` - audioCapturer.on('stateChange',(state) => { - console.info('AudioCapturerLog: Changed State to : ' + state) - switch (state) { - case audio.AudioState.STATE_PREPARED: - console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------'); - console.info('Audio State is : Prepared'); - break; - case audio.AudioState.STATE_RUNNING: - console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------'); - console.info('Audio State is : Running'); - break; - case audio.AudioState.STATE_STOPPED: - console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------'); - console.info('Audio State is : stopped'); - break; - case audio.AudioState.STATE_RELEASED: - console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------'); - console.info('Audio State is : released'); - break; - default: - console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------'); - console.info('Audio State is : invalid'); - break; - } - }); - + audioCapturer.on('stateChange',(state) => { + console.info('AudioCapturerLog: Changed State to : ' + state) + switch (state) { + case audio.AudioState.STATE_PREPARED: + console.info('--------CHANGE IN AUDIO STATE----------PREPARED--------------'); + console.info('Audio State is : Prepared'); + break; + case audio.AudioState.STATE_RUNNING: + console.info('--------CHANGE IN AUDIO STATE----------RUNNING--------------'); + console.info('Audio State is : Running'); + break; + case audio.AudioState.STATE_STOPPED: + console.info('--------CHANGE IN AUDIO STATE----------STOPPED--------------'); + console.info('Audio State is : stopped'); + break; + case audio.AudioState.STATE_RELEASED: + console.info('--------CHANGE IN AUDIO STATE----------RELEASED--------------'); + console.info('Audio State is : released'); + break; + default: + console.info('--------CHANGE IN AUDIO STATE----------INVALID--------------'); + console.info('Audio State is : invalid'); + break; + } + }); ``` 3. 在 AudioCapturer 实例上调用 **start()** 方法来启动/恢复录制任务。 启动完成后,录制器状态将为 STATE_RUNNING。 然后应用程序可以开始读取缓冲区。 ``` - await audioCapturer.start(); - if (audioCapturer.state == audio.AudioState.STATE_RUNNING) { - console.info('AudioRecLog: Capturer started'); - } else { - console.info('AudioRecLog: Capturer start failed'); - } - + await audioCapturer.start(); + if (audioCapturer.state == audio.AudioState.STATE_RUNNING) { + console.info('AudioRecLog: Capturer started'); + } else { + console.info('AudioRecLog: Capturer start failed'); + } ``` 4. 使用 **getBufferSize()** 方法获取要读取的最小缓冲区大小。 + ``` - var bufferSize = await audioCapturer.getBufferSize(); - console.info('AudioRecLog: buffer size: ' + bufferSize); - + var bufferSize = await audioCapturer.getBufferSize(); + console.info('AudioRecLog: buffer size: ' + bufferSize); ``` -5. 读取录制器的音频数据并将其转换为字节流。 重复调用**read()** 方法读取数据,直到应用程序想要停止录制。 +5. 读取录制器的音频数据并将其转换为字节流。 重复调用**read()** 方法读取数据,直到应用程序想要停止录制。 - 以下示例显示如何将记录的数据写入文件。 + 以下示例显示如何将记录的数据写入文件。 ``` - import fileio from '@ohos.fileio'; - - const path = '/data/data/.pulse_dir/capture_js.wav'; - let fd = fileio.openSync(path, 0o102, 0o777); - if (fd !== null) { - console.info('AudioRecLog: file fd created'); - } - else{ - console.info('AudioRecLog: file fd create : FAILED'); - return; - } - - fd = fileio.openSync(path, 0o2002, 0o666); - if (fd !== null) { - console.info('AudioRecLog: file fd opened in append mode'); - } - - var numBuffersToCapture = 150; - while (numBuffersToCapture) { - var buffer = await audioCapturer.read(bufferSize, true); - if (typeof(buffer) == undefined) { - console.info('read buffer failed'); - } else { - var number = fileio.writeSync(fd, buffer); - console.info('AudioRecLog: data written: ' + number); - } - - numBuffersToCapture--; - } + import fileio from '@ohos.fileio'; + + const path = '/data/data/.pulse_dir/capture_js.wav'; + let fd = fileio.openSync(path, 0o102, 0o777); + if (fd !== null) { + console.info('AudioRecLog: file fd created'); + } + else{ + console.info('AudioRecLog: file fd create : FAILED'); + return; + } + + fd = fileio.openSync(path, 0o2002, 0o666); + if (fd !== null) { + console.info('AudioRecLog: file fd opened in append mode'); + } + + var numBuffersToCapture = 150; + while (numBuffersToCapture) { + var buffer = await audioCapturer.read(bufferSize, true); + if (typeof(buffer) == undefined) { + console.info('read buffer failed'); + } else { + var number = fileio.writeSync(fd, buffer); + console.info('AudioRecLog: data written: ' + number); + } + + numBuffersToCapture--; + } ``` 6. 录制完成后,调用 AudioCapturer 的 **stop()** 方法 停止录制。 + ``` - await audioCapturer.stop(); - if (audioCapturer.state == audio.AudioState.STATE_STOPPED) { - console.info('AudioRecLog: Capturer stopped'); - } else { - console.info('AudioRecLog: Capturer stop failed'); - } + await audioCapturer.stop(); + if (audioCapturer.state == audio.AudioState.STATE_STOPPED) { + console.info('AudioRecLog: Capturer stopped'); + } else { + console.info('AudioRecLog: Capturer stop failed'); + } ``` 7. 录制任务完成后,调用AudioCapturer的**release()** 方法释放流资源。 + ``` - await audioCapturer.release(); - if (audioCapturer.state == audio.AudioState.STATE_RELEASED) { - console.info('AudioRecLog: Capturer released'); - } else { - console.info('AudioRecLog: Capturer release failed'); - } + await audioCapturer.release(); + if (audioCapturer.state == audio.AudioState.STATE_RELEASED) { + console.info('AudioRecLog: Capturer released'); + } else { + console.info('AudioRecLog: Capturer release failed'); + } ``` - ## **状态检查的重要性** + 应用程序开发人员应该记住,AudioCapturer 是基于状态的。 也就是说,AudioCapturer 有一个内部状态,应用程序在调用录制器控制 API 时必须始终检查该状态,因为某些操作仅在录制器处于给定状态时才可接受。 如果应用程序在录制器处于不正确状态时执行操作,系统可能会抛出错误/异常或生成其他未定义的行为。 ## **其他方法** + 更多有用的方法详见[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md),如**getAudioTime**、**getCapturerInfo**和**getStreamInfo**。 + -- Gitee From a3f5549eff5a92519e04671b30f196cd0706d088 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Fri, 4 Mar 2022 10:26:16 +0800 Subject: [PATCH 8/9] md --- zh-cn/application-dev/media/audio-capturer.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/zh-cn/application-dev/media/audio-capturer.md b/zh-cn/application-dev/media/audio-capturer.md index 6c23363b62a..f6d2e1dde1e 100644 --- a/zh-cn/application-dev/media/audio-capturer.md +++ b/zh-cn/application-dev/media/audio-capturer.md @@ -2,7 +2,7 @@ --- ## ***注意***: - 1. 本文档适用于JavaScript + 1. 本文档适用于JavaScript。 --- ## **摘要** 指南提供 JS 应用程序如何使用 AudioCapturer 来录制音频。 @@ -41,8 +41,8 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 ``` 2. (可选)使用 **on('stateChange')** API 订阅音频录制器状态更改事件。 - 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 - 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 + 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 + 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 ``` audioCapturer.on('stateChange',(state) => { @@ -158,4 +158,3 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 ## **其他方法** 更多有用的方法详见[**js api audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md),如**getAudioTime**、**getCapturerInfo**和**getStreamInfo**。 - -- Gitee From 38cb92138bf426e03451bc5fe4fc52bf59b58f92 Mon Sep 17 00:00:00 2001 From: lwx1059628 Date: Fri, 4 Mar 2022 10:29:33 +0800 Subject: [PATCH 9/9] md --- zh-cn/application-dev/media/audio-capturer.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/zh-cn/application-dev/media/audio-capturer.md b/zh-cn/application-dev/media/audio-capturer.md index f6d2e1dde1e..f04137a3504 100644 --- a/zh-cn/application-dev/media/audio-capturer.md +++ b/zh-cn/application-dev/media/audio-capturer.md @@ -14,8 +14,9 @@ AudioCapturer 接口是音频框架中最重要的组件之一。 AudioCapturer 框架提供了用于获取原始音频文件的方法。 ## **使用步骤** -这是一个如何使用 AudioCapturer 获取原始音频文件的示例。 -1. 使用 **createAudioCapturer()** 创建一个 AudioCapturer 实例。 音频录制器参数可以在**audioCapturerOptions**中设置。 +- 这是一个如何使用 AudioCapturer 获取原始音频文件的示例。 + +1. 使用 **createAudioCapturer()** 创建一个 AudioCapturer 实例。 音频录制器参数可以在**audioCapturerOptions**中设置。 该实例可用于录制、控制和获取录制状态,以及注册通知回调。 ``` @@ -40,7 +41,7 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 var state = audioRenderer.state; ``` -2. (可选)使用 **on('stateChange')** API 订阅音频录制器状态更改事件。 +2. (可选)使用 **on('stateChange')** API 订阅音频录制器状态更改事件。 如果应用程序想根据录制器中的状态更新采取行动,应用程序可以订阅状态更改事件。 应用程序还可以订阅更多事件,例如“markReach”和“periodReach”。 参考[**js-apis-audio.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-audio.md) 更多详细信息。 @@ -72,7 +73,7 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 }); ``` -3. 在 AudioCapturer 实例上调用 **start()** 方法来启动/恢复录制任务。 +3. 在 AudioCapturer 实例上调用 **start()** 方法来启动/恢复录制任务。 启动完成后,录制器状态将为 STATE_RUNNING。 然后应用程序可以开始读取缓冲区。 ``` @@ -84,16 +85,16 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 } ``` -4. 使用 **getBufferSize()** 方法获取要读取的最小缓冲区大小。 +4. 使用 **getBufferSize()** 方法获取要读取的最小缓冲区大小。 ``` var bufferSize = await audioCapturer.getBufferSize(); console.info('AudioRecLog: buffer size: ' + bufferSize); ``` -5. 读取录制器的音频数据并将其转换为字节流。 重复调用**read()** 方法读取数据,直到应用程序想要停止录制。 +5. 读取录制器的音频数据并将其转换为字节流。 重复调用**read()** 方法读取数据,直到应用程序想要停止录制。 - 以下示例显示如何将记录的数据写入文件。 + 以下示例显示如何将记录的数据写入文件。 ``` import fileio from '@ohos.fileio'; @@ -127,7 +128,7 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 } ``` -6. 录制完成后,调用 AudioCapturer 的 **stop()** 方法 停止录制。 +6. 录制完成后,调用 AudioCapturer 的 **stop()** 方法 停止录制。 ``` await audioCapturer.stop(); @@ -138,7 +139,7 @@ AudioCapturer 框架提供了用于获取原始音频文件的方法。 } ``` -7. 录制任务完成后,调用AudioCapturer的**release()** 方法释放流资源。 +7. 录制任务完成后,调用AudioCapturer的**release()** 方法释放流资源。 ``` await audioCapturer.release(); -- Gitee