@@ -16,49 +16,38 @@ You can use audio playback APIs to convert audio data into audible analog signal
...
@@ -16,49 +16,38 @@ You can use audio playback APIs to convert audio data into audible analog signal
## How to Develop
## How to Develop
For details about the APIs, see [AudioPlayer in the Media API](../reference/apis/js-apis-media.md).
For details about the APIs used for audio playback, see [AudioPlayer in the Media API](../reference/apis/js-apis-media.md).
### Full-Process Scenario
### Full-Process Scenario
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 the player, and releasing resources.
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 the player, and releasing resources.
For details about the **src** 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'
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfileIOfrom'@ohos.fileio'
// Print the stream track information.
functionSetCallBack(audioPlayer){
functionprintfDescription(obj){
for(letiteminobj){
letproperty=obj[item];
console.info('audio key is '+item);
console.info('audio value is '+property);
}
}
// Set the player callbacks.
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');
audioPlayer.play();// The play() API can be invoked only after the 'dataLoad' event callback is complete. The 'play' event callback is then triggered.
// The playback page is ready. You can click the Play button to start the playback.
});
});
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.pause();// Trigger the 'pause' event callback and pause the playback.
// The Play button is changed to the pausable state.
});
});
audioPlayer.on('pause',()=>{// Set the 'pause' event callback.
audioPlayer.on('pause',()=>{// Set the 'pause' event callback.
console.info('audio pause success');
console.info('audio pause success');
audioPlayer.seek(5000);// Trigger the 'timeUpdate' event callback, and seek to 5000 ms for playback.
// The Play button is changed to the playable state.
});
});
audioPlayer.on('stop',()=>{// Set the 'stop' event callback.
audioPlayer.on('stop',()=>{// Set the 'stop' event callback.
console.info('audio stop success');
console.info('audio stop success');
audioPlayer.reset();// Trigger the 'reset' event callback, and reconfigure the src attribute to switch to the next song.
// The playback stops, the playback progress bar returns to 0, and the Play button is changed to the playable state.
});
});
audioPlayer.on('reset',()=>{// Set the 'reset' event callback.
audioPlayer.on('reset',()=>{// Set the 'reset' event callback.
console.info('audio reset success');
console.info('audio reset success');
audioPlayer.release();// Release the AudioPlayer resources.
// You can reconfigure the src attribute to play another audio file.
audioPlayer=undefined;
});
});
audioPlayer.on('timeUpdate',(seekDoneTime)=>{// Set the 'timeUpdate' event callback.
audioPlayer.on('timeUpdate',(seekDoneTime)=>{// Set the 'timeUpdate' event callback.
if(typeof(seekDoneTime)=='undefined'){
if(typeof(seekDoneTime)=='undefined'){
...
@@ -66,49 +55,71 @@ function setCallBack(audioPlayer) {
...
@@ -66,49 +55,71 @@ function setCallBack(audioPlayer) {
return;
return;
}
}
console.info('audio seek success, and seek time is '+seekDoneTime);
console.info('audio seek success, and seek time is '+seekDoneTime);
audioPlayer.setVolume(0.5);// Trigger the 'volumeChange' event callback.
// The playback progress bar is updated to the seek position.
});
});
audioPlayer.on('volumeChange',()=>{// Set the 'volumeChange' event callback.
audioPlayer.on('volumeChange',()=>{// Set the 'volumeChange' event callback.
console.info('audio volumeChange success');
console.info('audio volumeChange success');
audioPlayer.getTrackDescription((error,arrlist)=>{// Obtain the audio track information in callback mode.
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}`);
});
});
}
}
asyncfunctionaudioPlayerDemo(){
functionprintfDescription(obj){
// 1. Create an audioPlayer instance.
for(letiteminobj){
letaudioPlayer=media.createAudioPlayer();
letproperty=obj[item];
setCallBack(audioPlayer);// Set the event callbacks.
console.info('audio key is '+item);
// 2. Set the URI of the audio file selected by the user.
console.info('audio value is '+property);
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.
audioPlayer.stop();// Trigger the 'stop' event callback.
// 9. Reset the player.
audioPlayer.reset();// Trigger the 'reset' event callback, and reconfigure the src attribute to switch to the next song.
// 10. Release the resource.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
```
```
### Normal Playback Scenario
### Normal Playback Scenario
...
@@ -116,40 +127,37 @@ async function audioPlayerDemo() {
...
@@ -116,40 +127,37 @@ async function audioPlayerDemo() {
```js
```js
importmediafrom'@ohos.multimedia.media'
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfileIOfrom'@ohos.fileio'
exportclassAudioDemo{
// Set the player callbacks.
functionSetCallBack(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 resources.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
audioPlayer=undefined;
});
}
asyncaudioPlayerDemo(){
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
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.
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
```
```
### Switching to the Next Song
### Switching to the Next Song
...
@@ -157,62 +165,52 @@ export class AudioDemo {
...
@@ -157,62 +165,52 @@ export class AudioDemo {
```js
```js
importmediafrom'@ohos.multimedia.media'
importmediafrom'@ohos.multimedia.media'
importfileIOfrom'@ohos.fileio'
importfileIOfrom'@ohos.fileio'
exportclassAudioDemo{
// Set the player callbacks.
functionSetCallBack(audioPlayer){
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');
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.on('reset',()=>{// Set the 'reset' event callback.
audioPlayer.on('finish',()=>{// Set the 'finish' event callback, which is triggered when the playback is complete.
console.info('audio play success');
console.info('audio play finish');
if(!this.isNextMusic){// When isNextMusic is false, changing songs is implemented.
this.nextMusic(audioPlayer);// Changing songs is implemented.
}else{
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer.release();// Release the AudioPlayer instance.
audioPlayer=undefined;
audioPlayer=undefined;
}
});
});
}
asyncnextMusic(audioPlayer){
this.isNextMusic=true;
letnextFdPath='fd://'
// The stream in the path can be pushed to the device by running the "hdc file send D:\xxx\02.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" command.
audioPlayer.src=nextFdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
asyncaudioPlayerDemo(){
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
this.setCallBack(audioPlayer);// Set the event callbacks.
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.
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.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.
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.
console.info('audio play finish');
asyncaudioPlayerDemo(){
audioPlayer.release();// Release the AudioPlayer instance.
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
audioPlayer=undefined;
this.setCallBack(audioPlayer);// Set the event callbacks.
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.
audioPlayer.src=fdPath;// Set the src attribute and trigger the 'dataLoad' event callback.
}
}
}
```
## Samples
The following samples are provided to help you better understand how to develop audio playback:
letaudioPlayer=media.createAudioPlayer();// Create an AudioPlayer instance.
SetCallBack(audioPlayer);// Set the event callbacks.
-[`JsDistributedMusicPlayer`: Distributed Music Player (JS) (API7)](https://gitee.com/openharmony/app_samples/tree/master/ability/JsDistributedMusicPlayer)
-[`JsAudioPlayer`: Audio Playback and Management (JS, API 7)](https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer)
/* Set the FD (local playback) of the audio file selected by the user. */
-[`eTsAudioPlayer`: Audio Player (eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)
privatetestFdNumber;// Used to save the FD address.
lettestFdNumber;
// Set the callbacks related to audio recording.
functionSetCallBack(audioRecorder){
setCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
audioRecorder.start();// Call the start API to start recording and trigger the 'start' event callback.
});
});
audioRecorder.on('start',()=>{// Set the 'start' event callback.
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
console.log('audio recorder start success');
audioRecorder.pause();// Call the pause API to pause recording and trigger the 'pause' event callback.
// The Record button is changed to the pausable state.
});
});
audioRecorder.on('pause',()=>{// Set the 'pause' event callback.
audioRecorder.on('pause',()=>{// Set the 'pause' event callback.
console.log('audio recorder pause success');
console.log('audio recorder pause success');
audioRecorder.resume();// Call the resume API to resume recording and trigger the 'resume' event callback.
// The Record button is changed to the recordable state.
});
});
audioRecorder.on('resume',()=>{// Set the 'resume' event callback.
audioRecorder.on('resume',()=>{// Set the 'resume' event callback.
console.log('audio recorder resume success');
console.log('audio recorder resume success');
audioRecorder.stop();// Call the stop API to stop recording and trigger the 'stop' event callback.
// The Record button is changed to the pausable state.
});
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
console.log('audio recorder stop success');
audioRecorder.reset();// Call the reset API to reset the recorder and trigger the 'reset' event callback.
});
});
audioRecorder.on('reset',()=>{// Set the 'reset' event callback.
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder reset success');
console.log('audio recorder release success');
audioRecorder.release();// Call the release API to release resources and trigger the 'release' event callback.
});
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
audioRecorder.on('reset',()=>{// Set the 'reset' event callback.
console.log('audio recorder release success');
console.log('audio recorder reset success');
audioRecorder=undefined;
// You need to reset the recording parameters for another recording.
});
});
audioRecorder.on('error',(error)=>{// Set the 'error' event callback.
audioRecorder.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}`);
});
});
}
}
// pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Video/01.mp3.
// 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.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncgetFd(pathName){
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
constmediaTest=mediaLibrary.getMediaLibrary();
letfileKeyObj=mediaLibrary.FileKey;
letfileKeyObj=mediaLibrary.FileKey;
...
@@ -75,37 +72,49 @@ export class AudioRecorderDemo {
...
@@ -75,37 +72,49 @@ export class AudioRecorderDemo {
privatetestFdNumber;// Used to save the FD address.
lettestFdNumber;
// Set the callbacks related to audio recording.
functionSetCallBack(audioRecorder){
setCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
audioRecorder.start();// Call the start API to start recording and trigger the 'start' event callback.
});
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
audioRecorder.stop();// Call the stop API to stop recording and trigger the 'stop' event callback.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
audioRecorder.release();// Call the release API to release resources and trigger the 'release' event callback.
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
audioRecorder=undefined;
});
audioRecorder.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}`);
});
});
}
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
// The Record button is changed to the pausable state.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
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/Video/01.mp3.
// 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.
// To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA.
asyncgetFd(pathName){
asyncfunctiongetFd(pathName){
letdisplayName=pathName;
letdisplayName=pathName;
constmediaTest=mediaLibrary.getMediaLibrary();
constmediaTest=mediaLibrary.getMediaLibrary();
letfileKeyObj=mediaLibrary.FileKey;
letfileKeyObj=mediaLibrary.FileKey;
...
@@ -153,43 +154,41 @@ export class AudioRecorderDemo {
...
@@ -153,43 +154,41 @@ export class AudioRecorderDemo {
awaitthis.getFd('01.mp3');// Call the getFd method to obtain the FD address of the file to be recorded.
// 3. Set the recording parameters.
letaudioRecorderConfig={
audioEncodeBitRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
uri:this.testFdNumber,// testFdNumber is generated by getFd.
location:{latitude:30,longitude:130},
audioEncoderMime:media.CodecMimeType.AUDIO_AAC,
fileFormat:media.ContainerFormatType.CFT_MPEG_4A,
}
audioRecorder.prepare(audioRecorderConfig);// Call the prepare method to trigger the 'prepare' event callback.
}
}
}
```
## Samples
awaitgetFd('01.mp3');
The following samples are provided to help you better understand how to develop audio recording:
// 1. Create an AudioRecorder instance.
letaudioRecorder=media.createAudioRecorder();
-[`Recorder`: Recorder (eTS, API 8)](https://gitee.com/openharmony/app_samples/tree/master/media/Recorder)
// 2. Set the callbacks.
-[`eTsAudioPlayer`: Audio Player (eTS)](https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets)