codecamp

百度智能小程序 音频

audio 音频

解释:音频

属性说明

属性名类型默认值必填说明

id

String

audio 组件的唯一标识符

src

String

要播放音频的资源地址

loop

Boolean

false

是否循环播放

controls

Boolean

false

是否显示默认控件

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 事件,detail = {duration}

bindpause

EventHandle

当暂停播放时触发 pause 事件,detail = {duration}

bindtimeupdate

EventHandle

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

bindended

EventHandle

当播放到末尾时触发 ended 事件,detail = {duration}

MediaError.code

返回错误码描述

1

获取资源过程被用户终止

2

网络错误

3

当解码时发生错误

4

不支持音频

示例 

在开发者工具中打开


<audio 
    poster="{{poster}}" 
    name="{{name}}" 
    loop="false" 
    author="{{author}}" 
    src="{{src}}" 
    id="myAudio" 
    controls 
    bind:error="audioError" 
    bind:play="audioPlay" 
    bind:pause="audioPause" 
    bind:timeupdate="audioTimeUpdate" 
    bind:ended="audioEnded">
</audio>
Page({
    data: {
        poster: 'https://b.bdstatic.com/searchbox/icms/searchbox/img/xuezhiqian.jpg',
        name: '演员',
        author: '薛之谦',
        src: 'https://b.bdstatic.com/miniapp/images/yanyuan.mp3'
    },
    audioError(e) {
        console.log('audio ', e.type);
        swan.showToast({
            title: '加载音频资源出错',
            duration: 1000,
            icon: 'none'
        });
    },
    audioPlay(e) {
        console.log('audio ', e.type);
        swan.showToast({
            title: '音频开始播放',
            duration: 1000,
            icon: 'none'
        });
    },
    audioPause(e) {
        console.log('audio ', e.type);
        swan.showToast({
            title: '音频暂停',
            duration: 1000,
            icon: 'none'
        });
    },
    audioEnded(e) {
        console.log('audio ', e.type);
        swan.showToast({
            title: '音频播放完',
            duration: 1000,
            icon: 'none'
        });
    },
    audioTimeUpdate(e) {
        console.log('audio ', e.type);
    }
});


百度智能小程序 动画
百度智能小程序 相机
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }