微信小程序媒体组件(一)audio
来源:互联网 发布:java 获取jar包名称 编辑:程序博客网 时间:2024/06/16 19:22
周四没有课,写个博客庆祝一下
今天记录一下audio的基本使用,首先看下效果图。(声音请脑补一下~)
1.介绍一下audio属性(来自微信官方文档)
属性名 类型 默认值 说明
audio 组件的唯一标识符srcString
要播放音频的资源地址loopBooleanfalse是否循环播放controlsBooleanfalse是否显示默认控件posterString
默认控件上的音频封面的图片资源地址,如果 controls 属性值为 false 则设置 poster 无效nameString未知音频默认控件上的音频名字,如果 controls 属性值为 false 则设置 name 无效authorString未知作者默认控件上的作者名字,如果 controls 属性值为 false 则设置 author 无效binderrorEventHandle
当发生错误时触发 error 事件,detail = {errMsg: MediaError.code}bindplayEventHandle
当开始/继续播放时触发play事件bindpauseEventHandle
当暂停播放时触发 pause 事件bindtimeupdateEventHandle
当播放进度改变时触发 timeupdate 事件,detail = {currentTime, duration}bindendedEventHandle
当播放到末尾时触发 ended 事件2.一起看一下 audio.wxml
<!--pages/audio/audio.wxml--><audio id="myAudio" poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" bindplay="bindPlay" bindpause='bindPause' bindended='bindEnd' binderror='bindError' bindtimeupdate='bindTimeUpdate' controls loop></audio><button type="primary" class="audioButton" bindtap="audioPlay">播放</button><button type="primary" class="audioButton" bindtap="audioPause">暂停</button><button type="primary" class="audioButton" bindtap="audio14">设置当前播放时间为14秒</button><button type="primary" class="audioButton" bindtap="audioStart">回到开头</button>
①id为audio组件的唯一标识,在js中通过该id获取audio上下文context
this.audioCtx = wx.createAudioContext('myAudio')②poster、name、author、src为audio资源,详见属性表
③bindplay、bindpause、bindended监听audio的播放、暂停和结束,在js中进行实现
bindPlay: function () {//监听音乐开始/继续播放 console.log("<" + this.data.name + '>继续播放') }, bindPause: function () {//监听音乐暂停 console.log("<" + this.data.name + '>暂停播放') }, bindEnd: function () {//监听音乐播放结束 console.log("<" + this.data.name + '>结束播放') },
④binderror监听audio的错误
bindError: function (error) {//监听错误,错误信息error.detail.errMsg console.log(error.detail.errMsg) },其中errMsg有四种
返回错误码 描述
⑤bindtimeupdate监听播放时间的变化,单位为s
bindTimeUpdate:function(timeupdate){ console.log("当前播放位置:"+timeupdate.detail.currentTime+"s") },⑥loop为true则音乐自动循环播放
⑦controls控制播放窗口(红色框内部分)的可见性↓
2.完整的js代码↓
Page({ onReady: function (e) { // 使用 wx.createAudioContext 获取 audio 上下文 context this.audioCtx = wx.createAudioContext('myAudio') }, data: { // poster: '../../resources/image/隔壁团.jpg', // name: '夏天海边', // author: '隔壁团乐队', // src: '../../resources/audio/夏天海边.mp3', src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46', poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000', name: '此时此刻', author: '许巍' }, bindPlay: function () {//监听音乐开始/继续播放 console.log("<" + this.data.name + '>继续播放') }, bindPause: function () {//监听音乐暂停 console.log("<" + this.data.name + '>暂停播放') }, bindEnd: function () {//监听音乐播放结束 console.log("<" + this.data.name + '>结束播放') }, bindError: function (error) {//监听错误,错误信息error.detail.errMsg console.log("=================================") console.log(error.detail.errMsg) console.log("=================================") }, bindTimeUpdate:function(timeupdate){ console.log("当前播放位置:"+timeupdate.detail.currentTime+"s") }, audioPlay: function () {//点击“播放”触发 this.audioCtx.play() }, audioPause: function () {//点击“暂停”触发 this.audioCtx.pause() }, audio14: function () {//设置当前播放时间为14秒,seek单位为s this.audioCtx.seek(14) }, audioStart: function () { this.audioCtx.seek(0) }})
audioContext 对象的方法列表:
3.wxss样式文件↓
/* pages/audio/audio.wxss */.audioButton{ margin-left: 20px; margin-right: 20px; margin-top: 20px;}4.上面给出的js源码中使用了网络资源,我们也可以使用本地资源。建立一个与pages同级的资源文件夹。目录结构如下↓(但是需要注意,微信小程序有大小限制。太大资源会无法在实体机上预览)
使用本地资源后,其他功能都不受影响,但是seek(14)有bug,每次都是从头开始播放。不知道是不是小程序的bug。今天关于audio的部分就记录到这里啦~
阅读全文
1 0
- 微信小程序媒体组件(一)audio
- 微信小程序媒体组件(二)image
- 微信小程序媒体组件(三)video
- 微信小程序把玩(二十七)audio组件
- 微信小程序------媒体组件(视频,音乐,图片)
- 微信小程序组件:audio及api
- 微信小程序-使用audio组件播放音乐
- 小程序十:媒体组件
- 微信小程序学习:audio组件发现的几个问题
- 微信小程序例子——使用audio组件播放音乐
- 怎么控制媒体播放(<audio><video>)
- 微信小程序组件(一)srcoll-view
- 微信小程序把玩(三十四)Audio API
- 微信小程序 Audio API
- 微信小程序开发-图片(媒体)
- 微信小程序学习(一)之view组件
- 微信小程序—audio(音频)
- 【微信小程序】初识地图组件(一)
- 面向过程与面向对象
- genymotion模拟器 安装使用
- 深入理解Android Handler 消息机制
- java生成word的几种方案
- C语言经典算法87-94
- 微信小程序媒体组件(一)audio
- CentOS7挂载读写NTFS分区的方法(ntfs-3g)
- 存储过程输出查询语句过程的固定形式
- iPhone X都没能实现的功能!三星Note 9要做到了?
- ofo与摩拜单车合并?一张合照引发的预测
- 支付宝正式宣布重磅功能!这8个城市用户有福了
- WPS Word中怎么打出拼音和声调让别人懂的这个字怎么读
- LINUX中常用操作命令
- +图片添加,可改变行列数之挖矿与补坑之路