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');
});
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, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
...
...
@@ -107,7 +107,7 @@ async function audioPlayerDemo() {
}).catch((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,16 +119,16 @@ import fileIO from '@ohos.fileio'
exportclassAudioDemo{
// Set the player callbacks.
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');
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');
});
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');
audioPlayer.release();// Release the AudioPlayer resources.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
});
}
...
...
@@ -147,7 +147,7 @@ export class AudioDemo {
}).catch((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.
}
}
```
...
...
@@ -161,15 +161,15 @@ export class AudioDemo {
// Set the player callbacks.
privateisNextMusic=false;
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');
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');
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');
if(!this.isNextMusic){// When isNextMusic is false, changing songs is implemented.
this.nextMusic(audioPlayer);// Changing songs is implemented.
...
...
@@ -193,7 +193,7 @@ export class AudioDemo {
}).catch((err)=>{
console.info('open fd failed err is'+err);
});
audioPlayer.src=nextFdPath;// Set the src attribute and trigger the dataLoad event callback.
audioPlayer.src=nextFdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
asyncaudioPlayerDemo(){
...
...
@@ -210,7 +210,7 @@ export class AudioDemo {
}).catch((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.
}
}
```
...
...
@@ -223,12 +223,12 @@ import fileIO from '@ohos.fileio'
exportclassAudioDemo{
// Set the player callbacks.
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');
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',()=>{// Sets 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');
});
}
...
...
@@ -247,7 +247,7 @@ export class AudioDemo {
}).catch((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.
}
}
```
...
...
@@ -257,7 +257,6 @@ export class AudioDemo {
The following samples are provided to help you better understand how to develop audio playback:
-[`JsDistributedMusicPlayer`: Distributed Music Player (JS, API version 7)](https://gitee.com/openharmony/app_samples/tree/master/ability/JsDistributedMusicPlayer)
-[`JsAudioPlayer`: Audio Playback and Management (JS, API version 7)](https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer)
-[`JsAudioPlayer`: Audio Playback and Management (JS, API version 8)](https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer)
-[`eTsAudioPlayer`: Audio Player (eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)