微信小程序之语音识别+PHP后台处理

微信前台录入语音PHP后台调用百度AI进行语音识别

先去百度AI下载 PHP sdk
目录如下
在这里插入图片描述
**

由于微信小程序暂且支持真机调试才会出现效果
所以真机调试的设备必须与js中发起请求的url在同一网络下

**

微信前台代码
.html

<button type='primary' bindtouchstart='startrecorderHandel' bindtouchend="sendrecorderHandel">点击录音</button>
<view>
  你说的话是:
  <view>
    {{msg}}
  </view>
</view>

.js

// 录音对象
const recorderManager = wx.getRecorderManager();

function sendRecord(src) {
  var obj = {
    url: "http://49.122.47.146/php-yuyin/yuyin.php",
    filePath: src,
    name: "recordFile",
    header: {
      'Content-Type': 'application/json'
    },
    success: function (result) {
      var data = JSON.parse(result.data);
      // msg 为最终语音识别的字符串
      var msg = data.result;
      // 获取当前页面对象
      var page = getCurrentPages()[0];
      page.setData({ msg: msg });
    },
    fail: function (err) {
      console.log(err);
    }
  };
  wx.uploadFile(obj)
}

// 结束录音的时候触发 
recorderManager.onStop((res) => {
  // 获取文件路径-提交到后台-后台发送到百度
  sendRecord(res.tempFilePath);
})

recorderManager.onError ((res) => {
  console.log("error", res);
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg: ""
  },
  // 按下按钮的时候触发
  startrecorderHandel() {
    // 开始录音
    recorderManager.start({
    });
  },
  // 松开按钮的时候触发-发送录音
  sendrecorderHandel() {
    // 结束录音
    recorderManager.stop();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.authorize({
      scope: 'record'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

PHP后台代码

<?php
/**
 * 调用百度AI识别语音
 */
require_once "aip-speech-php-sdk-1.6.0/AipSpeech.php";

$name = "./upload/".$_FILES['recordFile']['name'];
if(move_uploaded_file($_FILES['recordFile']['tmp_name'],$name)){

    $APP_ID = '你的$APP_ID';//你的$APP_ID
    $API_KEY = '你的$API_KEY';//你的$API_KEY
    $SECRET_KEY = '你的$SECRET_KEY';//你的$SECRET_KEY

    $client = new AipSpeech($APP_ID, $API_KEY, $SECRET_KEY);
    $res = $client->asr(file_get_contents($name), 'm4a', 16000, array(
        'dev_pid' => 1536,
    ));
    echo json_encode($res);
    die();
}else{
    echo "上传失败";
}

die();
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长夜将尽 来日可期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值