音频内容需健康文明,相关规定可查看 。若发布后内容涉及违规,支付宝将根据小程序服务协议、本规则及相应细则对音频、开发者以及小程序采取相应管理措施。
基础库 或更高版本; 支付宝客户端 10.1.60 或更高版本; 若版本较低,建议采取 兼容处理 。
主体: 企业支付宝小程序 、 个人支付宝小程序
全局唯一的录音管理器。
可通过 my.getrecordermanager 获取。
录音管理器提供了开始录音、暂停录音、继续录音、停止录音等功能,还可以结合 音频播放 api 快速开发具有音频录制、音频播放功能的小程序(可参考下方代码示例或者下载 tips 中的快速示例),具体功能方法概览如下:
方法
名称 | 类型 | 兼容性 | 功能说明 |
---|---|---|---|
recordermanager.start | function | 基础库: 1.11.0 支付宝: 10.1.60 |
开始录音。 |
recordermanager.pause | function | 基础库: 1.11.0 支付宝: 10.1.60 |
暂停录音。 |
recordermanager.resume | function | 基础库: 1.11.0 支付宝: 10.1.60 |
继续录音,即恢复之前暂停的录音。 |
recordermanager.stop | function | 基础库: 1.11.0 支付宝: 10.1.60 |
停止录音。 |
recordermanager.onstart | function | 基础库: 1.11.0 支付宝: 10.1.80 |
监听录音开始事件。 |
recordermanager.offstart | function | 基础库: 1.11.0 支付宝: 10.1.80 |
取消监听录音开始事件。 |
recordermanager.onpause | function | 基础库: 1.11.0 支付宝: 10.1.80 |
监听录音暂停事件。 |
recordermanager.offpause | function | 基础库: 1.11.0 支付宝: 10.1.80 |
取消监听录音暂停事件。 |
recordermanager.onresume | function | 基础库: 1.11.0 支付宝: 10.1.80 |
监听录音继续事件。 |
recordermanager.offresume | function | 基础库: 1.11.0 支付宝: 10.1.80 |
取消监听录音继续事件。 |
recordermanager.onstop | function | 基础库: 1.11.0 支付宝: 10.1.80 |
监听录音停止事件。 |
recordermanager.offstop | function | 基础库: 1.11.0 支付宝: 10.1.80 |
取消监听录音停止事件。 |
recordermanager.onerror | function | 基础库: 1.11.0 支付宝: 10.1.80 |
监听录音错误事件。 |
recordermanager.offerror | function | 基础库: 1.11.0 支付宝: 10.1.80 |
取消监听录音错误事件。 |
recordermanager.onframerecorded | function | 基础库: 1.12.0 支付宝: 10.1.80 |
监听已录制完指定帧大小的文件事件。 |
recordermanager.offframerecorded | function | 基础库: 1.12.0 支付宝: 10.1.80 |
取消监听已录制完指定帧大小的文件事件。 |
recordermanager.ondecibelchange | function | 基础库: 2.6.2 支付宝: 10.2.0 |
监听声音分贝变化事件。 |
recordermanager.offdecibelchange | function | 基础库: 2.6.2 支付宝: 10.2.0 |
取消监听声音分贝变化事件。 |
<view>
<button ontap="startrecorder">开始录音button>
<button ontap="stoprecorder">停止录音button>
<button ontap="playaudio">播放前景音频button>
view>
const inneraudiocontext = my.createinneraudiocontext ();
const recordermanager = my.getrecordermanager ();
const fs = my.getfilesystemmanager ();
// 监听播放录音事件
inneraudiocontext.onplay = function () {
console.log ('audio onplay');
my.alert ({content: 'onplay'});
};
// 监听停止播放录音事件
inneraudiocontext.onstop = function () {
console.log ('audio onstop');
my.alert ({content: 'onstop'});
};
// 监听录音停止事件,获取录音文件地址
recordermanager.onstop (res => {
console.log ('recorder onstop', json.stringify (res));
my.alert ({
content: 'recorder onstop'
json.stringify (res)
' '
res['tempfilepath'],
});
// 保存本次录音文件地址
fs.savefile ({
tempfilepath: res['tempfilepath'],
success: function (res) {
my.alert ({content: 'recorder savefile success' json.stringify (res)});
},
fail: function (res) {
my.alert ({content: 'recorder savefile fail' json.stringify (res)});
},
complete: function (res) {
my.alert ({content: 'recorder savefile complete' json.stringify (res)});
},
});
// 将文件临时路径 tempfilepath 作为音频播放源
inneraudiocontext.src = res['tempfilepath'];
});
// 监听录发生错误停止事件
recordermanager.onerror (res => {
console.log ('recorder onerror' json.stringify (res));
my.alert ({content: 'recorder onerror' json.stringify (res)});
});
page ({
startrecorder () {
recordermanager.start ();
},
stoprecorder () {
recordermanager.stop ();
},
playaudio () {
my.alert ({content: inneraudiocontext.src '-----'});
inneraudiocontext.obeymuteswitch = false;
inneraudiocontext.play ();
},
stopaudio () {
inneraudiocontext.stop ();
},
});
<view>
<button ontap="startrecorder">开始录音button>
<button ontap="stoprecorder">停止录音button>
<button ontap="playaudio">播放前景音频button>
view>
const inneraudiocontext = my.createinneraudiocontext ();
const recordermanager = my.getrecordermanager ();
const fs = my.getfilesystemmanager ();
// 监听播放录音事件
inneraudiocontext.onplay = function () {
console.log ('audio onplay');
my.alert ({content: 'onplay'});
};
// 监听停止播放录音事件
inneraudiocontext.onstop = function () {
console.log ('audio onstop');
my.alert ({content: 'onstop'});
};
// 监听录音停止事件,获取录音文件地址
recordermanager.onstop (res => {
console.log ('recorder onstop', json.stringify (res));
my.alert ({
content: 'recorder onstop'
json.stringify (res)
' '
res['tempfilepath'],
});
// 保存本次录音文件地址
fs.savefile ({
tempfilepath: res['tempfilepath'],
success: function (res) {
my.alert ({content: 'recorder savefile success' json.stringify (res)});
},
fail: function (res) {
my.alert ({content: 'recorder savefile fail' json.stringify (res)});
},
complete: function (res) {
my.alert ({content: 'recorder savefile complete' json.stringify (res)});
},
});
// 将文件临时路径 tempfilepath 作为音频播放源
inneraudiocontext.src = res['tempfilepath'];
});
// 监听录发生错误停止事件
recordermanager.onerror (res => {
console.log ('recorder onerror' json.stringify (res));
my.alert ({content: 'recorder onerror' json.stringify (res)});
});
page ({
startrecorder () {
recordermanager.start ();
},
stoprecorder () {
recordermanager.stop ();
},
playaudio () {
my.alert ({content: inneraudiocontext.src '-----'});
inneraudiocontext.obeymuteswitch = false;
inneraudiocontext.play ();
},
stopaudio () {
inneraudiocontext.stop ();
},
});
可结合 音频播放 api,以前景音频播放为例,将 onstop 的 listener 回调参数 tempfilepath(文件临时路径)作为音频源 src 播放音频。
-
tip支付宝提供了简单的快速示例,方便开发者能够快速上手使用录音功能,可以点此下载 。