音频。
属性名 | 类型 | 默认值 | 说明 |
---|---|---|---|
id | string | audio 组件的唯一标识符 | |
src | string | 要播放音频的资源地址 | |
loop | boolean | false | 是否循环播放 |
controls | boolean | true | 是否显示默认控件 |
poster | string | 默认控件上的音频封面的图片资源地址,如果 controls 属性值为 false 则设置 poster 无效 | |
name | string | 未知音频 | 默认控件上的音频名字,如果 controls 属性值为 false 则设置 name 无效 |
author | string | 未知作者 | 默认控件上的作者名字,如果 controls 属性值为 false 则设置 author 无效 |
binderror | eventhandle | 当发生错误时触发 error 事件,detail = {errmsg: mediaerror.code} | |
bindplay | eventhandle | 当开始/继续播放时触发play事件 | |
bindpause | eventhandle | 当暂停播放时触发 pause 事件 | |
bindtimeupdate | eventhandle | 当播放进度改变时触发 timeupdate 事件,detail = {currenttime, duration} | |
bindended | eventhandle | 当播放到末尾时触发 ended 事件 |
mediaerror.code
返回错误码 | 描述 |
---|---|
media_err_aborted | 获取资源被用户禁止 |
media_err_netword | 网络错误 |
media_err_decode | 解码错误 |
media_err_src_not_suppoerted | 不合适资源 |
示例代码:
<!-- audio.wxml -->
<audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myaudio" controls loop></audio>
<button type="primary" bindtap="audioplay">播放</button>
<button type="primary" bindtap="audiopause">暂停</button>
<button type="primary" bindtap="audio14">设置当前播放时间为14秒</button>
<button type="primary" bindtap="audiostart">回到开头</button>
// audio.js
page({
onready: function (e) {
// 使用 wx.createaudiocontext 获取 audio 上下文 context
this.audioctx = wx.createaudiocontext('myaudio')
},
data: {
poster: 'http://y.gtimg.cn/music/photo_new/t002r300x300m000003rskf44gyask.jpg?max_age=2592000',
name: '此时此刻',
author: '许巍',
src: 'http://ws.stream.qqmusic.qq.com/m500001vfvsj21xfqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292f51e1e384e06dcbdc9ab7c49fd713d632d313ac4858bacb8ddd29067d3c601481d36e62053bf8dfeaf74c0a5ccfadd6471160caf3e6a&fromtag=46',
},
audioplay: function () {
this.audioctx.play()
},
audiopause: function () {
this.audioctx.pause()
},
audio14: function () {
this.audioctx.seek(14)
},
audiostart: function () {
this.audioctx.seek(0)
}
})
相关api:wx.createaudiocontext