@@ -25,6 +25,9 @@ The full audio playback process includes creating an instance, setting the URI,
...
@@ -25,6 +25,9 @@ The full audio playback process includes creating an instance, setting the URI,
For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_Attributes).
For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_Attributes).
```js
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
functionSetCallBack(audioPlayer){
functionSetCallBack(audioPlayer){
audioPlayer.on('dataLoad',()=>{// Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
audioPlayer.on('dataLoad',()=>{// Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
console.info('audio set source success');
console.info('audio set source success');
...
@@ -80,7 +83,18 @@ function printfDescription(obj) {
...
@@ -80,7 +83,18 @@ function printfDescription(obj) {
letaudioPlayer=media.createAudioPlayer();
letaudioPlayer=media.createAudioPlayer();
SetCallBack(audioPlayer);// Set the event callbacks.
SetCallBack(audioPlayer);// Set the event callbacks.
// 2. Set the URI of the audio file.
// 2. Set the URI of the audio file.
audioPlayer.src='file:///data/data/ohos.xxx.xxx/files/test.mp3';// Set the src attribute and trigger the 'dataLoad' event callback.
@@ -23,6 +23,11 @@ For details about the APIs used for audio recording, see [js-apis-media.md](../r
...
@@ -23,6 +23,11 @@ For details about the APIs used for audio recording, see [js-apis-media.md](../r
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
console.log('prepare success');
...
@@ -57,6 +62,31 @@ function SetCallBack(audioRecorder) {
...
@@ -57,6 +62,31 @@ function SetCallBack(audioRecorder) {
});
});
}
}
// 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.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
console.log('prepare success');
...
@@ -108,6 +143,32 @@ function SetCallBack(audioPlayer) {
...
@@ -108,6 +143,32 @@ function SetCallBack(audioPlayer) {
console.log('audio recorder release success');
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/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.
@@ -28,10 +28,23 @@ For details about the APIs used for video playback, see [js-apis-media.md](../re
...
@@ -28,10 +28,23 @@ For details about the APIs used for video playback, see [js-apis-media.md](../re
The full video playback process includes creating an instance, setting the URL, setting the surface ID, preparing for video playback, playing video, pausing playback, obtaining track information, seeking to a playback position, setting the volume, setting the playback speed, stopping playback, resetting the playback configuration, and releasing resources.
The full video playback process includes creating an instance, setting the URL, setting the surface ID, preparing for video playback, playing video, pausing playback, obtaining track information, seeking to a playback position, setting the volume, setting the playback speed, stopping playback, resetting the playback configuration, and releasing resources.
For details about the **url** media source input types supported by **VideoPlayer**, see the [url attribute](../reference/apis/js-apis-media.md#videoplayer_attributes).
For details about how to create an Xcomponent, see [Xcomponent Creation](#Xcomponent).
```js
```js
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letvideoPlayer=undefined;// Used to store instances created by calling the createVideoPlayer method.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
letsurfaceID=undefined;// Used to save the surface ID returned by the Xcomponent interface.
// The LoadXcomponent() method is used to obtain the surface ID and save it to the **surfaceID** variable. This method is automatically called when the Xcomponent is loaded.
if="{{isFlush}}"// Refresh the surface ID. To enable automatic loading of the Xcomponent and obtain the new surface ID, assign **false** to **isFlush** and then assign **true** to **isFlush**.
type='surface'
onload='LoadXcomponent'// Default interface for loading the Xcomponent.
style="width:720px;height:480px;border-color:red;border-width:5px;">// Set the window width, height, and other attributes.
@@ -23,6 +23,36 @@ For details about the APIs used for video recording, see [js-apis-media.md](../r
...
@@ -23,6 +23,36 @@ For details about the APIs used for video recording, see [js-apis-media.md](../r
The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources.
The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources.
// pathName indicates the passed recording file name, for example, 01.mp4. The generated file address is /storage/media/100/local/files/Movies/01.mp4.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
The full audio playback process includes creating an instance, setting the URI, playing audio, seeking to the playback position, setting the volume, pausing playback, obtaining track information, stopping playback, resetting resources, and releasing resources.
For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_attributes).
During audio recording, audio signals are captured, encoded, and saved to files. You can specify parameters such as the sampling rate, number of audio channels, encoding format, encapsulation format, and file path for audio recording.
For details about the APIs used for audio recording, see [js-apis-media.md](../reference/apis/js-apis-media.md).
### 全流程场景
### Full-Process Scenario
包含流程:创建实例,设置录制参数,录制音频,暂停录制,恢复录制,停止录制,释放资源等流程。
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
```js
```js
importmediafrom'@ohos.multimedia.media'
importmediafrom'@ohos.multimedia.media'
...
@@ -29,41 +29,41 @@ import mediaLibrary from '@ohos.multimedia.mediaLibrary'
...
@@ -29,41 +29,41 @@ import mediaLibrary from '@ohos.multimedia.mediaLibrary'
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncfunctiongetFd(pathName){
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
constmediaTest=mediaLibrary.getMediaLibrary();
...
@@ -87,39 +87,39 @@ async function getFd(pathName) {
...
@@ -87,39 +87,39 @@ async function getFd(pathName) {
awaitgetFd('01.mp3');
awaitgetFd('01.mp3');
// 1.创建实例
// 1. Create an AudioRecorder instance.
letaudioRecorder=media.createAudioRecorder();
letaudioRecorder=media.createAudioRecorder();
// 2.设置回调
// 2. Set the callbacks.
SetCallBack(audioRecorder);
SetCallBack(audioRecorder);
// 3.设置录制参数
// 3. Set the recording parameters.
letaudioRecorderConfig={
letaudioRecorderConfig={
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncodeBitRate:22050,
audioEncodeBitRate:22050,
audioSampleRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
numberOfChannels:2,
format:media.AudioOutputFormat.AAC_ADTS,
format:media.AudioOutputFormat.AAC_ADTS,
uri:testFdNumber,// testFdNumber由getFd生成
uri:testFdNumber,// testFdNumber is generated by getFd.
audioRecorder.stop();// The stop method can be called to trigger the 'stop' event callback only after the 'start' or 'resume' event callback is complete.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncfunctiongetFd(pathName){
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
constmediaTest=mediaLibrary.getMediaLibrary();
...
@@ -169,27 +169,26 @@ async function getFd(pathName) {
...
@@ -169,27 +169,26 @@ async function getFd(pathName) {
awaitgetFd('01.mp3');
awaitgetFd('01.mp3');
// 1.创建实例
// 1. Create an AudioRecorder instance.
letaudioRecorder=media.createAudioRecorder();
letaudioRecorder=media.createAudioRecorder();
// 2.设置回调
// 2. Set the callbacks.
SetCallBack(audioRecorder);
SetCallBack(audioRecorder);
// 3.设置录制参数
// 3. Set the recording parameters.
letaudioRecorderConfig={
letaudioRecorderConfig={
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncodeBitRate:22050,
audioEncodeBitRate:22050,
audioSampleRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
numberOfChannels:2,
format:media.AudioOutputFormat.AAC_ADTS,
format:media.AudioOutputFormat.AAC_ADTS,
uri:testFdNumber,// testFdNumber由getFd生成
uri:testFdNumber,// testFdNumber is generated by getFd.
audioRecorder.stop();// The stop method can be called to trigger the 'stop' event callback only after the 'start' or 'resume' event callback is complete.
// 6.释放资源
// 6. Release resources.
audioRecorder.release();// audioRecorder资源被销毁
audioRecorder.release();// The AudioRecorder resource is destroyed.
During video recording, audio and video signals are captured, encoded, and saved to files. You can specify parameters such as the encoding format, encapsulation format, and file path for video recording.
For details about the APIs used for video recording, see [js-apis-media.md](../reference/apis/js-apis-media.md).
### 全流程场景
### Full-Process Scenario
包含流程:创建实例,设置录制参数,录制视频,暂停录制,恢复录制,停止录制,释放资源等流程。
The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources.
```js
```js
importmediafrom'@ohos.multimedia.media'
importmediafrom'@ohos.multimedia.media'
...
@@ -28,8 +28,8 @@ import mediaLibrary from '@ohos.multimedia.mediaLibrary'
...
@@ -28,8 +28,8 @@ import mediaLibrary from '@ohos.multimedia.mediaLibrary'
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncfunctiongetFd(pathName){
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
constmediaTest=mediaLibrary.getMediaLibrary();
...
@@ -70,29 +70,29 @@ let videoConfig = {
...
@@ -70,29 +70,29 @@ let videoConfig = {
audioSourceType:1,
audioSourceType:1,
videoSourceType:0,
videoSourceType:0,
profile:videoProfile,
profile:videoProfile,
url:testFdNumber,// testFdNumber由getFd生成
url:testFdNumber,// testFdNumber is generated by getFd.
orientationHint:0,
orientationHint:0,
location:{latitude:30,longitude:130},
location:{latitude:30,longitude:130},
}
}
// 当发生错误上上报的错误回调接口
// Error callback triggered in the case of an error
functionfailureCallback(error){
functionfailureCallback(error){
console.info('error happened, error name is '+error.name);
console.info('error happened, error name is '+error.name);
console.info('error happened, error code is '+error.code);
console.info('error happened, error code is '+error.code);
console.info('error happened, error message is '+error.message);
console.info('error happened, error message is '+error.message);
}
}
// 当发生异常时,系统调用的错误回调接口
// Error callback triggered in the case of an exception
functioncatchCallback(error){
functioncatchCallback(error){
console.info('catch error happened, error name is '+error.name);
console.info('catch error happened, error name is '+error.name);
console.info('catch error happened, error code is '+error.code);
console.info('catch error happened, error code is '+error.code);
console.info('catch error happened, error message is '+error.message);
console.info('catch error happened, error message is '+error.message);
// Video recording depends on camera-related interfaces. The following operations can be performed only after the video output start interface is invoked.
// 视频录制启动接口
// Start video recording.
awaitvideoRecorder.start().then(()=>{
awaitvideoRecorder.start().then(()=>{
console.info('start success');
console.info('start success');
},failureCallback).catch(catchCallback);
},failureCallback).catch(catchCallback);
// 调用pause接口时需要暂停camera出流
// Pause video playback before the video output stop interface is invoked.
awaitvideoRecorder.pause().then(()=>{
awaitvideoRecorder.pause().then(()=>{
console.info('pause success');
console.info('pause success');
},failureCallback).catch(catchCallback);
},failureCallback).catch(catchCallback);
// 调用resume接口时需要恢复camera出流
// Resume video playback after the video output start interface is invoked.
awaitvideoRecorder.resume().then(()=>{
awaitvideoRecorder.resume().then(()=>{
console.info('resume success');
console.info('resume success');
},failureCallback).catch(catchCallback);
},failureCallback).catch(catchCallback);
// 停止camera出流后,停止视频录制
// Stop video recording after the video output stop interface is invoked.
awaitvideoRecorder.stop().then(()=>{
awaitvideoRecorder.stop().then(()=>{
console.info('stop success');
console.info('stop success');
},failureCallback).catch(catchCallback);
},failureCallback).catch(catchCallback);
// 重置录制相关配置
// Reset the recording configuration.
awaitvideoRecorder.reset().then(()=>{
awaitvideoRecorder.reset().then(()=>{
console.info('reset success');
console.info('reset success');
},failureCallback).catch(catchCallback);
},failureCallback).catch(catchCallback);
// 释放视频录制相关资源并释放camera对象相关资源
// Release the video recording resources and camera object resources.