首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >微信小程序官方组件展示之媒体组件audio源码

微信小程序官方组件展示之媒体组件audio源码

作者头像
软件事业部
发布于 2022-09-29 10:32:30
发布于 2022-09-29 10:32:30
9750
举报

以下将展示微信小程序之媒体组件audio源码官方组件能力,组件样式仅供参考,开发者可根据自身需求定义组件样式,具体属性参数详见小程序开发文档。

功能描述:

音频。

属性说明:

属性

类型

默认值

必填

说明

最低版本

id

string

否

audio 组件的唯一标识符

1.0.0

src

string

否

要播放音频的资源地址

1.0.0

loop

boolean

FALSE

否

是否循环播放

1.0.0

controls

boolean

FALSE

否

是否显示默认控件

1.0.0

poster

string

否

默认控件上的音频封面的图片资源地址,如果 controls 属性值为 false 则设置 poster 无效

1.0.0

name

string

未知音频

否

默认控件上的音频名字,如果 controls 属性值为 false 则设置 name 无效

1.0.0

author

string

未知作者

否

默认控件上的作者名字,如果 controls 属性值为 false 则设置 author 无效

1.0.0

binderror

eventhandle

否

当发生错误时触发 error 事件,detail = {errMsg:MediaError.code}

1.0.0

bindplay

eventhandle

否

当开始/继续播放时触发 play 事件

1.0.0

bindpause

eventhandle

否

当暂停播放时触发 pause 事件

1.0.0

bindtimeupdate

eventhandle

否

当播放进度改变时触发 timeupdate 事件,detail = {currentTime, duration}

1.0.0

bindended

eventhandle

否

当播放到末尾时触发 ended 事件

1.0.0

MediaError.code

返回错误码

描述

1

获取资源被用户禁止

2

网络错误

3

解码错误

4

不合适资源

示例代码

JAVASCRIPT

代码语言:javascript
复制
// 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)
  }
})

WXML

代码语言:javascript
复制
<!-- 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>

版权声明: 本站所有内容均由互联网收集整理、上传,如涉及版权问题,请联系我们第一时间处理。

原文链接地址:https://developers.weixin.qq.com/miniprogram/dev/component/audio.html

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档