audioPlayer.stop();// Trigger the 'stop' event callback to stop the playback.
audioPlayer.stop();// Trigger the 'stop' event callback to stop the playback.
});
});
});
});
audioPlayer.on('finish',()=>{// Set the 'finish' event callback, which is triggered when the playback is complete.
audioPlayer.on('finish',()=>{// Set the 'finish' event callback, which is triggered when the playback is complete.
console.info('audio play finish');
console.info('audio play finish');
});
});
audioPlayer.on('error',(error)=>{// Set the 'error' event callback.
audioPlayer.on('error',(error)=>{// Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
console.info(`audio error called, errMessage is ${error.message}`);
...
@@ -94,7 +94,7 @@ function setCallBack(audioPlayer) {
...
@@ -94,7 +94,7 @@ function setCallBack(audioPlayer) {
asyncfunctionaudioPlayerDemo(){
asyncfunctionaudioPlayerDemo(){
// 1. Create an AudioPlayer instance.
// 1. Create an AudioPlayer instance.
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.
letfdPath='fd://'
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
...
@@ -107,7 +107,7 @@ async function audioPlayerDemo() {
...
@@ -107,7 +107,7 @@ async function audioPlayerDemo() {
}).catch((err)=>{
}).catch((err)=>{
console.info('open fd failed err is'+err);
console.info('open fd failed err is'+err);
});
});
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
audioPlayer.src=fdPath;// Set the src attribute and trigger the `dataLoad` event callback.
}
}
```
```
...
@@ -119,23 +119,23 @@ import fileIO from '@ohos.fileio'
...
@@ -119,23 +119,23 @@ import fileIO from '@ohos.fileio'
exportclassAudioDemo{
exportclassAudioDemo{
// Set the player callbacks.
// Set the player callbacks.
setCallBack(audioPlayer){
setCallBack(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');
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
});
});
audioPlayer.on('play',()=>{// Set the 'play' event callback.
audioPlayer.on('play',()=>{// Set the `play` event callback.
console.info('audio play success');
console.info('audio play success');
});
});
audioPlayer.on('finish',()=>{// Set the 'finish' event callback, which is triggered when the playback is complete.
audioPlayer.on('finish',()=>{// Set the 'finish' event callback, which is triggered when the playback is complete.
console.info('audio play finish');
console.info('audio play finish');
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
audioPlayer=undefined;
});
});
}
}
asyncaudioPlayerDemo(){
asyncaudioPlayerDemo(){
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
audioPlayer.src=fdPath;// Set the src attribute and trigger the `dataLoad` event callback.
}
}
}
}
```
```
...
@@ -161,20 +161,20 @@ export class AudioDemo {
...
@@ -161,20 +161,20 @@ export class AudioDemo {
// Set the player callbacks.
// Set the player callbacks.
privateisNextMusic=false;
privateisNextMusic=false;
setCallBack(audioPlayer){
setCallBack(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');
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
});
});
audioPlayer.on('play',()=>{// Set the 'play' event callback.
audioPlayer.on('play',()=>{// Set the `play` event callback.
console.info('audio play success');
console.info('audio play success');
audioPlayer.reset();// Call the reset() API and trigger the 'reset' event callback.
audioPlayer.reset();// Call the reset() API and trigger the 'reset' event callback.
});
});
audioPlayer.on('reset',()=>{// Set the 'reset' event callback.
audioPlayer.on('reset',()=>{// Set the `reset` event callback.
console.info('audio play success');
console.info('audio play success');
if(!this.isNextMusic){// When isNextMusic is false, changing songs is implemented.
if(!this.isNextMusic){// When isNextMusic is false, changing songs is implemented.
this.nextMusic(audioPlayer);// Changing songs is implemented.
this.nextMusic(audioPlayer);// Changing songs is implemented.
}else{
}else{
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
audioPlayer=undefined;
}
}
});
});
...
@@ -197,8 +197,8 @@ export class AudioDemo {
...
@@ -197,8 +197,8 @@ export class AudioDemo {
}
}
asyncaudioPlayerDemo(){
asyncaudioPlayerDemo(){
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
@@ -223,19 +223,19 @@ import fileIO from '@ohos.fileio'
...
@@ -223,19 +223,19 @@ import fileIO from '@ohos.fileio'
exportclassAudioDemo{
exportclassAudioDemo{
// Set the player callbacks.
// Set the player callbacks.
setCallBack(audioPlayer){
setCallBack(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');
audioPlayer.loop=true;// Set the loop playback attribute.
audioPlayer.loop=true;// Set the loop playback attribute.
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
audioPlayer.play();// Call the play() API to start the playback and trigger the 'play' event callback.
});
});
audioPlayer.on('play',()=>{// Set the 'play' event callback to start loop playback.
audioPlayer.on('play',()=>{// Set the 'play' event callback to start loop playback.
console.info('audio play success');
console.info('audio play success');
});
});
}
}
asyncaudioPlayerDemo(){
asyncaudioPlayerDemo(){
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
this.setCallBack(audioPlayer);// Set the event callbacks.
letfdPath='fd://'
letfdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.