<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p11619510202511"><aname="p11619510202511"></a><aname="p11619510202511"></a>Creates an <strongid="b1189187131414"><aname="b1189187131414"></a><aname="b1189187131414"></a>AudioPlayer</strong> instance.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p1382410184281"><aname="p1382410184281"></a><aname="p1382410184281"></a>Provides audio playback functions. For details, see <strongid="b753712315016"><aname="b753712315016"></a><aname="b753712315016"></a>AudioPlayer</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p530714615545"><aname="p530714615545"></a><aname="p530714615545"></a>Resets the audio source to be played.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p440817478271"><aname="p440817478271"></a><aname="p440817478271"></a>Changes the playback position.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1040814782716"><aname="p1040814782716"></a><aname="p1040814782716"></a>Defines the URI of an audio file to play.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p105031612141811"><aname="p105031612141811"></a><aname="p105031612141811"></a>Defines a playback state.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p3409174762710"><aname="p3409174762710"></a><aname="p3409174762710"></a>Defines the current playback position.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p154091247132715"><aname="p154091247132715"></a><aname="p154091247132715"></a>Defines the playback duration.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p94091847102711"><aname="p94091847102711"></a><aname="p94091847102711"></a>Defines whether to loop audio playback.</p>
</td>
</tr>
<trid="row44091647192718"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p18937155862412"><aname="p18937155862412"></a><aname="p18937155862412"></a>on('play', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p13409164722717"><aname="p13409164722717"></a><aname="p13409164722717"></a>Subscribes to the playback start event.</p>
</td>
</tr>
<trid="row12409154711272"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p124091474279"><aname="p124091474279"></a><aname="p124091474279"></a>on('pause', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p7409174714274"><aname="p7409174714274"></a><aname="p7409174714274"></a>Subscribes to the playback pause event.</p>
</td>
</tr>
<trid="row040917477273"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p18981171411254"><aname="p18981171411254"></a><aname="p18981171411254"></a>on('stop', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1409164772714"><aname="p1409164772714"></a><aname="p1409164772714"></a>Subscribes to the playback stop event.</p>
</td>
</tr>
<trid="row712184163016"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p1167517115311"><aname="p1167517115311"></a><aname="p1167517115311"></a>on('reset', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1113134115301"><aname="p1113134115301"></a><aname="p1113134115301"></a>Subscribes to the playback reset event.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p8410124742713"><aname="p8410124742713"></a><aname="p8410124742713"></a>Subscribes to the playback end event.</p>
</td>
</tr>
<trid="row64105474278"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p166588276253"><aname="p166588276253"></a><aname="p166588276253"></a>on('error', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p54101047102712"><aname="p54101047102712"></a><aname="p54101047102712"></a>Subscribes to the playback error event.</p>
</td>
</tr>
<trid="row1410154715273"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p135217361257"><aname="p135217361257"></a><aname="p135217361257"></a>on('dataload', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1541054719271"><aname="p1541054719271"></a><aname="p1541054719271"></a>Subscribes to the data loading event.</p>
</td>
</tr>
<trid="row126521548145814"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p2652134812586"><aname="p2652134812586"></a><aname="p2652134812586"></a>on('volumeChange', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p116527483584"><aname="p116527483584"></a><aname="p116527483584"></a>Subscribes to the volume change event.</p>
</td>
</tr>
<trid="row1410124732711"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p942310426254"><aname="p942310426254"></a><aname="p942310426254"></a>on('timeUpdate', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p18410144715271"><aname="p18410144715271"></a><aname="p18410144715271"></a>Subscribes to the progress change event.</p>
</td>
</tr>
</tbody>
</table>
1. Create an audio player.
```
## Available APIs
import media from '@ohos.multimedia.media';
var player = media.createAudioPlayer();
**Table 1** APIs for audio playback
```
| API| Description|
2. Set the subscription events.
| -------- | -------- |
| media.createAudioPlayer() | Creates an **AudioPlayer** instance.|
```
| AudioPlayer | Provides audio playback features. For details, see the table below.|
player.on('play', (err, action) => {
**Table 2** AudioPlayer methods
| Method| Description|
| -------- | -------- |
| release() | Releases audio resources.|
| play() | Starts audio playback.|
| pause() | Pauses playback.|
| stop() | Stops playback.|
| reset()<sup>7+</sup> | Resets the audio source to be played.|
| seek(timeMs: number) | Changes the playback position.|
| src:string | Defines the URI of an audio file to play.|
| state:AudioState | Defines the playback state.|
| currentTime:number | Defines the current playback position.|
| 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.|
| on('play', function callback) | Subscribes to the playback start event.|
| on('pause', function callback) | Subscribes to the playback pause event.|
| on('stop', function callback) | Subscribes to the playback stop event.|
| on('reset', function callback) | Subscribes to the playback reset event.|
| on('finish',function callback) | Subscribes to the playback end event.|
| on('error', function callback) | Subscribes to the playback error event.|
| on('dataload', function callback) | Subscribes to the data loading event.|
| on('volumeChange', function callback) | Subscribes to the volume change event.|
| on('timeUpdate', function callback) | Subscribes to the progress change event.|
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) {
if (err) {
console.error('Error returned in the play() callback.');
console.error('Error returned in the pause() callback.');
return;
return;
}
}
console.info('Current player duration: '+ player.duration);
console.info('Current player status: ' + player.state);
console.info('Current player time: ' + player.currentTime);
console.info('Current player time: ' + player.currentTime);
console.info('Current player status: '+player.state);
player.seek(30000); // Seek for 30 seconds.
console.info('Pause MP3');
});
player.pause();
player.on('stop', (err, action) => {
});
if (err) {
player.on('pause', (err, action) => {
console.error('Error returned in the stop() callback.');
if (err) {
return;
console.error('Error returned in the pause() callback.');