提交 eb65b5aa 编写于 作者: J jiao_yanlin

Sample code problem modification

Signed-off-by: Njiao_yanlin <jiaoyanlin@huawei.com>
上级 06d61182
...@@ -21,19 +21,28 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -21,19 +21,28 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
## 约束与限制 ## 约束与限制
开发者在进行音频数据采集功能开发前,需要先对所开发的应用配置麦克风权限(ohos.permission.MICROPHONE),权限配置相关内容可参考:[访问控制授权申请指导](../security/accesstoken-guidelines.md) 开发者在进行音频数据采集功能开发前,需要先对所开发的应用配置麦克风权限(ohos.permission.MICROPHONE),配置方式请参见[访问控制授权申请](../security/accesstoken-guidelines.md#配置文件权限声明)
## 开发指导 ## 开发指导
详细API含义可参考:[音频管理API文档AudioCapturer](../reference/apis/js-apis-audio.md#audiocapturer8) 详细API含义可参考:[音频管理API文档AudioCapturer](../reference/apis/js-apis-audio.md#audiocapturer8)
1. 使用createAudioCapturer()创建一个AudioCapturer实例。 1. 使用createAudioCapturer()创建一个全局的AudioCapturer实例。
在audioCapturerOptions中设置音频采集器的相关参数。该实例可用于音频采集、控制和获取采集状态,以及注册通知回调。 在audioCapturerOptions中设置音频采集器的相关参数。该实例可用于音频采集、控制和获取采集状态,以及注册通知回调。
```js ```js
import audio from '@ohos.multimedia.audio'; import audio from '@ohos.multimedia.audio';
import fs from '@ohos.file.fs'; //便于步骤3 read函数调用
//音频渲染相关接口自测试
@Entry
@Component
struct AudioRenderer {
@State message: string = 'Hello World'
private audioCapturer : audio.AudioCapturer; //供全局调用
async initAudioCapturer(){
let audioStreamInfo = { let audioStreamInfo = {
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100, samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_44100,
channels: audio.AudioChannel.CHANNEL_1, channels: audio.AudioChannel.CHANNEL_1,
...@@ -51,8 +60,9 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -51,8 +60,9 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
capturerInfo: audioCapturerInfo capturerInfo: audioCapturerInfo
} }
let audioCapturer = await audio.createAudioCapturer(audioCapturerOptions); this.audioCapturer = await audio.createAudioCapturer(audioCapturerOptions);
console.log('AudioRecLog: Create audio capturer success.'); console.log('AudioRecLog: Create audio capturer success.');
}
``` ```
2. 调用start()方法来启动/恢复采集任务。 2. 调用start()方法来启动/恢复采集任务。
...@@ -60,25 +70,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -60,25 +70,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
启动完成后,采集器状态将变更为STATE_RUNNING,然后应用可以开始读取缓冲区。 启动完成后,采集器状态将变更为STATE_RUNNING,然后应用可以开始读取缓冲区。
```js ```js
import audio from '@ohos.multimedia.audio'; async startCapturer() {
let state = this.audioCapturer.state;
async function startCapturer() {
let state = audioCapturer.state;
// Capturer start时的状态应该是STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一. // Capturer start时的状态应该是STATE_PREPARED、STATE_PAUSED和STATE_STOPPED之一.
if (state != audio.AudioState.STATE_PREPARED || state != audio.AudioState.STATE_PAUSED || if (state == audio.AudioState.STATE_PREPARED || state == audio.AudioState.STATE_PAUSED ||
state != audio.AudioState.STATE_STOPPED) { state == audio.AudioState.STATE_STOPPED) {
console.info('Capturer is not in a correct state to start'); await this.audioCapturer.start();
return; state = this.audioCapturer.state;
}
await audioCapturer.start();
state = audioCapturer.state;
if (state == audio.AudioState.STATE_RUNNING) { if (state == audio.AudioState.STATE_RUNNING) {
console.info('AudioRecLog: Capturer started'); console.info('AudioRecLog: Capturer started');
} else { } else {
console.error('AudioRecLog: Capturer start failed'); console.error('AudioRecLog: Capturer start failed');
} }
} }
}
``` ```
3. 读取采集器的音频数据并将其转换为字节流。重复调用read()方法读取数据,直到应用准备停止采集。 3. 读取采集器的音频数据并将其转换为字节流。重复调用read()方法读取数据,直到应用准备停止采集。
...@@ -86,17 +91,15 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -86,17 +91,15 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
参考以下示例,将采集到的数据写入文件。 参考以下示例,将采集到的数据写入文件。
```js ```js
import fs from '@ohos.file.fs'; async readData(){
let state = this.audioCapturer.state;
let state = audioCapturer.state;
// 只有状态为STATE_RUNNING的时候才可以read. // 只有状态为STATE_RUNNING的时候才可以read.
if (state != audio.AudioState.STATE_RUNNING) { if (state != audio.AudioState.STATE_RUNNING) {
console.info('Capturer is not in a correct state to read'); console.info('Capturer is not in a correct state to read');
return; return;
} }
const path = '/data/data/.pulse_dir/capture_js.wav'; // 采集到的音频文件存储路径 const path = '/data/data/.pulse_dir/capture_js.wav'; // 采集到的音频文件存储路径
let file = fs.openSync(filePath, 0o2); let file = fs.openSync(path, 0o2);
let fd = file.fd; let fd = file.fd;
if (file !== null) { if (file !== null) {
console.info('AudioRecLog: file created'); console.info('AudioRecLog: file created');
...@@ -104,16 +107,14 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -104,16 +107,14 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
console.info('AudioRecLog: file create : FAILED'); console.info('AudioRecLog: file create : FAILED');
return; return;
} }
if (fd !== null) { if (fd !== null) {
console.info('AudioRecLog: file fd opened in append mode'); console.info('AudioRecLog: file fd opened in append mode');
} }
let numBuffersToCapture = 150; // 循环写入150次 let numBuffersToCapture = 150; // 循环写入150次
let count = 0; let count = 0;
while (numBuffersToCapture) { while (numBuffersToCapture) {
let bufferSize = await audioCapturer.getBufferSize(); this.bufferSize = await this.audioCapturer.getBufferSize();
let buffer = await audioCapturer.read(bufferSize, true); let buffer = await this.audioCapturer.read(this.bufferSize, true);
let options = { let options = {
offset: count * this.bufferSize, offset: count * this.bufferSize,
length: this.bufferSize length: this.bufferSize
...@@ -127,22 +128,23 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -127,22 +128,23 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
numBuffersToCapture--; numBuffersToCapture--;
count++; count++;
} }
}
``` ```
4. 采集完成后,调用stop方法,停止录制。 4. 采集完成后,调用stop方法,停止录制。
```js ```js
async function StopCapturer() { async StopCapturer() {
let state = audioCapturer.state; let state = this.audioCapturer.state;
// 只有采集器状态为STATE_RUNNING或STATE_PAUSED的时候才可以停止 // 只有采集器状态为STATE_RUNNING或STATE_PAUSED的时候才可以停止
if (state != audio.AudioState.STATE_RUNNING && state != audio.AudioState.STATE_PAUSED) { if (state != audio.AudioState.STATE_RUNNING && state != audio.AudioState.STATE_PAUSED) {
console.info('AudioRecLog: Capturer is not running or paused'); console.info('AudioRecLog: Capturer is not running or paused');
return; return;
} }
await audioCapturer.stop(); await this.audioCapturer.stop();
state = audioCapturer.state; state = this.audioCapturer.state;
if (state == audio.AudioState.STATE_STOPPED) { if (state == audio.AudioState.STATE_STOPPED) {
console.info('AudioRecLog: Capturer stopped'); console.info('AudioRecLog: Capturer stopped');
} else { } else {
...@@ -154,17 +156,17 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -154,17 +156,17 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
5. 任务结束,调用release()方法释放相关资源。 5. 任务结束,调用release()方法释放相关资源。
```js ```js
async function releaseCapturer() { async releaseCapturer() {
let state = audioCapturer.state; let state = this.audioCapturer.state;
// 采集器状态不是STATE_RELEASED或STATE_NEW状态,才能release // 采集器状态不是STATE_RELEASED或STATE_NEW状态,才能release
if (state == audio.AudioState.STATE_RELEASED || state == audio.AudioState.STATE_NEW) { if (state == audio.AudioState.STATE_RELEASED || state == audio.AudioState.STATE_NEW) {
console.info('AudioRecLog: Capturer already released'); console.info('AudioRecLog: Capturer already released');
return; return;
} }
await audioCapturer.release(); await this.audioCapturer.release();
state = audioCapturer.state; state = this.audioCapturer.state;
if (state == audio.AudioState.STATE_RELEASED) { if (state == audio.AudioState.STATE_RELEASED) {
console.info('AudioRecLog: Capturer released'); console.info('AudioRecLog: Capturer released');
} else { } else {
...@@ -178,23 +180,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -178,23 +180,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
通过以下代码,可以获取采集器的相关信息。 通过以下代码,可以获取采集器的相关信息。
```js ```js
async getAudioCapturerInfo(){
// 获取当前采集器状态 // 获取当前采集器状态
let state = audioCapturer.state; let state = this.audioCapturer.state;
// 获取采集器信息 // 获取采集器信息
let audioCapturerInfo : audio.AuduioCapturerInfo = await audioCapturer.getCapturerInfo(); let audioCapturerInfo : audio.AudioCapturerInfo = await this.audioCapturer.getCapturerInfo();
// 获取音频流信息 // 获取音频流信息
let audioStreamInfo : audio.AudioStreamInfo = await audioCapturer.getStreamInfo(); let audioStreamInfo : audio.AudioStreamInfo = await this.audioCapturer.getStreamInfo();
// 获取音频流ID // 获取音频流ID
let audioStreamId : number = await audioCapturer.getAudioStreamId(); let audioStreamId : number = await this.audioCapturer.getAudioStreamId();
// 获取纳秒形式的Unix时间戳 // 获取纳秒形式的Unix时间戳
let audioTime : number = await audioCapturer.getAudioTime(); let audioTime : number = await this.audioCapturer.getAudioTime();
// 获取合理的最小缓冲区大小 // 获取合理的最小缓冲区大小
let bufferSize : number = await audioCapturer.getBufferSize(); let bufferSize : number = await this.audioCapturer.getBufferSize();
}
``` ```
7. (可选)使用on('markReach')方法订阅采集器标记到达事件,使用off('markReach')取消订阅事件。 7. (可选)使用on('markReach')方法订阅采集器标记到达事件,使用off('markReach')取消订阅事件。
...@@ -202,12 +201,13 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -202,12 +201,13 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
注册markReach监听后,当采集器采集的帧数到达设定值时,会触发回调并返回设定的值。 注册markReach监听后,当采集器采集的帧数到达设定值时,会触发回调并返回设定的值。
```js ```js
audioCapturer.on('markReach', (reachNumber) => { async markReach(){
this.audioCapturer.on('markReach', 10, (reachNumber) => {
console.info('Mark reach event Received'); console.info('Mark reach event Received');
console.info(`The Capturer reached frame: ${reachNumber}`); console.info(`The Capturer reached frame: ${reachNumber}`);
}); });
this.audioCapturer.off('markReach'); // 取消markReach事件的订阅,后续将无法监听到“标记到达”事件
audioCapturer.off('markReach'); // 取消markReach事件的订阅,后续将无法监听到“标记到达”事件 }
``` ```
8. (可选)使用on('periodReach')方法订阅采集器区间标记到达事件,使用off('periodReach')取消订阅事件。 8. (可选)使用on('periodReach')方法订阅采集器区间标记到达事件,使用off('periodReach')取消订阅事件。
...@@ -215,18 +215,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -215,18 +215,20 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
注册periodReach监听后,**每当**采集器采集的帧数到达设定值时,会触发回调并返回设定的值。 注册periodReach监听后,**每当**采集器采集的帧数到达设定值时,会触发回调并返回设定的值。
```js ```js
audioCapturer.on('periodReach', (reachNumber) => { async periodReach(){
this.audioCapturer.on('periodReach', 10, (reachNumber) => {
console.info('Period reach event Received'); console.info('Period reach event Received');
console.info(`In this period, the Capturer reached frame: ${reachNumber}`); console.info(`In this period, the Capturer reached frame: ${reachNumber}`);
}); });
this.audioCapturer.off('periodReach'); // 取消periodReach事件的订阅,后续将无法监听到“区间标记到达”事件
audioCapturer.off('periodReach'); // 取消periodReach事件的订阅,后续将无法监听到“区间标记到达”事件 }
``` ```
9. 如果应用需要在采集器状态更新时进行一些操作,可以订阅该事件,当采集器状态更新时,会受到一个包含有事件类型的回调。 9. 如果应用需要在采集器状态更新时进行一些操作,可以订阅该事件,当采集器状态更新时,会受到一个包含有事件类型的回调。
```js ```js
audioCapturer.on('stateChange', (state) => { async stateChange(){
this.audioCapturer.on('stateChange', (state) => {
console.info(`AudioCapturerLog: Changed State to : ${state}`) console.info(`AudioCapturerLog: Changed State to : ${state}`)
switch (state) { switch (state) {
case audio.AudioState.STATE_PREPARED: case audio.AudioState.STATE_PREPARED:
...@@ -251,4 +253,5 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以 ...@@ -251,4 +253,5 @@ AudioCapturer提供了用于获取原始音频文件的方法。开发者可以
break; break;
} }
}); });
}
``` ```
\ No newline at end of file
...@@ -4533,15 +4533,15 @@ let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; ...@@ -4533,15 +4533,15 @@ let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
let stat = await fs.stat(path); let stat = await fs.stat(path);
let buf = new ArrayBuffer(bufferSize); let buf = new ArrayBuffer(bufferSize);
let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1); let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1);
for (let i = 0;i < len; i++) { for (let i = 0;i < len; i++) {
let options = { let options = {
offset: i * this.bufferSize, offset: i * bufferSize,
length: this.bufferSize length: bufferSize
} }
let readsize = await fs.read(file.fd, buf, options) let readsize = await fs.read(file.fd, buf, options)
let writeSize = await new Promise((resolve,reject)=>{ let writeSize = await new Promise((resolve,reject)=>{
this.audioRenderer.write(buf,(err,writeSize)=>{ audioRenderer.write(buf,(err,writeSize)=>{
if(err){ if(err){
reject(err) reject(err)
}else{ }else{
...@@ -4586,15 +4586,15 @@ let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; ...@@ -4586,15 +4586,15 @@ let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY); let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
let stat = await fs.stat(path); let stat = await fs.stat(path);
let buf = new ArrayBuffer(bufferSize); let buf = new ArrayBuffer(bufferSize);
let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1); let len = stat.size % bufferSize == 0 ? Math.floor(stat.size / bufferSize) : Math.floor(stat.size / bufferSize + 1);
for (let i = 0;i < len; i++) { for (let i = 0;i < len; i++) {
let options = { let options = {
offset: i * this.bufferSize, offset: i * bufferSize,
length: this.bufferSize length: bufferSize
} }
let readsize = await fs.read(file.fd, buf, options) let readsize = await fs.read(file.fd, buf, options)
try{ try{
let writeSize = await this.audioRenderer.write(buf); let writeSize = await audioRenderer.write(buf);
} catch(err) { } catch(err) {
console.error(`audioRenderer.write err: ${err}`); console.error(`audioRenderer.write err: ${err}`);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册