# Audio Playback
## Modules to Import
```
import audio from '@ohos.multimedia.media';
```
## Required Permissions
None
## Methods
## createAudioPlayer\(\)
Creates an **AudioPlayer** instance to control audio playback.
**Return Values**
Type
|
Description
|
AudioPlayer
|
Returns the AudioPlayer instance if the operation is successful; returns null otherwise.
|
**Example**
```
var audioplayer = audio.createAudioPlayer();
```
## Appendixes
## AudioPlayer
Represents an audio player.
### Attributes
Name
|
Type
|
Readable
|
Writable
|
Description
|
src
|
string
|
Yes
|
Yes
|
URI of the audio resources
|
loop
|
boolean
|
Yes
|
Yes
|
Whether to loop audio playback
|
currentTime
|
number
|
Yes
|
No
|
Current playback position
|
duration
|
number
|
Yes
|
No
|
Playback duration
|
state
|
AudioState
|
Yes
|
No
|
Playback status
|
### play\(\)
Starts audio playback.
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.info('Error returned in the prepare() callback.');
return;
}
console.info('Start to play the audio.');
audioplayer.play();
};
audioplayer.on('play', (err, action) => {
if (err) {
console.info('Error returned in the play() callback.');
return;
}
console.info('Succeeded in playing the audio.');
};
audioplayer.src = 'common/mydream.mp3';
```
### pause\(\)
Pauses audio playback.
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.info('Error returned in the prepare() callback.');
return;
}
console.info('Start to play the audio.');
audioplayer.play();
}
audioplayer.on('play', (err, action) => {
if (err) {
console.info('Error returned in the play() callback.');
return;
}
console.info('Start to pause audio playback.');
audioplayer.pause();
};
audioplayer.on('pause', (err, action) => {
if (err) {
console.info('Error returned in the pause() callback.');
return;
}
console.info('Succeeded in pausing audio playback.');
};
audioplayer.src = 'common/mydream.mp3';
```
### stop\(\)
Stops audio playback.
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.info('Error returned in the prepare() callback.');
return;
}
console.info('Start to play the audio.');
audioplayer.play();
};
audioplayer.on('play', (err, action) => {
if (err) {
console.info('Error returned in the play() callback.');
return;
}
console.info('Start to pause audio playback.');
audioplayer.pause();
};
audioplayer.on('stop', (err, action) => {
if (err) {
console.info('Error returned in the stop() callback.');
return;
}
console.info('Succeeded in stopping audio playback.');
};
audioplayer.src = 'common/mydream.mp3';
```
### seek\(number\)
Goes to a specified playback position.
**Parameters**
Name
|
Type
|
Mandatory
|
Description
|
timeMs
|
number
|
Yes
|
Target playback position
|
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.info('Error returned in the prepare() callback.');
return;
}
console.info('Start to play the audio.');
audioplayer.play();
};
audioplayer.on('play', (err, action) => {
if (err) {
console.info('Error returned in the play() callback.');
return;
}
console.info('Succeeded in playing the audio.')
};
audioplayer.on('timeUpdate', (seekTime, action) => {
console.info('Seek time returned in the seek() callback: ' + seekTime);
var newTime = audioplayer.currenTime;
if(newTime == 30000) {
console.info('Seek succeeded. New time: ' + newTime);
} else {
console.info('Seek failed.');
}
};
audioplayer.src = 'common/mydream.mp3';
audioplayer.seek(30000);
```
### setVolume\(number\)
Sets the volume.
**Parameters**
Name
|
Type
|
Mandatory
|
Description
|
vol
|
number
|
Yes
|
Volume to set. The value ranges from 0 to 1.
|
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.error('Error returned in the prepare() callback.');
return;
}
console.info('Start to play the audio.');
audioplayer.play();
};
audioplayer.on('play', (err, action) => {
if (err) {
console.error('Error returned in the play() callback.');
return;
}
console.info('Succeeded in playing the audio.')
};
audioplayer.on('volumeChange', (err, action) => {
if (err) {
console.error('Error returned in the setVolume() callback.');
return;
}
console.info('Playback volume changed.');
};
audioplayer.src = 'common/mydream.mp3';
audioplayer.setVolume(0.5);
```
### reset\(\)7+
Switches the audio resource to be played.
**Example**
```
audioplayer.on('dataload', (err, action) => {
if (err) {
console.error('Error returned in the prepare() callback.');
return;
}
console.info('Start to reset audio playback.');
audioplayer.reset();
};
audioplayer.on('reset', (err, action) => {
if (err) {
console.error('Error returned in the reset() callback.');
return;
}
console.info('Reset succeeded.');
};
audioplayer.src = 'common/mydream.mp3';
```
### release\(\)
Releases audio resources.
**Example**
```
audioplay.release();
```
### Events
### play
Triggered when **play\(\)** is called
### pause
Triggered when **pause\(\)** is called
### stop
Triggered when **stop\(\)** is called
### dataLoad
Triggered when audio data is loaded
### timeUpdate
Triggered when the playback position changes
### volumeChange
Triggered when playback volume changes
### finish
Triggered when playback is finished
### error
Triggered when a playback error occurs
Name
|
Type
|
Description
|
callback
|
ErrorCallback
|
Callback function for handling this event
|
## AudioState
Describes playback status.
Name
|
Description
|
idle
|
Audio playback is idle.
|
playing
|
The audio is being played.
|
paused
|
Audio playback is paused.
|
stopped
|
Audio playback is stopped.
|