飞书对接网页应用语音(前端)

可以先查看飞书官方文档步骤:开发文档 - 飞书开放平台

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.监听录音的开始和结束,结束后会返回一个本地临时文件地址,通过getFileSystemManagerreadFile进行读取文件,是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.注意要使用识别语音文件的能力,需要在飞书开放平台-权限管理中申请语音识别能力

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值