# 音频播放 - [导入模块](#zh-cn_topic_0000001103383404_s56d19203690d4782bfc74069abb6bd71) - [权限](#zh-cn_topic_0000001103383404_section11257113618419) - [方法](#zh-cn_topic_0000001103383404_section125675489541) - [creatAudioPlayer\(\)](#zh-cn_topic_0000001103383404_section582314017253) - [附录](#zh-cn_topic_0000001103383404_section1933416317165) - [AudioPlayer](#zh-cn_topic_0000001103383404_section5174142818365) - [属性](#zh-cn_topic_0000001103383404_section4947115405) - [play\(\)](#zh-cn_topic_0000001103383404_section964512672913) - [pause\(\)](#zh-cn_topic_0000001103383404_section78173258296) - [stop\(\)](#zh-cn_topic_0000001103383404_section122114334296) - [seek\(number\)](#zh-cn_topic_0000001103383404_section1387113816298) - [setVolume\(number\)](#zh-cn_topic_0000001103383404_section164235176552) - [release\(\)](#zh-cn_topic_0000001103383404_section9224621145512) - [事件](#zh-cn_topic_0000001103383404_section5453721192911) - [play](#zh-cn_topic_0000001103383404_section87307411494) - [pause](#zh-cn_topic_0000001103383404_section198217471590) - [stop](#zh-cn_topic_0000001103383404_section437616531910) - [dataLoad](#zh-cn_topic_0000001103383404_section982114219106) - [timeUpdate](#zh-cn_topic_0000001103383404_section13687114181014) - [volumeChange](#zh-cn_topic_0000001103383404_section139227715717) - [finish](#zh-cn_topic_0000001103383404_section15181321181018) - [error](#zh-cn_topic_0000001103383404_section5593132921016) - [AudioState](#zh-cn_topic_0000001103383404_section5181155710523) ## 导入模块 ``` import audio from '@ohos.multimedia.media'; ``` ## 权限 无 ## 方法 ## creatAudioPlayer\(\) 创建音频播放的实例来控制音频的播放。 **返回值:**

类型

说明

AudioPlayer

返回AudioPlayer类实例,失败时返回null。

**示例:** ``` var audioplayer = audio.createAudioPlayer(); ``` ## 附录 ## AudioPlayer 音频播放器。 ### 属性

名称

参数类型

可读

可写

说明

src

string

音频媒体URI。

loop

boolean

音频循环播放属性。

currentTime

number

音频的当前播放阶段。

duration

number

音频播放的时长。

state

AudioState

音频播放的状态。

### play\(\) 开始播放音频资源。 **示例:** ``` audioplayer.src = 'common/mydream.mp3'; audioplayer.on('play', (err, action) => { if (err) { console.log(`err returned in play() callback`); return; } console.log('start to play'); audioplayer.pause(); }; audioplayer.play(); ``` ### pause\(\) 暂停播放音频资源。 **示例:** ``` audioplayer.src = 'common/mydream.mp3'; audioplayer.on('pause', (err, action) => { if (err) { console.log(`err returned in pause() callback`); return; } console.log('playback pause'); }; audioplayer.pause(); ``` ### stop\(\) 停止播放音频资源。 **示例:** ``` audioplayer.src = 'common/mydream.mp3'; audioplayer.on('stop',(err, action) => { if (err) { console.log(`err returned in stop() callback`); return; } console.log('playback stop'); }; audioplayer.stop(); ``` ### seek\(number\) 跳转到指定播放位置。 **参数:**

参数名

类型

必填

说明

timeMs

number

想要跳转到的时间点。

**示例:** ``` audioplayer.src = 'common/mydream.mp3'; audioplayer.on('timeupdate', (err, action) => { if (err) { console.log(`err returned in seek() callback`); return; } var newTime = audioplayer.currenTime; if(newTime = 30000) { console.info('Seek success, new time : ' + newTime); } else { console.info('Seek Failed'); } }; audioplayer.seek(30000); ``` ### setVolume\(number\) 设置音量。 **参数:**

参数名

类型

必填

说明

vol

number

想要设置的音量值。

**示例:** ``` audioplayer.src = 'common/mydream.mp3'; audioplayer.on('volumeChange', (err, action) => { if (err) { console.log(`err returned in setVolume() callback`); return; } console.log('playback volume change'); }; audioplayer.setVolume(30); ``` ### release\(\) 释放音频资源。 **示例:** ``` audioplay.release(); ``` ### 事件 ### play 在调用play方法后的回调事件。 ### pause 在调用pause方法后的回调事件。 ### stop 在调用stop方法后的回调事件。 ### dataLoad 在音频数据加载后的回调事件。 ### timeUpdate 在播放时间改变后的回调事件。 ### volumeChange 在播放音量改变后的回调事件。 ### finish 在播放完成后的回调事件。 ### error 在播放错误时的回调事件。

参数名

类型

说明

callback

ErrorCallback

错误事件的回调函数。

## AudioState 音频播放的状态机。

名称

描述

playing

音频正在播放。

paused

音频暂停播放

stopped

音频播放停止。