privatetestFdNumber;// Used to save the FD address.
// Set the callbacks related to audio recording.
setCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
audioRecorder.start();// Call the start API to start recording and trigger the 'start' event callback.
lettestFdNumber;
functionSetCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
});
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
audioRecorder.pause();// Call the pause API to pause recording and trigger the 'pause' event callback.
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
// The Record button is changed to the pausable state.
});
audioRecorder.on('pause',()=>{// Set the 'pause' event callback.
console.log('audio recorder pause success');
audioRecorder.resume();// Call the resume API to resume recording and trigger the 'resume' event callback.
audioRecorder.on('pause',()=>{// Set the 'pause' event callback.
console.log('audio recorder pause success');
// The Record button is changed to the recordable state.
});
audioRecorder.on('resume',()=>{// Set the 'resume' event callback.
console.log('audio recorder resume success');
audioRecorder.stop();// Call the stop API to stop recording and trigger the 'stop' event callback.
audioRecorder.on('resume',()=>{// Set the 'resume' event callback.
console.log('audio recorder resume success');
// The Record button is changed to the pausable state.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
audioRecorder.reset();// Call the reset API to reset the recorder and trigger the 'reset' event callback.
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.on('reset',()=>{// Set the 'reset' event callback.
console.log('audio recorder reset success');
audioRecorder.release();// Call the release API to release resources and trigger the 'release' event callback.
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
audioRecorder=undefined;
audioRecorder.on('reset',()=>{// Set the 'reset' event callback.
console.log('audio recorder reset success');
// You need to reset the recording parameters for another recording.
});
audioRecorder.on('error',(error)=>{// Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
audioRecorder.on('error',(error)=>{// Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
});
}
}
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Video/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncgetFd(pathName){
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Movies/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
letfileKeyObj=mediaLibrary.FileKey;
...
...
@@ -75,37 +72,49 @@ export class AudioRecorderDemo {
privatetestFdNumber;// Used to save the FD address.
// Set the callbacks related to audio recording.
setCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
audioRecorder.start();// Call the start API to start recording and trigger the 'start' event callback.
});
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
audioRecorder.stop();// Call the stop API to stop recording and trigger the 'stop' event callback.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
audioRecorder.release();// Call the release API to release resources and trigger the 'release' event callback.
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
audioRecorder=undefined;
});
audioRecorder.on('error',(error)=>{// Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
lettestFdNumber;
functionSetCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
});
}
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
// The Record button is changed to the pausable state.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
});
}
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Video/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncgetFd(pathName){
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Movies/01.mp3.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
letfileKeyObj=mediaLibrary.FileKey;
...
...
@@ -153,43 +154,41 @@ export class AudioRecorderDemo {
awaitthis.getFd('01.mp3');// Call the getFd method to obtain the FD address of the file to be recorded.
// 3. Set the recording parameters.
letaudioRecorderConfig={
audioEncodeBitRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
uri:this.testFdNumber,// testFdNumber is generated by getFd.
location:{latitude:30,longitude:130},
audioEncoderMime:media.CodecMimeType.AUDIO_AAC,
fileFormat:media.ContainerFormatType.CFT_MPEG_4A,
}
audioRecorder.prepare(audioRecorderConfig);// Call the prepare method to trigger the 'prepare' event callback.
}
}
```
## Samples
The following samples are provided to help you better understand how to develop audio recording:
-[`Recorder`: Recorder (eTS, API 8)](https://gitee.com/openharmony/app_samples/tree/master/media/Recorder)
-[`eTsAudioPlayer`: Audio Player (eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)