E企盈营销工具技术服务商 热线:4006-838-530

微信小程序口令红包

E企盈直播平台营销卖货系统

思路:使用小程序录音API服务,建立录音文件。后台调用百度在线语音识别API接口,转换语音为文字。后台调用百度短文本相似度API接口,比对原始文本与语音文本的相似度。根据第三步的相似度决定是否抽红包。实现:1、使用小程序录音API服务,建立录音文件。小程序文档:全局唯一的录音管理器 2、小程序代码<view>      “黑灰化肥会挥发发灰黑讳为花飞;灰黑化肥会挥发发黑灰为讳飞花”</view><button class=’btn’ style=’background:{{bg}}’ bindtouchstart=”handleTouchStart” bindtouchend=”handleTouchEnd”>      <image src=’https://www.zlye.com/10/voice.png’></image>      <text>{{touchStartText}}</text></button>const rm = wx.getRecorderManager();Page({  /**   * 页面的初始数据   */  data: {    bg:”linear-gradient(180deg,rgba(255,246,174,1) 0%,rgba(251,230,132,1) 100%)”,    photo: true,    photoGif:false,    touchStartText:”按住说出以上口令领红包”  },  /**   * 生命周期函数–监听页面加载   */  onLoad: function (options) {    let that = this    rm.onStop(res => {      //请求后台比对      that.verifyVoice(res)    })  },  handleTouchStart(){    console.log(“录音开始”)    const options = {      duration: 60000,      sampleRate: 44100,      numberOfChannels: 1,      encodeBitRate: 192000,      format: ‘mp3’,      frameSize: 50    }    rm.start(options)    this.setData({      bg: “#ECD76C”,      photoGif: true,      photo: false,      touchStartText: “松开结束录音”    })  },  handleTouchEnd(){    console.log(“录音结束”);    rm.stop()    this.setData({      bg: “linear-gradient(180deg,rgba(255,246,174,1) 0%,rgba(251,230,132,1) 100%)”,      photoGif: false,      photo: true,      touchStartText: “按住说出以上口令领红包”    })  },  verifyVoice(res){    wx.uploadFile({      url: “后台地址”,      filePath: res.tempFilePath,      name: ‘file’,      formData: {        ‘user’: ‘test’      },      success: function (res) {        console.log(res); console.log(res.data);      },      fail: function () {        console.log(“语音识别失败”);      }    })  }})3、配置Centos7环境,安装音视频转码软件ffmpeg,具体安装步骤参考推荐文章。FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。【linux】ffmpeg 安装教程ffmpeg发行版本列表当前使用版本ffmpeg-4.1.tar.gz4、后台代码Controller层方法@PostMapping(“/verifyVoice”)public xxx verifyVoice(MultipartRequest request){    //调用Service层方法    xxx.verifyVoice(request);}Service层方法百度在线语音识别API接口,转换语音为文字。百度开放平台文档:在线语音识别@Overridepublic xxx verifyVoice(MultipartHttpServletRequest request) {logger.debug(“>>>>开始语音识别”);MultipartFile fileDetail = request.getFile(“file”);String fileName = System.currentTimeMillis() + “.mp3”;String path = “/opt/service/”;File file = new File(path);if (!file.exists() && !file.isDirectory()) {file.mkdirs();}RandomAccessFile randomFile = null;try {randomFile = new RandomAccessFile(path + fileName, “rw”);randomFile.seek(randomFile.length());randomFile.write(fileDetail.getBytes());} catch (IOException e) {e.printStackTrace();} finally {try {randomFile.close();} catch (IOException e) {e.printStackTrace();}}String res = null;try {String filePath = zm(path, fileName);File f = new File(filePath);for(int i=0;i<100000;i++){if(f.exists()){res = sb(filePath);//识别完毕删除文件final String fName = fileName.split(“\.”)[0];final String p = path;break;}}String fName = fileName.split(“\.”)[0];String p = path;File f1 = new File(p+fName+”.mp3″);File f2 = new File(p+fName+”.pcm”);f1.delete();f2.delete();System.out.println(“语音识别结果=”+res);} catch (Exception e) {System.out.println(“语音识别失败”);e.printStackTrace();}if(StringUtils.isBlank(res)){return null;}res = res.trim();    return null;}public String zm(String path, String fileName) throws IOException, InterruptedException {String zmName = fileName.split(“\.”)[0] + “.pcm”;String ffmpegPath = “/monchickey/ffmpeg/bin/”;String cmd = ffmpegPath+”ffmpeg -y -i “+path+fileName+” -acodec pcm_s16le -f s16le -ac 1 -ar 16000 “+path+zmName;try {              String[] cmdA = { “/bin/sh”, “-c”, cmd };              Process process = Runtime.getRuntime().exec(cmdA);              LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));              StringBuffer sb = new StringBuffer();              String line;              while ((line = br.readLine()) != null) {                  System.out.println(line);                  sb.append(line).append(“”);              }              System.out.println(sb.toString());          } catch (Exception e) {              e.printStackTrace();          } logger.debug(“zm返回值”+path+zmName);return path+zmName;}public static String sb(String filePath) throws JSONException, InterruptedException{String string = “”;try {// 初始化一个AipSpeechAipSpeech client = new AipSpeech(“xxxx”, “yyyy”, “zzzz”);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);// 调用接口JSONObject res = client.asr(filePath, “pcm”, 16000, null);string = res.toString(2);}catch (Exception e) {}return string;}注意://项目部署路径String path = “/opt/service/”;//步骤3中安装的音视频转码软件ffmpeg的位置String ffmpegPath = “/monchickey/ffmpeg/bin/”;// 初始化一个AipSpeechAipSpeech client = new AipSpeech(“xxxx”, “yyyy”, “zzzz”);替换为自己的百度开放平台授权信息。 百度短文本相似度API接口百度开放平台文档:短文本相似度接口 具体语音转文字后的相似度逻辑改造上面代码:System.out.println(“语音识别结果=”+res);

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序口令红包
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏