可以先查看飞书官方文档步骤:开发文档 - 飞书开放平台
1.引入jssdk
https://lf1-cdn-tos.bytegoofy.com/goofy/lark/op/h5-js-sdk-1.5.19.js
2.需要后端做飞书授权,然后返回jsapiTicket
3.拿到jsapiTicket后,调用 config 接口进行 JSAPI 鉴权
h5sdk,tt两个全局变量只有在飞书环境中才能调用,故最好用try catch包裹,否则容易报错
const { timestamp, noncestr, signature,appId } = jsapiTicket;
try{
window.h5sdk.config({
appId,
timestamp,
nonceStr: noncestr,
signature,
jsApiList:["getRecorderManager","getFileSystemManager",'createInnerAudioContext'],
//鉴权成功回调
onSuccess: (configRes) => {
console.log('config success',configRes);
},
//鉴权失败回调
onFail: (err) => {
console.log("config failed",err);
throw `config failed: ${JSON.stringify(err)}`;
},
});
}catch(e){}
4.查看用户录音授权信息
try{
tt.authorize({
scope: "scope.record",
success(res) {
console.log('用户授权信息======',res);
that.doFsAudio()
},
fail(res) {
that.$messageBox.Error({
message: "用户拒绝授权录音",
duration: 1000
});
}
});
}catch(e){}
5.用户授权后,调用getRecorderManager进行录音,可查看官方文档:开发文档 - 飞书开放平台
that.recorderManager = tt.getRecorderManager();
that.recorderManager.start();
that.recorderManager.stop();
6.监听录音的开始和结束,结束后会返回一个本地临时文件地址,通过getFileSystemManager的readFile进行读取文件,是ArrayBuffer格式,将它转化为file文件传给后端,后端调用识别语音文件(ASR)的能力进行翻译后,再传给前端,服务端调用接口能力文档:开发文档 - 飞书开放平台
that.recorderManager.onStop(async (res) => {
console.log('recorder stop', res)
const { tempFilePath } = res
const fileSystemManager = tt.getFileSystemManager();
fileSystemManager.readFile({
filePath: tempFilePath,
success(fileRes) {
console.log('读取到了文件=====',fileRes.data);
const bufferFile = new Uint8Array(fileRes.data)
const file = new File([bufferFile],'语音文件')
//这里是调用的后端翻译接口
commonDataClass.fsAudioToText(file,txtRes =>{
console.log('文字是=====',txtRes);
})
},
fail(fileRes) {
console.error("读取失败", fileRes);
},
});
});
7.注意要使用识别语音文件的能力,需要在飞书开放平台-权限管理中申请语音识别能力