前不久写了个工具型微信小程序(Find周边),里面用到了语音识别技术。现将实现细节整理以下:php
接口预览html
经过阅读了解科大讯飞接口文档、小程序接口开发文档以及对后端ThinkPhp框架的学习,我整理了以下开发步骤:java
音频录制接口node
wx.startRecord()和wx.stopRecord()接口也能够知足需求,但从1.6.0 版本开始再也不被微信团队维护。建议使用能力更强的 wx.getRecorderManager 接口。该接口获取到的音频格式为silk。
silk是webm格式经过base64编码后的结果,咱们解码后须要将webm转换成pcm、wavpython
相对wx.startRecord()接口,该接口提供的能力更为强大(详情),能够暂停录音也能够继续录音,根据本身需求设置编码码率,录音通道数,采样率。最让人开心的是能够指定音频格式,有效值 aac/mp3。很差的是wx.getRecorderManager()在1.6.0才开始被支持。固然若是你要兼容低端微信用户须要使用wx.startRecord()作兼容处理。linux
// wxjs:
const recorderManager = wx.getRecorderManager()
recorderManager.onStart(() => {
//开始录制的回调方法
})
//录音中止函数
recorderManager.onStop((res) => {
const { tempFilePath } = res;
//上传录制的音频
wx.uploadFile({
url: app.d.hostUrl + '/Api/Index/wxupload', //仅为示例,非真实的接口地址
filePath: tempFilePath,
name: 'viceo',
success: function (res) {
console.log(res);
}
})
})
Page({
//按下按钮--录音
startHandel: function () {
console.log("开始")
recorderManager.start({
duration: 10000
})
},
//松开按钮
endHandle: function () {
console.log("结束")
//触发录音中止
recorderManager.stop()
}
})
//wxml:
<view bindtouchstart='startHandel' bindtouchend='endHandle' class="tapview">
<text>{{text}}</text>
</view>
复制代码
音频转换web
我这边后端使用php的开源框架thinkphp,固然node、java、python等后端语言均可以,你根据本身的喜爱和能力来。想作好音频转码咱们就要借助音视频转码工具ffmpeg、avconv,它们都依赖于gcc。安装过程你们能够自行百度,或者关注我后面的文章。thinkphp
<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {
//音频上传编解码
public function wxupload(){
$upload_res=$_FILES['viceo'];
$tempfile = file_get_contents($upload_res['tmp_name']);
$wavname = substr($upload_res['name'],0,strripos($upload_res['name'],".")).".wav";
$arr = explode(",", $tempfile);
$path = 'Aduio/'.$upload_res['name'];
if ($arr && !empty(strstr($tempfile,'base64'))){
//微信模拟器录制的音频文件能够直接存储返回
file_put_contents($path, base64_decode($arr[1]));
$data['path'] = $path;
apiResponse("success","转码成功!",$data);
}else{
//手机录音文件
$path = 'Aduio/'.$upload_res['name'];
$newpath = 'Aduio/'.$wavname;
file_put_contents($path, $tempfile);
chmod($path, 0777);
$exec1 = "avconv -i /home/wwwroot/mapxcx.kanziqiang.top/$path -vn -f wav /home/wwwroot/mapxcx.kanziqiang.top/$newpath";
exec($exec1,$info,$status);
chmod($newpath, 0777);
if ( !empty($tempfile) && $status == 0 ) {
$data['path'] = $newpath;
apiResponse("success","转码成功!",$data);
}
}
apiResponse("error","发生未知错误!");
}
//json数据返回方法封装
function apiResponse($flag = 'error', $message = '',$data = array()){
$result = array('flag'=>$flag,'message'=>$message,'data'=>$data);
print json_encode($result);exit;
}
}
复制代码
调用识别接口json
当咱们把文件准备好以后,接下来咱们就能够将base64编码以后的音频文件经过api接口请求传输过去。期间咱们要注意严格按照文档中所说的规范传输,不然将形成不可知的结果。小程序
<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {
public function _initialize(){
}
//封装数据请求方法
public function httpsRequest($url,$data = null,$xparam){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HEADER, 0);
$Appid = "";//开放平台的appid
$Appkey = "";//开放平台的Appkey
$curtime = time();
$CheckSum = md5($Appkey.$curtime.$xparam.$data);
$headers = array(
'X-Appid:'.$Appid,
'X-CurTime:'.$curtime,
'X-CheckSum:'.$CheckSum,
'X-Param:'.$xparam,
'Content-Type:'.'application/x-www-form-urlencoded; charset=utf-8'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//请求接口数据处理
public function getVoice($path){
$d = base64_encode($path);
$url = "https://api.xfyun.cn/v1/aiui/v1/voice_semantic";
$xparam = base64_encode( json_encode(array('scene' => 'main','userid'=>'user_0001',"auf"=>"16k","aue"=>"raw","spx_fsize"=>"60" )));
$data = "data=".$d;
$res = $this->httpsRequest($url,$data,$xparam);
if(!empty($res) && $res['code'] == 00000){
apiResponse("success","识别成功!",$res);
}else{
apiResponse("error","识别失败!");
}
}
//数据返回封装
function apiResponse($flag = 'error', $message = '',$data = array()){
$result = array('flag'=>$flag,'message'=>$message,'data'=>$data);
print json_encode($result);exit;
}
}
复制代码
到这里基本就完成了。以上代码是通过整理以后的,并不必定可以知足各位的实际开发需求。若是发现不当之处欢迎微信交流(xiaoqiang0672)。