@@ -21,7 +21,7 @@ This following figure shows the audio capturer state transitions.
## Constraints
Before developing the audio data collection feature, configure the **ohos.permission.MICROPHONE** permission for your application. For details about permission configuration, see [Permission Application Guide](../security/accesstoken-guidelines.md).
Before developing the audio data collection feature, configure the **ohos.permission.MICROPHONE** permission for your application. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md).
## How to Develop
...
...
@@ -72,7 +72,7 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
}
awaitaudioCapturer.start();
letstate=audioCapturer.state;
state=audioCapturer.state;
if(state==audio.AudioState.STATE_RUNNING){
console.info('AudioRecLog: Capturer started');
}else{
...
...
@@ -86,7 +86,7 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
The following example shows how to write recorded data into a file.
```js
importfileiofrom'@ohos.fileio';
importfsfrom'@ohos.file.fs';
letstate=audioCapturer.state;
// The read operation can be performed only when the state is STATE_RUNNING.
...
...
@@ -96,31 +96,36 @@ For details about the APIs, see [AudioCapturer in Audio Management](../reference
}
constpath='/data/data/.pulse_dir/capture_js.wav';// Path for storing the collected audio file.
@@ -38,7 +38,7 @@ For details about the **src** types supported by **AudioPlayer**, see the [src a
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
// Print the stream track information.
functionprintfDescription(obj){
...
...
@@ -112,14 +112,8 @@ async function audioPlayerDemo() {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber;
console.info('open fd success fd is'+fdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letfile=awaitfs.open(path);
fdPath=fdPath+''+file.fd;
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
```
...
...
@@ -128,7 +122,7 @@ async function audioPlayerDemo() {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassAudioDemo{
// Set the player callbacks.
...
...
@@ -154,14 +148,8 @@ export class AudioDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber;
console.info('open fd success fd is'+fdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letfile=awaitfs.open(path);
fdPath=fdPath+''+file.fd;
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
}
...
...
@@ -171,7 +159,7 @@ export class AudioDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassAudioDemo{
// Set the player callbacks.
...
...
@@ -202,14 +190,8 @@ export class AudioDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\02.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letnextpath=pathDir+'/02.mp3'
awaitfileIO.open(nextpath).then((fdNumber)=>{
nextFdPath=nextFdPath+''+fdNumber;
console.info('open fd success fd is'+nextFdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letnextFile=awaitfs.open(nextpath);
nextFdPath=nextFdPath+''+nextFile.fd;
audioPlayer.src=nextFdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
...
...
@@ -220,14 +202,8 @@ export class AudioDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber;
console.info('open fd success fd is'+fdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letfile=awaitfs.open(path);
fdPath=fdPath+''+file.fd;
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
}
...
...
@@ -237,7 +213,7 @@ export class AudioDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassAudioDemo{
// Set the player callbacks.
...
...
@@ -259,14 +235,8 @@ export class AudioDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el2/100/base/ohos.acts.multimedia.audio.audioplayer/haps/entry/files" command.
letpath=pathDir+'/01.mp3'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber;
console.info('open fd success fd is'+fdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letfile=awaitfs.open(path);
fdPath=fdPath+''+file.fd;
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
@@ -34,8 +34,7 @@ For details about the APIs, see [AudioRenderer in Audio Management](../reference
1. Use **createAudioRenderer()** to create an **AudioRenderer** instance.
Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. This instance is used to render audio, control and obtain the rendering status, and register a callback for notification.
Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. This instance is used to render audio, control and obtain the rendering status, and register a callback for notification.
```js
importaudiofrom'@ohos.multimedia.audio';
...
...
@@ -82,15 +81,15 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
}
}
```
The renderer state will be **STATE_RUNNING** once the audio renderer is started. The application can then begin reading buffers.
The renderer state will be **STATE_RUNNING** once the audio renderer is started. The application can then begin reading buffers.
3. Call **write()** to write data to the buffer.
Read the audio data to be played to the buffer. Call **write()** repeatedly to write the data to the buffer.
```js
importfileiofrom'@ohos.fileio';
importfsfrom'@ohos.file.fs';
importaudiofrom'@ohos.multimedia.audio';
asyncfunctionwriteBuffer(buf){
...
...
@@ -109,35 +108,33 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
// Set a proper buffer size for the audio renderer. You can also select a buffer of another size.
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath=dir+'/file_example_WAV_2MG.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
console.info(`file path: ${path}`);
letss=fileio.createStreamSync(path,'r');
consttotalSize=fileio.statSync(path).size;// Size of the file to render.
letdiscardHeader=newArrayBuffer(bufferSize);
ss.readSync(discardHeader);
letrlen=0;
rlen+=bufferSize;
letid=setInterval(()=>{
if(audioRenderer.state==audio.AudioState.STATE_RELEASED){// The rendering stops if the audio renderer is in the STATE_RELEASED state.
if(rlen>=totalSize){// The rendering stops if the file finishes reading.
ss.closeSync();
awaitaudioRenderer.stop();
clearInterval(id);
}
constfilePath=dir+'/file_example_WAV_2MG.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
@@ -424,35 +421,31 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath1=dir+'/music001_48000_32_1.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/music001_48000_32_1.wav
console.info(`audioRender1 file path: ${path1}`);
letss1=awaitfileio.createStream(path1,'r');
consttotalSize1=fileio.statSync(path1).size;// Size of the file to render.
@@ -499,36 +492,32 @@ Set parameters of the **AudioRenderer** instance in **audioRendererOptions**. Th
// 2.6 Read the original audio data file.
letdir=globalThis.fileDir;// You must use the sandbox path.
constpath2=dir+'/music002_48000_32_1.wav';// The file to render is in the following path: /data/storage/el2/base/haps/entry/files/music002_48000_32_1.wav
@@ -104,7 +104,7 @@ The full playback process includes creating an instance, setting resources, sett
```js
importmediafrom'@ohos.multimedia.media'
importaudiofrom'@ohos.multimedia.audio';
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
constTAG='AVPlayerDemo:'
exportclassAVPlayerDemo{
...
...
@@ -223,14 +223,8 @@ export class AVPlayerDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
letpath=pathDir+'/H264_AAC.mp4'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber
console.info('open fd success fd is'+fdPath)
},(err)=>{
console.info('open fd failed err is'+err)
}).catch((err)=>{
console.info('open fd failed err is'+err)
});
letfile=awaitfs.open(path)
fdPath=fdPath+''+file.fd
this.avPlayer.url=fdPath
}
}
...
...
@@ -240,7 +234,7 @@ export class AVPlayerDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
constTAG='AVPlayerDemo:'
exportclassAVPlayerDemo{
...
...
@@ -280,7 +274,7 @@ export class AVPlayerDemo {
break;
case'stopped':// This state is reported upon a successful callback of stop().
console.info(TAG+'state stopped called')
this.avPlayer.reset()// Call reset() to initialize the AVPlayer state.
this.avPlayer.release()// Call reset() to initialize the AVPlayer state.
break;
case'released':
console.info(TAG+'state released called')
...
...
@@ -302,24 +296,18 @@ export class AVPlayerDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
letpath=pathDir+'/H264_AAC.mp4'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber
console.info('open fd success fd is'+fdPath)
},(err)=>{
console.info('open fd failed err is'+err)
}).catch((err)=>{
console.info('open fd failed err is'+err)
});
letfile=awaitfs.open(path)
fdPath=fdPath+''+file.fd
this.avPlayer.url=fdPath
}
}
```
### Switching to the Next Video Clip
### Looping a Song
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
constTAG='AVPlayerDemo:'
exportclassAVPlayerDemo{
...
...
@@ -362,7 +350,7 @@ export class AVPlayerDemo {
break;
case'stopped':// This state is reported upon a successful callback of stop().
console.info(TAG+'state stopped called')
this.avPlayer.reset()// Call reset() to initialize the AVPlayer state.
this.avPlayer.release()// Call reset() to initialize the AVPlayer state.
break;
case'released':
console.info(TAG+'state released called')
...
...
@@ -393,23 +381,17 @@ export class AVPlayerDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
letpath=pathDir+'/H264_AAC.mp4'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber
console.info('open fd success fd is'+fdPath)
},(err)=>{
console.info('open fd failed err is'+err)
}).catch((err)=>{
console.info('open fd failed err is'+err)
});
letfile=awaitfs.open(path)
fdPath=fdPath+''+file.fd
this.avPlayer.url=fdPath
}
}
```
### Looping a Song
### Switching to the Next Video Clip
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
constTAG='AVPlayerDemo:'
exportclassAVPlayerDemo{
...
...
@@ -422,14 +404,8 @@ export class AVPlayerDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_MP3.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
letpath=pathDir+'/H264_MP3.mp4'
awaitfileIO.open(path).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber
console.info('open fd success fd is'+fdPath)
},(err)=>{
console.info('open fd failed err is'+err)
}).catch((err)=>{
console.info('open fd failed err is'+err)
});
letfile=awaitfs.open(path)
fdPath=fdPath+''+file.fd
this.avPlayer.url=fdPath// The initialized state is reported again.
}
...
...
@@ -493,14 +469,8 @@ export class AVPlayerDemo {
letpathDir="/data/storage/el2/base/haps/entry/files"// The path used here is an example. Obtain the path based on project requirements.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el2/100/base/ohos.acts.multimedia.media.avplayer/haps/entry/files" command.
@@ -51,7 +51,7 @@ For details about how to create an XComponent, see [XComponent](../reference/ark
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassVideoPlayerDemo{
// Report an error in the case of a function invocation failure.
failureCallback(error){
...
...
@@ -82,14 +82,8 @@ export class VideoPlayerDemo {
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command.
// Call createVideoPlayer to create a VideoPlayer instance.
awaitmedia.createVideoPlayer().then((video)=>{
if(typeof(video)!='undefined'){
...
...
@@ -180,7 +174,7 @@ export class VideoPlayerDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassVideoPlayerDemo{
// Report an error in the case of a function invocation failure.
failureCallback(error){
...
...
@@ -211,14 +205,8 @@ export class VideoPlayerDemo {
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command.
// Call createVideoPlayer to create a VideoPlayer instance.
awaitmedia.createVideoPlayer().then((video)=>{
if(typeof(video)!='undefined'){
...
...
@@ -267,7 +255,7 @@ export class VideoPlayerDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassVideoPlayerDemo{
// Report an error in the case of a function invocation failure.
failureCallback(error){
...
...
@@ -299,14 +287,8 @@ export class VideoPlayerDemo {
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command.
// Call createVideoPlayer to create a VideoPlayer instance.
awaitmedia.createVideoPlayer().then((video)=>{
if(typeof(video)!='undefined'){
...
...
@@ -341,14 +323,8 @@ export class VideoPlayerDemo {
// Obtain the next video FD address.
fdPath='fd://'
awaitfileIO.open(nextPath).then((fdNumber)=>{
fdPath=fdPath+''+fdNumber;
console.info('open fd success fd is'+fdPath);
},(err)=>{
console.info('open fd failed err is'+err);
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
letnextFile=awaitfs.open(nextPath);
fdPath=fdPath+''+nextFile.fd;
// Set the second video playback source.
videoPlayer.url=fdPath;
...
...
@@ -378,7 +354,7 @@ export class VideoPlayerDemo {
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfsfrom'@ohos.file.fs'
exportclassVideoPlayerDemo{
// Report an error in the case of a function invocation failure.
failureCallback(error){
...
...
@@ -409,14 +385,8 @@ export class VideoPlayerDemo {
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" command.
| type | string | Yes | Event type, which is **'bitrateDone'** in this case|
| type | string | Yes | Event type, which is **'bitrateDone'** in this case.|
**Example**
...
...
@@ -1694,6 +1694,8 @@ Sets audio and video recording parameters. This API uses an asynchronous callbac
This permission is required only if audio recording is involved.
To use the camera to record videos, the camera module is required. For details about how to obtain the permissions and use the APIs, see [Camera Management](js-apis-camera.md).
@@ -1717,13 +1719,14 @@ For details about the error codes, see [Media Error Codes](../errorcodes/errorco
**Example**
```js
// Configure the parameters based on those supported by the hardware device.
letAVRecorderProfile={
audioBitrate:48000,
audioChannels:2,
audioCodec:media.CodecMimeType.AUDIO_AAC,
audioSampleRate:48000,
fileFormat:media.ContainerFormatType.CFT_MPEG_4,
videoBitrate:48000,
videoBitrate:2000000,
videoCodec:media.CodecMimeType.VIDEO_MPEG4,
videoFrameWidth:640,
videoFrameHeight:480,
...
...
@@ -1757,6 +1760,8 @@ Sets audio and video recording parameters. This API uses a promise to return the
This permission is required only if audio recording is involved.
To use the camera to record videos, the camera module is required. For details about how to obtain the permissions and use the APIs, see [Camera Management](js-apis-camera.md).