For details about the APIs used for audio playback, see [js-apis-media.md](../reference/apis/js-apis-media.md).
| API| Description|
### Full-Process Scenario
| -------- | -------- |
| media.createAudioPlayer() | Creates an **AudioPlayer** instance.|
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.
| AudioPlayer | Provides audio playback features. For details, see the table below.|
For details about the **src** media source input types supported by **AudioPlayer**, see the [src attribute](../reference/apis/js-apis-media.md#audioplayer_Attributes).
**Table 2** AudioPlayer methods
```js
| Method| Description|
functionSetCallBack(audioPlayer){
| -------- | -------- |
audioPlayer.on('dataLoad',()=>{// Set the 'dataLoad' event callback, which is triggered when the src attribute is set successfully.
| release() | Releases audio resources.|
console.info('audio set source success');
| play() | Starts audio playback.|
// The playback page is ready. You can click the Play button to start the playback.
| pause() | Pauses playback.|
});
| stop() | Stops playback.|
audioPlayer.on('play',()=>{// Set the 'play' event callback.
| reset()<sup>7+</sup> | Resets the audio source to be played.|
// The Play button is changed to the pausable state.
| seek(timeMs: number) | Changes the playback position.|
});
| src:string | Defines the URI of an audio file to play.|
audioPlayer.on('pause',()=>{// Set the 'pause' event callback.
| state:AudioState | Defines the playback state.|
console.info('audio pause success');
| currentTime:number | Defines the current playback position.|
// The Play button is changed to the playable state.
| duration:number | Defines the playback duration. The value **-1** is returned if the data source does not support playback position change, for example, in the real-time streaming media scenario.|
});
| loop:boolean | Defines whether to loop audio playback.|
audioPlayer.on('stop',()=>{// Set the 'stop' event callback.
| on('play', function callback) | Subscribes to the playback start event.|
console.info('audio stop success');
| on('pause', function callback) | Subscribes to the playback pause event.|
// The playback stops, the playback progress bar returns to 0, and the Play button is changed to the playable state.
| on('stop', function callback) | Subscribes to the playback stop event.|
});
| on('reset', function callback) | Subscribes to the playback reset event.|
audioPlayer.on('reset',()=>{// Set the 'reset' event callback.
| on('finish',function callback) | Subscribes to the playback end event.|
console.info('audio reset success');
| on('error', function callback) | Subscribes to the playback error event.|
// You can reconfigure the src attribute to play another audio file.
| on('dataload', function callback) | Subscribes to the data loading event.|
});
| on('volumeChange', function callback) | Subscribes to the volume change event.|
audioPlayer.on('timeUpdate',(seekDoneTime)=>{// Set the 'timeUpdate' event callback.
| on('timeUpdate', function callback) | Subscribes to the progress change event.|
if(typeof(seekDoneTime)=='undefined'){
console.info('audio seek fail');
1. Create an audio player.
```
import media from '@ohos.multimedia.media';
var player = media.createAudioPlayer();
```
2. Set the subscription events.
```
player.on('play', (err, action) => {
if (err) {
console.error('Error returned in the play() callback.');
return;
}
console.info('Current player duration: '+ player.duration);
console.info('Current player time: ' + player.currentTime);
console.info('Current player status: '+player.state);
console.info('Pause MP3');
player.pause();
});
player.on('pause', (err, action) => {
if (err) {
console.error('Error returned in the pause() callback.');
return;
return;
}
console.info('Current player status: ' + player.state);
console.info('Current player time: ' + player.currentTime);
player.seek(30000); // Seek for 30 seconds.
});
player.on('stop', (err, action) => {
if (err) {
console.error('Error returned in the stop() callback.');