微信小程序录音直传阿里云OSS并语音识别

前言

如题:做一个录音文字识别功能,知识点有三个,分别是微信小程序的录音功能、录音文件直传阿里云OSS、使用阿里云的录音文件识别接口返回识别后的文字


一、微信小程序录音

官方文档:微信小程序全局唯一的录音管理器 RecorderManager

wxml:

<view style="padding-top:40%"></view>
<button bindtap="start">开始录音</button>
<!-- <button bindtap="pause">暂停录音</button> -->
<!-- <button bindtap="resume">继续录音</button> -->
<view style="padding-top:10px"></view>
<button bindtap="stop">停止录音并识别</button>
<!-- <button bindtap="play">播放录音[本地]</button> -->
<!-- <button bindtap="play">播放录音[线上]</button> -->
<view style="text-align:center;padding-top:20px">{{text}}</view>

js:需要注意的是,点击开始录音时要判断当前是否获取到了录音权限,如果没有录音权限进行提示,引导用户重新授权

const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()

Page({
  data: {
    text: '语音识别后文字展示'
  },
  onLoad: function () {
  },
  //开始录音的时候
  start: function () {
    var that = this
    const options = {
      duration: 100000, //指定录音的时长,单位 ms
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式,有效值 aac/mp3
      frameSize: 50, //指定帧大小,单位 KB
    }
    //开始录音
    wx.authorize({
      scope: 'scope.record',
      success() {
        console.log("录音授权成功");
        //第一次成功授权后 状态切换为2
        that.setData({
          status: 2,
        })
        recorderManager.start(options);
        recorderManager.onStart(() => {
          //可以弹出模态框等友好提示
          console.log('录音开始')
        });
        //错误回调
        recorderManager.onError((res) => {
          //进行错误提示
          console.log(res);
        })
      },
      fail() {
        console.log("第一次录音授权失败");
        wx.showModal({
          title: '提示',
          content: '您未授权录音,功能将无法使用',
          showCancel: true,
          confirmText: "授权",
          confirmColor: "#52a2d8",
          success: function (res) {
            if (res.confirm) {
              //确认则打开设置页面(重点)
              wx.openSetting({
                success: (res) => {
                  console.log(res.authSetting);
                  if (!res.authSetting['scope.record']) {
                    //未设置录音授权
                    console.log("未设置录音授权");
                    wx.showModal({
                      title: '提示',
                      content: '您未授权录音,功能将无法使用',
                      showCancel: false
                    })
                  } else {
                    //第二次才成功授权
                    console.log("设置录音授权成功");
                    that.setData({
                      status: 2
                    })
                  }
                },
                fail: function () {
                  console.log("授权设置录音失败");
                }
              })
            } else if (res.cancel) {
              console.log("cancel");
            }
          },
          fail: function () {
            console.log("openfail");
          }
        })
      }
    })
  },
  //暂停录音-需求不需要暂停
  pause: function () {
    recorderManager.pause();
    recorderManager.onPause((res) => {
      console.log('暂停录音')
    })
  },
  //继续录音-不需要暂停也就不需要继续
  resume: function () {
    recorderManager.resume();
    recorderManager.onStart(() => {
      console.log('重新开始录音')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  //停止录音
  stop: function () {
    recorderManager.stop();
    recorderManager.onStop((res) => {
      console.log('停止录音', res.tempFilePath)
      this.getPolicy(res.tempFilePath)
    })
  },
  //播放声音
  play: function () {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  }
})

二、微信小程序录音直传阿里云OSS

官方文档:微信小程序直传实践

如文档所示,首先进行配置Bucket跨域访问,这个不做解释,其次在微信小程序中配置域名白名单也就是uploadFile和downloadFile合法域名为对应Bucket的外网访问域名

Javascript直传OSS需要提前获取policy,服务端如下:

  public Map<String, String> ossPolicy() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    String path = "data/" + dateFormat.format(date) + "/";
    try {
      long expireTime = 3000;
      long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
      Date expiration = new Date(expireEndTime);
      PolicyConditions policyConds = new PolicyConditions();
      policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
      policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, path);

      String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
      byte[] binaryData = postPolicy.getBytes("utf-8");
      String encodedPolicy = BinaryUtil.toBase64String(binaryData);
      String postSignature = ossClient.calculatePostSignature(postPolicy);

      Map<String, String> respMap = new LinkedHashMap<String, String>();
      respMap.put("accessid", ossClient.getCredentialsProvider().getCredentials().getAccessKeyId());
      respMap.put("policy", encodedPolicy);
      respMap.put("signature", postSignature);
      respMap.put("dir", path);
      respMap.put("host", configService.findConfigValueBySuffix(ConfigConstant.CONFIG_SUFFIX_RESOURCE_URL));
      respMap.put("expire", String.valueOf(expireEndTime / 1000));
      return respMap;
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

获取到policy以及签名等后,直传录音文件到阿里云OSS:

  getPolicy: function (path) {
    wx.request({
      url: 'https://xxx.com/oss/policy',
      method: 'GET',
      success: (res) => {
        console.log('获取policy成功' + path)
        this.upload(res, path)
      }
    })
  },
  upload: function (res, path) {
    const host = res.data.datas.host;
    const signature = res.data.datas.signature;
    const ossAccessKeyId = res.data.datas.accessid;
    const policy = res.data.datas.policy;
    //这里的key实际是新起的文件名和路径
    const key = res.data.datas.dir + Date.now() + '.mp3';
    wx.uploadFile({
      url: host,
      filePath: path,
      name: 'file', // 必须填file。
      formData: {
        key,
        policy,
        OSSAccessKeyId: ossAccessKeyId,
        signature,
      },
      success: (res) => {
        console.log(res);
        if (res.statusCode === 204) {
          console.log('上传成功');
          this.toText(key)
        }
      },
      fail: err => {
        console.log(err);
      }
    });
  }

三、阿里云录音文字识别极速版

进行录音识别之前需要先创建智能语音交互服务对应的项目,获得AppKey

官方文档:创建智能语音交互服务项目

官方文档:录音文件识别极速版

由于将录音文件直传OSS了,所以不需要将录音文件上传到语音识别里,所以采取第二种方式,使用音频文件链接进行语音识别:

  public ResultBody record2text(String recordUrl) throws Exception {
    Assert.isBlank(recordUrl, JobErrorInfoEnum.PARAM_NOT_NULL);

    String prefix = configService.findConfigValueBySuffix(ConfigConstant.CONFIG_SUFFIX_RESOURCE_URL);

    AccessToken accessToken = new AccessToken(id, secret);
    accessToken.apply();
    String token = accessToken.getToken();

    String fileName = prefix + recordUrl;
    String format = "mp3";
    int sampleRate = 16000;

    String allText = process(fileName, appKey, token, format, sampleRate);
    Assert.isBlank(allText, JobErrorInfoEnum.RECORD_TRANS_ERROR);

    return ResultBody.success(allText);
  }

  public String process(String fileName, String appKey, String token, String format, int sampleRate) {
    /**
     * 设置HTTPS REST POST请求
     * 1.使用http协议
     * 2.语音识别服务域名:nls-gateway.cn-shanghai.aliyuncs.com
     * 3.语音识别接口请求路径:/stream/v1/FlashRecognizer
     * 4.设置必须请求参数:appkey、token、format、sample_rate
     */
    String request = url;
    request = request + "?appkey=" + appKey;
    request = request + "&token=" + token;
    request = request + "&format=" + format;
    request = request + "&sample_rate=" + sampleRate;

    /**
     * 设置HTTPS头部字段
     * 发送HTTPS POST请求,返回服务端的响应。
     *
     * 1.Content-Type:application/octet-stream
     */
    HashMap<String, String> headers = new HashMap<String, String>();
    String response;
    if (new File(fileName).isFile()) {
      headers.put("Content-Type", "application/octet-stream");
      response = HttpUtil.sendPostFile(request, headers, fileName);
    } else {
      headers.put("Content-Type", "application/text");
      response = HttpUtil.sendPostLink(request, headers, fileName);
    }

    String allText = "";
    if (response != null) {
      Map map = JsonUtils.json2Map(response);
      if ((int) map.get("status") == 20000000) {
        Map result = (Map) map.get("flash_result");
        List<Map> sentences = (List<Map>) result.get("sentences");
        if (sentences != null && sentences.size() > 0) {
          for (Map sentence : sentences) {
            allText += sentence.get("text");
          }
        }
      }
    }
    return allText;
  }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的微信小程序中图片上传阿里云OSS的例子: 1. 在微信小程序中,使用wx.chooseImage()方法选择需要上传的图片,并将其保存在本地变量tempFilePaths中。 ``` wx.chooseImage({ success: function(res) { var tempFilePaths = res.tempFilePaths; // 上传图片到阿里云OSS uploadImage(tempFilePaths[0]); } }) ``` 2. 编写上传图片的函数uploadImage(),其中需要设置header、formData、name、url等参数,并调用wx.uploadFile()方法上传图片。 ``` function uploadImage(filePath) { // 阿里云OSS的Bucket名称和上传文件夹名称 var bucketName = 'your-bucket-name'; var folderName = 'your-folder-name/'; // 生成上传文件的唯一key var timestamp = new Date().getTime(); var key = folderName + timestamp + '-' + Math.floor(Math.random() * 10000) + '.jpg'; // 生成OSS上传接口地址 var policyBase64 = 'your-policy-base64'; var signature = 'your-signature'; var aliyunUrl = 'https://' + bucketName + '.oss-cn-hangzhou.aliyuncs.com'; // 设置header和formData var header = { 'content-type': 'multipart/form-data' }; var formData = { 'key': key, 'policy': policyBase64, 'OSSAccessKeyId': 'your-access-key-id', 'success_action_status': '200', 'signature': signature }; // 调用wx.uploadFile()方法上传图片 wx.uploadFile({ url: aliyunUrl, filePath: filePath, name: 'file', header: header, formData: formData, success: function(res) { // 上传成功,获取图片URL地址 var imageUrl = aliyunUrl + '/' + key; console.log('上传成功,图片URL地址为:' + imageUrl); }, fail: function(res) { // 上传失败 console.log('上传失败:' + res.errMsg); } }) } ``` 需要注意的是,以上代码仅为一个示例,实际使用时需要按照阿里云OSS的规定进行设置,并在后端服务中编写相应的签名验证逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值