国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序錄音直傳阿里云OSS并語音識別

這篇具有很好參考價值的文章主要介紹了微信小程序錄音直傳阿里云OSS并語音識別。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

如題:做一個錄音文字識別功能,知識點有三個,分別是微信小程序的錄音功能、錄音文件直傳阿里云OSS、使用阿里云的錄音文件識別接口返回識別后的文字


一、微信小程序錄音

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

wxml:

<view style="padding-top:40%"></view>
<button bindtap="start">開始錄音</button>
<!-- <button bindtap="pause">暫停錄音</button> -->
<!-- <button bindtap="resume">繼續(xù)錄音</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:需要注意的是,點擊開始錄音時要判斷當(dāng)前是否獲取到了錄音權(quán)限,如果沒有錄音權(quán)限進行提示,引導(dǎo)用戶重新授權(quán)

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, //錄音通道數(shù)
      encodeBitRate: 96000, //編碼碼率
      format: 'mp3', //音頻格式,有效值 aac/mp3
      frameSize: 50, //指定幀大小,單位 KB
    }
    //開始錄音
    wx.authorize({
      scope: 'scope.record',
      success() {
        console.log("錄音授權(quán)成功");
        //第一次成功授權(quán)后 狀態(tài)切換為2
        that.setData({
          status: 2,
        })
        recorderManager.start(options);
        recorderManager.onStart(() => {
          //可以彈出模態(tài)框等友好提示
          console.log('錄音開始')
        });
        //錯誤回調(diào)
        recorderManager.onError((res) => {
          //進行錯誤提示
          console.log(res);
        })
      },
      fail() {
        console.log("第一次錄音授權(quán)失敗");
        wx.showModal({
          title: '提示',
          content: '您未授權(quán)錄音,功能將無法使用',
          showCancel: true,
          confirmText: "授權(quán)",
          confirmColor: "#52a2d8",
          success: function (res) {
            if (res.confirm) {
              //確認則打開設(shè)置頁面(重點)
              wx.openSetting({
                success: (res) => {
                  console.log(res.authSetting);
                  if (!res.authSetting['scope.record']) {
                    //未設(shè)置錄音授權(quán)
                    console.log("未設(shè)置錄音授權(quán)");
                    wx.showModal({
                      title: '提示',
                      content: '您未授權(quán)錄音,功能將無法使用',
                      showCancel: false
                    })
                  } else {
                    //第二次才成功授權(quán)
                    console.log("設(shè)置錄音授權(quán)成功");
                    that.setData({
                      status: 2
                    })
                  }
                },
                fail: function () {
                  console.log("授權(quán)設(shè)置錄音失敗");
                }
              })
            } else if (res.cancel) {
              console.log("cancel");
            }
          },
          fail: function () {
            console.log("openfail");
          }
        })
      }
    })
  },
  //暫停錄音-需求不需要暫停
  pause: function () {
    recorderManager.pause();
    recorderManager.onPause((res) => {
      console.log('暫停錄音')
    })
  },
  //繼續(xù)錄音-不需要暫停也就不需要繼續(xù)
  resume: function () {
    recorderManager.resume();
    recorderManager.onStart(() => {
      console.log('重新開始錄音')
    });
    //錯誤回調(diào)
    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合法域名為對應(yīng)Bucket的外網(wǎng)訪問域名

Javascript直傳OSS需要提前獲取policy,服務(wù)端如下:

  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);
      }
    });
  }

三、阿里云錄音文字識別極速版

進行錄音識別之前需要先創(chuàng)建智能語音交互服務(wù)對應(yīng)的項目,獲得AppKey

官方文檔:創(chuàng)建智能語音交互服務(wù)項目

官方文檔:錄音文件識別極速版

由于將錄音文件直傳OSS了,所以不需要將錄音文件上傳到語音識別里,所以采取第二種方式,使用音頻文件鏈接進行語音識別:文章來源地址http://www.zghlxwxcb.cn/news/detail-496624.html

  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) {
    /**
     * 設(shè)置HTTPS REST POST請求
     * 1.使用http協(xié)議
     * 2.語音識別服務(wù)域名:nls-gateway.cn-shanghai.aliyuncs.com
     * 3.語音識別接口請求路徑:/stream/v1/FlashRecognizer
     * 4.設(shè)置必須請求參數(shù):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;

    /**
     * 設(shè)置HTTPS頭部字段
     * 發(fā)送HTTPS POST請求,返回服務(wù)端的響應(yīng)。
     *
     * 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;
  }

到了這里,關(guān)于微信小程序錄音直傳阿里云OSS并語音識別的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包