audio

音频。
属性名类型默认值说明
idString video 组件的唯一标识符
srcString 要播放音频的资源地址
loopBooleanfalse是否循环播放
controlsBooleantrue是否显示默认控件
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 事件
MediaError.code
返回错误码描述
MEDIA_ERR_ABORTED获取资源被用户禁止
MEDIA_ERR_NETWORD网络错误
MEDIA_ERR_DECODE解码错误
MEDIA_ERR_SRC_NOT_SUPPOERTED不合适资源
 
my example:
<audio  src="{{src}}" action="{{action}}" controls="{{controls}}" ></audio>
  onReady:function () {
    //页面渲染完成
        this.reload(this.data.currentId);
    },
 reload:function (id) {
        var song = data[id] || {}; 
        console.log(song);       
        this.setData ({
            picurl: song.album.picUrl,
            src: song.mp3Url,
            controls:true,   //不显示默认控件
            action: {
                method: 'setCurrentTime',
                data: 0
            }
        });
        wx.setNavigationBarTitle({
          title: song.name
        });
        setTimeout(() => {   //延迟执行,不重复执行,实现音频自动播放
            this.setData({
                action:{
                    method:'play'
                }
            })
        },100);
    }
 
示例代码:
<!-- 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: {
    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)
  }
})

转载于:https://www.cnblogs.com/tian-sun/p/7405697.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值