未验证 提交 e49118bc 编写于 作者: S shuboxu 提交者: Gitee

update zh-cn/application-dev/media/using-avrecorder-for-recording.md.

Signed-off-by: Nshuboxu <xushubo1@huawei.com>
上级 7fd3e9ad
...@@ -20,11 +20,11 @@ ...@@ -20,11 +20,11 @@
```ts ```ts
import media from '@ohos.multimedia.media'; import media from '@ohos.multimedia.media';
let avRecorder = undefined; let avRecorder: media.AVRecorder;
media.createAVRecorder().then((recorder) => { media.createAVRecorder().then((recorder: media.AVRecorder) => {
avRecorder = recorder; avRecorder = recorder;
}, (err) => { }, (error: Error) => {
console.error(`Invoke createAVRecorder failed, code is ${err.code}, message is ${err.message}`); console.error(`createAVRecorder failed`);
}) })
``` ```
...@@ -37,13 +37,13 @@ ...@@ -37,13 +37,13 @@
```ts ```ts
// 状态上报回调函数 // 状态上报回调函数
avRecorder.on('stateChange', (state, reason) => { avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
console.log(`current state is ${state}`); 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}`); console.error(`avRecorder failed, code is ${err.code}, message is ${err.message}`);
}) })
``` ```
...@@ -61,21 +61,33 @@ ...@@ -61,21 +61,33 @@
```ts ```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, // 音频比特率 audioBitrate: 100000, // 音频比特率
audioChannels: 2, // 音频声道数 audioChannels: 2, // 音频声道数
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
audioSampleRate: 48000, // 音频采样率 audioSampleRate: 48000, // 音频采样率
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
} }
let avConfig = { let avConfig: AVConfig = {
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风 audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
profile: avProfile, profile: avProfile,
url: 'fd://35', // 参考应用文件访问与管理中的开发示例获取创建的音频文件fd填入此处 url: 'fd://35', // 参考应用文件访问与管理中的开发示例获取创建的音频文件fd填入此处
} }
avRecorder.prepare(avConfig).then(() => { avRecorder.prepare(avConfig).then(() => {
console.log('Invoke prepare succeeded.'); console.log('Invoke prepare succeeded.');
}, (err) => { }, (err: BusinessError) => {
console.error(`Invoke prepare failed, code is ${err.code}, message is ${err.message}`); console.error(`Invoke prepare failed, code is ${err.code}, message is ${err.message}`);
}) })
``` ```
...@@ -99,17 +111,30 @@ ...@@ -99,17 +111,30 @@
```ts ```ts
import media from '@ohos.multimedia.media'; 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 { export class AudioRecorderDemo {
private avRecorder; private avRecorder: media.AVRecorder;
private avProfile = { private avProfile: AVProfile = {
audioBitrate: 100000, // 音频比特率 audioBitrate: 100000, // 音频比特率
audioChannels: 2, // 音频声道数 audioChannels: 2, // 音频声道数
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
audioSampleRate: 48000, // 音频采样率 audioSampleRate: 48000, // 音频采样率
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
}; };
private avConfig = { private avConfig: AVConfig = {
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风 audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
profile: this.avProfile, profile: this.avProfile,
url: 'fd://35', // 参考应用文件访问与管理开发示例新建并读写一个文件 url: 'fd://35', // 参考应用文件访问与管理开发示例新建并读写一个文件
...@@ -118,11 +143,11 @@ export class AudioRecorderDemo { ...@@ -118,11 +143,11 @@ export class AudioRecorderDemo {
// 注册audioRecorder回调函数 // 注册audioRecorder回调函数
setAudioRecorderCallback() { setAudioRecorderCallback() {
// 状态机变化回调函数 // 状态机变化回调函数
this.avRecorder.on('stateChange', (state, reason) => { this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
console.log(`AudioRecorder current state is ${state}`); 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}`); console.error(`AudioRecorder failed, code is ${err.code}, message is ${err.message}`);
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册