微信小程序之音乐播放
这次用的是API中的wx.createInnerAudioContext()接口 (创建内部 audio 上下文 InnerAudioContext 对象。)
InnerAudioContext 对象中的属性
属性 | |
---|---|
string src | 音频资源的地址,用于直接播放。2.2.3 开始支持云文件ID |
number startTime | 开始播放的位置(单位:s),默认为 0 |
boolean autoplay | 是否自动开始播放,默认为 false |
boolean loop | 是否循环播放,默认为 false |
boolean obeyMuteSwitch | 是否遵循系统静音开关,默认为 true。当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音。从 2.3.0 版本开始此参数不生效,使用 wx.setInnerAudioOption 接口统一设置。 |
number volume | 音量。范围 0~1。默认为 1基础库 1.9.90 开始支持,低版本需做兼容处理。 |
number playbackRate | 播放速度。范围 0.5-2.0,默认为 1。(Android 需要 6 及以上版本)基础库 2.11.0 开始支持,低版本需做兼容处理。 |
number duration | 当前音频的长度(单位 s)。只有在当前有合法的 src 时返回(只读) |
number currentTime | 当前音频的播放位置(单位 s)。只有在当前有合法的 src 时返回,时间保留小数点后 6 位(只读) |
boolean paused | 当前是是否暂停或停止状态(只读) |
number buffered | 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲(只读) |
InnerAudioContext 对象中的方法
方法 | |
---|---|
InnerAudioContext.play() | 播放 |
InnerAudioContext.pause() | 暂停。暂停后的音频再播放会从暂停处开始播放 |
InnerAudioContext.stop() | 停止。停止后的音频再播放会从头开始播放。 |
InnerAudioContext.seek(number position) | 跳转到指定位置 |
InnerAudioContext.destroy() | 销毁当前实例 |
InnerAudioContext.onCanplay(function callback) | 监听音频进入可以播放状态的事件。但不保证后面可以流畅播放 |
InnerAudioContext.offCanplay(function callback) | 取消监听音频进入可以播放状态的事件 |
InnerAudioContext.onPlay(function callback) | 监听音频播放事件 |
InnerAudioContext.offPlay(function callback) | 取消监听音频播放事件 |
InnerAudioContext.onPause(function callback) | 监听音频暂停事件 |
InnerAudioContext.offPause(function callback) | 取消监听音频暂停事件 |
InnerAudioContext.onStop(function callback) | 监听音频停止事件 |
InnerAudioContext.offStop(function callback) | 取消监听音频停止事件 |
InnerAudioContext.onEnded(function callback) | 监听音频自然播放至结束的事件 |
InnerAudioContext.offEnded(function callback) | 取消监听音频自然播放至结束的事件 |
InnerAudioContext.onTimeUpdate(function callback) | 监听音频播放进度更新事件 |
InnerAudioContext.offTimeUpdate(function callback) | 取消监听音频播放进度更新事件 |
InnerAudioContext.onError(function callback) | 监听音频播放错误事件 |
InnerAudioContext.offError(function callback) | 取消监听音频播放错误事件 |
InnerAudioContext.onWaiting(function callback) | 监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发 |
InnerAudioContext.offWaiting(function callback) | 取消监听音频加载中事件 |
InnerAudioContext.onSeeking(function callback) | 监听音频进行跳转操作的事件 |
InnerAudioContext.offSeeking(function callback) | 取消监听音频进行跳转操作的事件 |
InnerAudioContext.onSeeked(function callback) | 监听音频完成跳转操作的事件 |
InnerAudioContext.offSeeked(function callback) | 取消监听音频完成跳转操作的事件 |
支持音频格式
格式 | iOS | Android |
---|---|---|
flac | x | √ |
m4a | √ | √ |
ogg | x | √ |
ape | x | √ |
amr | x | √ |
wma | x | √ |
wav | √ | √ |
mp3 | √ | √ |
mp4 | x | √ |
aac | √ | √ |
aiff | √ | x |
caf | √ | x |
//声明一个公共的API接口
var bgam = wx.createInnerAudioContext();
var id = 1
//在page js中这样写
Page({
onLoad: function (options) {
//在页面家在方法中写出播放文件的地址
bgam.src = "http://antiserver.kuwo.cn/anti.s?useless=/resource/&format=mp3&rid=MUSIC_28428497&response=res&type=convert_url&"
//默认在页面加载完成后就开始播放
bgam.play()
}
//控制音乐播放暂停的方法
music: function () {
//判断id的初始值是多少(id初始值给的是1代表播放)
if (id == 1) {
//如果id等于一的话代表正在播放就用.push的方法暂停,然后再给id赋值为0代表音乐暂停
bgam.pause()
id = 0
} else {
//如果id不等于一的话代表音乐暂停就用.play的方法播放,然后再给id赋值为1代表音乐播放
bgam.play()
id = 1
}
},
})