从基础库 1.6.0 开始,本接口停止维护,请使用 wx.createInnerAudioContext 代替
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
功能描述
创建 audio 上下文 AudioContext 对象。
参数
string id
audio 组件的 id
Object this
在自定义组件下,当前组件实例的this,以操作组件内 audio 组件
从基础库 1.6.0 开始,本接口停止维护,请使用 wx.createInnerAudioContext 代替
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
创建 audio 上下文 AudioContext 对象。
audio 组件的 id
在自定义组件下,当前组件实例的this,以操作组件内 audio 组件
基础库 1.6.0 开始支持,低版本需做兼容处理。
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
微信 Windows 版:支持
微信 Mac 版:支持
微信 鸿蒙 OS 版:支持
创建内部 audio 上下文 InnerAudioContext 对象。
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
|---|---|---|---|---|---|
| useWebAudioImplement | boolean | false | 否 | 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项。 | 2.19.0 |
const innerAudioContext = wx.createInnerAudioContext({
useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项
})
innerAudioContext.src = 'https://wx_test.mp3'
innerAudioContext.play() // 播放
innerAudioContext.pause() // 暂停
innerAudioContext.stop() // 停止
innerAudioContext.destroy() // 释放音频资源
基础库 2.13.0 开始支持,低版本需做兼容处理。
微信小程序插件:支持
创建媒体音频播放器对象 MediaAudioPlayer 对象,可用于播放视频解码器 VideoDecoder 输出的音频。
// 创建视频解码器,具体参数见 createVideoDecoder 文档
const videoDecoder = wx.createVideoDecoder()
// 创建媒体音频播放器
const mediaAudioPlayer = wx.createMediaAudioPlayer()
// 启动视频解码器
videoDecoder.start()
// 启动播放器
mediaAudioPlayer.start().then(() => {
// 添加播放器音频来源
mediaAudioPlayer.addAudioSource(videoDecoder).then(res => {
videoDecoder.getFrameData() // 建议在 requestAnimationFrame 里获取每一帧视频数据
console.log(res)
})
// 移除播放器音频来源
mediaAudioPlayer.removeAudioSource(videoDecoder).then()
// 停止播放器
mediaAudioPlayer.stop().then()
// 销毁播放器
mediaAudioPlayer.destroy().then()
// 设置播放器音量
mediaAudioPlayer.volume = 0.5
})
基础库 2.19.0 开始支持,低版本需做兼容处理。
微信小程序插件:不支持
微信 Windows 版:支持
微信 Mac 版:支持
创建 WebAudio 上下文。
一个简单的播放demo
const audioCtx = wx.createWebAudioContext()
const loadAudio = (url) => {
return new Promise((resolve) => {
wx.request({
url,
responseType: 'arraybuffer',
success: res => {
console.log('res.data', res.data)
audioCtx.decodeAudioData(res.data, buffer => {
resolve(buffer)
}, err => {
console.error('decodeAudioData fail', err)
reject()
})
},
fail: res => {
console.error('request fail', res)
reject()
}
})
})
}
const play = () => {
loadAudio('xxx-test.mp3').then(buffer => {
let source = audioCtx.createBufferSource()
source.buffer = buffer
source.connect(audioCtx.destination)
source.start()
}).catch(() => {
console.log('fail')
})
}
play()
基础库 2.1.0 开始支持,低版本需做兼容处理。
以 Promise 风格 调用:支持
微信小程序插件:支持,需要微信小程序基础库版本不低于 2.15.0
获取当前支持的音频输入源
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
| 属性 | 类型 | 说明 | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| audioSources | Array.<string> | 支持的音频输入源列表,可在 RecorderManager.start() 接口中使用。返回值定义参考 https://developer.android.com/reference/kotlin/android/media/MediaRecorder.AudioSource | |||||||||||||||||
| |||||||||||||||||||
从基础库 1.6.0 开始,本接口停止维护,请使用 wx.createInnerAudioContext 代替
以 Promise 风格 调用:支持
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
暂停正在播放的语音。再次调用 wx.playVoice 播放同一个文件时,会从暂停处开始播放。如果想从头开始播放,需要先调用 wx.stopVoice。
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath
})
setTimeout(() => { wx.pauseVoice() }, 5000)
}
})
从基础库 1.6.0 开始,本接口停止维护,请使用 wx.createInnerAudioContext 代替
以 Promise 风格 调用:支持
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
开始播放语音。同时只允许一个语音文件正在播放,如果前一个语音文件还没播放完,将中断前一个语音播放。
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
|---|---|---|---|---|---|
| filePath | string | 是 | 需要播放的语音文件的文件路径 (本地路径) | ||
| duration | number | 60 | 否 | 指定播放时长,到达指定的播放时长后会自动停止播放,单位:秒 | 1.6.0 |
| success | function | 否 | 接口调用成功的回调函数 | ||
| fail | function | 否 | 接口调用失败的回调函数 | ||
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
complete () { }
})
}
})
基础库 2.3.0 开始支持,低版本需做兼容处理。
以 Promise 风格 调用:支持
微信小程序插件:支持,需要微信小程序基础库版本不低于 2.10.0
设置 InnerAudioContext 的播放选项。设置之后对当前微信小程序全局生效。
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| mixWithOther | boolean | true | 否 | 是否与其他音频混播,设置为 true 之后,不会终止其他应用或微信内的音乐 |
| obeyMuteSwitch | boolean | true | 否 | (仅在 iOS 生效)是否遵循静音开关,设置为 false 之后,即使是在静音模式下,也能播放声音 |
| speakerOn | boolean | true | 否 | true 代表用扬声器播放,false 代表听筒播放,默认值为 true。 |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
从基础库 1.6.0 开始,本接口停止维护,请使用 wx.createInnerAudioContext 代替
以 Promise 风格 调用:支持
微信小程序插件:支持,需要微信小程序基础库版本不低于 1.9.6
结束播放语音。
| 属性 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
})
setTimeout(() => { wx.stopVoice() }, 5000)
}
})