From e49118bc42cb2ff5fe5601e8e296b71dd45a5ffb Mon Sep 17 00:00:00 2001 From: shuboxu Date: Fri, 25 Aug 2023 03:46:43 +0000 Subject: [PATCH] update zh-cn/application-dev/media/using-avrecorder-for-recording.md. Signed-off-by: shuboxu --- .../media/using-avrecorder-for-recording.md | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/zh-cn/application-dev/media/using-avrecorder-for-recording.md b/zh-cn/application-dev/media/using-avrecorder-for-recording.md index 98c9c4633e..b97bd3dc9e 100644 --- a/zh-cn/application-dev/media/using-avrecorder-for-recording.md +++ b/zh-cn/application-dev/media/using-avrecorder-for-recording.md @@ -20,11 +20,11 @@ ```ts import media from '@ohos.multimedia.media'; - let avRecorder = undefined; - media.createAVRecorder().then((recorder) => { + let avRecorder: media.AVRecorder; + media.createAVRecorder().then((recorder: media.AVRecorder) => { avRecorder = recorder; - }, (err) => { - console.error(`Invoke createAVRecorder failed, code is ${err.code}, message is ${err.message}`); + }, (error: Error) => { + console.error(`createAVRecorder failed`); }) ``` @@ -37,13 +37,13 @@ ```ts // 状态上报回调函数 - avRecorder.on('stateChange', (state, reason) => { + avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => { console.log(`current state is ${state}`); // 用户可以在此补充状态发生切换后想要进行的动作 }) // 错误上报回调函数 - avRecorder.on('error', (err) => { + avRecorder.on('error', (err: BusinessError) => { console.error(`avRecorder failed, code is ${err.code}, message is ${err.message}`); }) ``` @@ -61,21 +61,33 @@ ```ts - let avProfile = { + class AVProfile { + audioBitrate: number; + audioChannels: number; + audioCodec: media.CodecMimeType; + audioSampleRate: number; + fileFormat: media.ContainerFormatType; + } + class AVConfig { + audioSourceType: media.AudioSourceType; + profile: AVProfile; + url: string + } + let avProfile: AVProfile = { audioBitrate: 100000, // 音频比特率 audioChannels: 2, // 音频声道数 audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac audioSampleRate: 48000, // 音频采样率 fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a } - let avConfig = { + let avConfig: AVConfig = { audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风 profile: avProfile, url: 'fd://35', // 参考应用文件访问与管理中的开发示例获取创建的音频文件fd填入此处 } avRecorder.prepare(avConfig).then(() => { console.log('Invoke prepare succeeded.'); - }, (err) => { + }, (err: BusinessError) => { console.error(`Invoke prepare failed, code is ${err.code}, message is ${err.message}`); }) ``` @@ -99,17 +111,30 @@ ```ts import media from '@ohos.multimedia.media'; - +import { BusinessError } from '@ohos.base'; + +class AVProfile { + audioBitrate: number; + audioChannels: number; + audioCodec: media.CodecMimeType; + audioSampleRate: number; + fileFormat: media.ContainerFormatType; +} +class AVConfig { + audioSourceType: media.AudioSourceType; + profile: AVProfile; + url: string +} export class AudioRecorderDemo { - private avRecorder; - private avProfile = { + private avRecorder: media.AVRecorder; + private avProfile: AVProfile = { audioBitrate: 100000, // 音频比特率 audioChannels: 2, // 音频声道数 audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac audioSampleRate: 48000, // 音频采样率 fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a }; - private avConfig = { + private avConfig: AVConfig = { audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风 profile: this.avProfile, url: 'fd://35', // 参考应用文件访问与管理开发示例新建并读写一个文件 @@ -118,11 +143,11 @@ export class AudioRecorderDemo { // 注册audioRecorder回调函数 setAudioRecorderCallback() { // 状态机变化回调函数 - this.avRecorder.on('stateChange', (state, reason) => { + this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => { console.log(`AudioRecorder current state is ${state}`); }) // 错误上报回调函数 - this.avRecorder.on('error', (err) => { + this.avRecorder.on('error', (err: BusinessError) => { console.error(`AudioRecorder failed, code is ${err.code}, message is ${err.message}`); }) } -- GitLab