@@ -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.
@@ -33,31 +33,30 @@ The following figure shows the audio renderer state transitions.
For details about the APIs, see [AudioRenderer in Audio Management](../reference/apis/js-apis-audio.md#audiorenderer8).
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.
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);
}
letbuf=newArrayBuffer(bufferSize);
rlen+=ss.readSync(buf);
console.info(`Total bytes read from file: ${rlen}`);
writeBuffer(buf);
}else{
console.info('check after next interval');
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 +419,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 +490,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.