audio

audio为音频组件,我们可以轻松的在小程序中播放音频。

  • audio组件属性如下:
属性名 类型 默认值 说明
id String   video 组件的唯一标识符,
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 不合适资源
  • wx.createAudioContext(audioId)

    创建并返回audio上下文audioContext对象,控制音频的播放暂停与跳转。

方法 参数 说明
play 播放
pause 暂停
seek position 跳转到指定位置,单位 s
  • wxml
<!--
poster:音频封面图片
name:歌名
author:歌手
src:音频地址
controls:是否显示默认控件,也就是下面这个东东
    loop:是否循环播放
id:标注唯一组件以this.audioCtx = wx.createAudioContext('myAudio')获取控制组件的对象。
bindplay:播放时触发该事件
bindpause:停止时触发该事件
bindtimeupdate:时间改变时触发,该函数携带有参数detail={currentTime, duration}当前时间,持续播放时间
bindended:播放结束时触发
binderror;播放错误时调用,携带参数detail = {errMsg: MediaError.code} -->
<audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}" id="myAudio" controls loop bindplay="funplay" bindpause="funpause" bindtimeupdate="funtimeupdate" bindended="funended" binderror="funerror"></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>
  • 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)
},
funplay: function(){
console.log("audio play");
},
funpause: function(){
console.log("audio pause");
},
funtimeupdate: function(u){
console.log(u.detail.currentTime);
console.log(u.detail.duration);
},
funended: function(){
console.log("audio end");
},
funerror: function(u){
console.log(u.detail.errMsg);
}
})
  • 效果

今天早上发现微信小程序的官方文档在实时跟新,之前的有些标签或者方法不见了。以上是控制audio组件的方法是根据组件的唯一id生成相应的实例对象,通过对象的各种发放来控制组件。我现在看到的官方文档是通过有个action属性,给属性指定特定的值组件就会执行特定的动作。

method 描述 data
play 播放  
pause 暂停  
setPlaybackRate 调整速度 倍速
setCurrentTime 设置当前时间 播放时间
  • .wxml
<audio poster="{{poster}}" name="{{name}}" author="{{author}}" src="http://qqma.tingge123.com:823/mp3/2015-06-13/1434188181.mp3" action="{{action}}" controls loop></audio>

<button type="primary" bindtap="audioPlay">播放</button>
<button type="primary" bindtap="audioPause">暂停</button>
<button type="primary" bindtap="audioPlaybackRateSpeedUp">调为2倍速</button>
<button type="primary" bindtap="audioPlaybackRateNormal">调为1倍速</button>
<button type="primary" bindtap="audioPlaybackRateSlowDown">调为0.5倍速</button>
<button type="primary" bindtap="audio14">设置当前播放时间为14秒</button>
<button type="primary" bindtap="audioStart">回到开头</button>
  • .js
Page({
data: {
poster: 'http://pic.pimg.tw/pam86591/1408719752-3322564110_n.jpg',
name: 'Sugar',
author: 'Maroon 5'
},
audioPlay: function () {
this.setData({
action: {
method: 'play'
}
});
},
audioPause: function () {
this.setData({
action: {
method: 'pause'
}
});
},
audioPlaybackRateSpeedUp: function () {
this.setData({
action: {
method: 'setPlaybackRate',
data: 2//加快速度
}
});
},
audioPlaybackRateSlowDown: function () {
this.setData({
action: {
method: 'setPlaybackRate',
data: 0.5//小于零放慢速度
}
});
},
audio14: function () {
this.setData({
action: {
method: 'setCurrentTime',
data: 14
}
});
},
audioStart: function () {
this.setData({
action: {
method: 'setCurrentTime',
data: 0
}
});
}
})
  • 效果

上一种方法好像没有不能控制播放速度,这种方法相比上一种比较方便,并且效率上应该也比较高。

微信小程序之----audio音频播放的更多相关文章

  1. 微信小程序——实现动画循环播放

    今天在做砍价页面的时候需要将一个按钮动起来,效果图如下: 其实它实现的原理很简单,就是循环的缩小放大. css3中的animate 有个属性是 animation-iteration-count 可以 ...

  2. 微信小程序 写音乐播放器 slider组件 将value设置为0 真机测试滑块不能回到起点

    最近在用微信小程序写一个音频播放页面,做时间进度的时候用到了slider插件,但是在自然播放完成,或者上/下切换的时候,将slider的value属性值设为0,开发工具上滑块会回到起点,有效.但是真机 ...

  3. 微信小程序_(校园视)开发视频的展示页_上

    微信小程序_(校园视) 开发用户注册登陆 传送门 微信小程序_(校园视) 开发上传视频业务 传送门 微信小程序_(校园视) 开发视频的展示页-上 传送门 微信小程序_(校园视) 开发视频的展示页-下 ...

  4. [小程序开发] 微信小程序audio音频播放组件+api_wx.createAudioContext

    引言: audio是微信小程序中的音频组件,可以轻松实现小程序中播放/停止音频等自定义动作. 附上微信小程序audio组件的相关属性说明:https://mp.weixin.qq.com/debug/ ...

  5. 微信小程序音频背景播放

    由于微信小程序官方将音频的样式固定死了,往往再工作中和UI设计师设计出来的样式不符,故一般都采用背景音频播放来实现自定义的UI样式的音频播放,即使用官网API提供的BackgroundAudioMan ...

  6. 微信小程序video组件出现无法播放或卡顿

    微信小程序使用video组件播放视频的时候,会出现卡顿或者无法播放的问题,加一个custom-cache=”true“即可解决,这个属性文档上没有,是从小程序开发社区中get到的.

  7. 微信小程序 教程及示例

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有,转载请联系作者获得授权.微信小程序正式公测, ...

  8. 微信小程序,前端大梦想(八)

    微信小程序之多媒体实例-播放器 播放音频和视频的功能也是小程序的特色,API也十分简单,本节我们一起来开发一个播放网络音乐的功能.API如下: 属性名 类型 默认值 说明 id String audi ...

  9. 微信小程序踩坑集合

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教程:https://mp.weixin.qq.com/debu ...

随机推荐

  1. 转:webdriver驱动未在默认目录安装的firefox

    刚开始用webdriver的朋友一定会苦恼它时常不能启动firefox,很多时候是因为firefox安装在默认路径下.此时,我们有些常用方法,可以解决此问题. [1] System.setProper ...

  2. HDU 3265 Posters ——(线段树+扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...

  3. seo步骤

    1. 关键词.找trends主词.去渣渣.(扩展).(去重tool sort) .打乱 :https://adwords.google.com/https://www.google.com/trend ...

  4. svn log操作

    查看当前文件夹的最近N次提交记录 svn update; svn log --limit <N> -v 含义是:查询最近N次提交记录的详细信息,包括版本号,提交文件列表,log信息 对比某 ...

  5. IP 网际协议

    1. IP数据首部长度: 4位版本号 : 4 位,用于标明 IP 版本号,0100 表示 IPv4,0110 表示 IPv6.目前常见的是 IPv4. 4位首部长度: 首部长度是占4位,可表示的最大十 ...

  6. js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。

    一.将整数转成字符: String.fromCharCode(17496>>8,17496&0xFF,19504>>8,19504&0xFF,12848> ...

  7. CentOS 6.3下NTP服务安装和配置

    测试环境: NTPserver 192.168.1.252 NTPclient 192.168.1.251 准备工作: 关闭selinux: vi /etc/selinux/config SELINU ...

  8. 清除浮动的方法 after伪类。

    .clearfix{ *zoom:1; } .clearfix:after{ content: ""; display: block; clear: both; } 在样式中加入上 ...

  9. decoder3_8

    这两天回归书本,继续阅读书上的内容,此时的体会与刚开始学那会的体会是不一样的,比如3_8decoder,之前就认为可以用case来写,而书上有一种更简便的方式来描述,带给你新的思路,既然有新方式可以描 ...

  10. jdk8 eclipse luna market crashed

    THAT WORKS! Eclipse Luna starts normally when I first do the suggested: export SWT_GTK3=0 https://bu ...