Audio Playback
WdAudio
Property Description
External properties received by the component
Property Name | Property Identifier | Type | Description |
|---|
| 模板 | template | string | 指定audio模板 Example: "normal" |
| 音频资源 | src | string | 音频资源的地址,用于直接播放,支持云文件ID Example: "https://qcloudimg.tencent-cloud.cn/raw/472043e1e13e1cc06e4afc46407e8c4d.mp4" |
| 自动播放 | autoplay | boolean | 根据一般规则,用户和网页已有交互行为时方可自动播放 Example: false |
| 循环播放 | loop | boolean | Example: false |
| 倍速播放 | playbackRate | number | Example: 1 |
| 开始播放位置(单位:s) | startTime | number | Example: 0 |
Event Description
Events exposed by the component. You can listen to component events to trigger external actions
Event Name | Event Code | Event Output Parameters event.detail | Applicable Scenarios | Description |
|---|
| audio实例初始化完成 | audioReady | object
| Compatible with all platforms | audio 实例初始化完成时触发 |
| 开始/继续播放 | play | Compatible with all platforms | 当开始/继续播放时触发 play 事件 | |
| 结束播放 | stop | Compatible with all platforms | 当结束播放时触发 stop 事件 | |
| 暂停播放 | pause | Compatible with all platforms | 当暂停播放时触发 pause 事件 | |
| 播放报错 | error | object
| Compatible with all platforms | 当发生错误时触发 error 事件,小程序事件说明 https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.onError.html |
| 跳转到指定位置 | seeked | object
| Compatible with all platforms | 当音频跳转到指定位置时触发 seek 事件 |
| 播放到末尾 | ended | Compatible with all platforms | 当播放到末尾时触发 ended 事件 | |
| 播放进度变化 | timeupdate | object
| Compatible with all platforms | 播放进度变化时触发,event.detail = {currentTime, duration} |
Property API
Through the Property API, you can access the internal state and property values of components. You can access internal values using$w.componentId.propertyName, such as $w.input1.value. For details, please refer to Property API
Read-only Property Name | Property Identifier | Type | Description |
|---|
| Audio实例 | audioInstance | object | 获取 Audio 实例的引用,当前属性列表只例举了部分关键属性,其他更多的属性方法使用请参考 微信小程序端说明 |
| 以 00:00 形式返回当前媒体元素的播放时间 | currentTimeStr | string | |
| 以 00:00 形式给出媒体的长度 | durationStr | string | |
| 是否正在播放 | played | boolean |
Method API
Through the Method API, you can programmatically trigger internal methods of components, such as submitting forms, displaying popups, etc. You can call component methods using $w.componentId.methodName, such as $w.form1.submit()
Method Name | Method Identifier | Parameters | Method Description |
|---|
| 播放音频 | play | 通过 $w.audio1.play() 来播放音频,小程序 API 文档 https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.play.html | |
| 暂停播放 | pause | 通过 $w.audio1.pause() 来暂停播放,小程序 API 文档 https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.pause.html | |
| 停止播放 | stop | 通过 $w.audio1.stop() 来停止播放,停止后的音频再播放会从头开始播放,小程序 API 文档 https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.stop.html | |
| 跳转到指定位置 | seek | object
| 通过 $w.audio1.seek({ position } ) 来跳转到指定位置,小程序 API 文档https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.seek.html |
| 快进 | fastForward | object
| 通过 $w.audio1.fastForward({ time } ) 来快进一段指定时间 |
| 快退 | backward | object
| 通过 $w.audio1.backward({ time } ) 来快退一段指定时间 |
| 销毁当前实例 | destroy | 通过 $w.audio1.destroy() 来销毁当前实例,小程序 API 文档 https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.destroy.html | |
| 重新初始化当前实例 | init | 通过 $w.audio1.init() 来重新初始化当前实例,适用于通过 $w.audio1.destroy() 后的重新初始化 |
Style API
Through the Style API, you can override the styles of internal elements in components to achieve customization. For example, in the low-code editor, you can write styles for all button components using #wd-page-root .wd-btn, and control individual component styles with :scope. For detailed instructions, please refer toStyle API
Name | Class Name | Description and Examples |
|---|
| 根元素 | .wd-audio | audio组件根元素 |
| PC 端audio组件根元素 | .wd-pc-audio | 可以为 PC 端的audio编写样式 |
| H5 端audio组件根元素 | .wd-h5-audio | 可以为 H5 端的audio编写样式 |
| 小程序 端audio组件根元素 | .wd-mp-audio | 可以为 小程序 端的audio编写样式 |