微信小程序语音识别接百度接口实例php
1.首先需要下载两个东西ffmpeg和silk_v3_decoder,我使用的是直接下载包含exe的文件,没有安装,这两个再官方都可以下载的到
2.百度语音申请账号,创建应用
3.微信语音上传
wxml文件
<button type="primary" bindtouchstart="startRecode" bindtouchend="endRecode" class="cxbtn">按住录音(可选)</button>
js文件
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
},
onLoad: function () {
},
startRecode:function(){
var s = this;
console.log("start");
wx.startRecord({
success: function (res) {
console.log(res);
var tempFilePath = res.tempFilePath;
s.setData({ recodePath: tempFilePath, isRecode:true});
},
fail: function (res) {
console.log("fail");
console.log(res);
//录音失败
}
});
},
endRecode:function(){//结束录音
var s = this;
console.log("end");
wx.stopRecord();
s.setData({ isRecode: false });
wx.showToast();
setTimeout(function () {
var urls = "";
console.log(s.data.recodePath);
wx.uploadFile({
url: urls,
filePath: s.data.recodePath,
name: 'file',
header: {
'content-type': 'multipart/form-data'
},
success: function (res) {
console.log(res);
},
fail: function (res) {
console.log(res);
wx.showModal({
title: '提示',
content: "网络请求失败,请确保网络是否正常",
showCancel: false,
success: function (res) {
}
});
wx.hideToast();
}
});
},1000)
}
})
//麦克风帧动画
function speaking() {
var _this = this;
//话筒帧动画
var i = 1;
this.timer = setInterval(function () {
i++;
i = i % 5;
_this.setData({
j: i
})
}, 200);
}
css文件
/**index.wxss**/
.speak-style{
position: relative;
height: 240rpx;
width: 240rpx;
border-radius: 20rpx;
margin: 50% auto;
background: #26A5FF;
}
.item-style{
margin-top: 30rpx;
margin-bottom: 30rpx;
}
.text-style{
text-align: center;
}
.record-style{
position: fixed;
bottom: 0;
left: 0;
height: 120rpx;
width: 100%;
}
.btn-style{
margin-left: 30rpx;
margin-right: 30rpx;
}
.sound-style{
position: absolute;
width: 74rpx;
height:150rpx;
margin-top: 45rpx;
margin-left: 83rpx;
}
.board {
overflow: hidden;
border-bottom: 2rpx solid #26A5FF;
}
/*列布局*/
.cell{
display: flex;
margin: 20rpx;
}
.cell-hd{
margin-left: 10rpx;
color: #885A38;
}
.cell .cell-bd{
flex:1;
position: relative;
}
/**只显示一行*/
.date{
font-size: 30rpx;
text-overflow: ellipsis;
white-space:nowrap;
overflow:hidden;
}
服务端,这里因为服务器的ssl证书好像过期了,我curl直接跳过检查这一项
public function silk_upload(){
$after_name = end(explode(".",$_FILES['file']['name']));//获取上传信息的名称
if($after_name == 'silk'){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->rootPath = './Public/weixin/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$uplode->subName = array('date','Ymd');//文件命名方式已时期时间戳命名
$info = $upload->upload();
if(!$info){
$data = array(
"msg" => "语音上传失败",
"info" => ""
);
$this->ajaxReturn(fail($data));
}else{
$savepath = $info['file']['savepath'];
$savename = $info['file']['savename'];
$name=explode(".",$savename);//转变数组--单个元素
$name = $name[0];
$file_path='D:/phpStudy/WWW/power/Public/';
$file = $file_path.'weixin/'.$savepath.$name.'.silk';
$type = $file_path.'weixin/'.$savepath.$name.'.pcm';
$res = $file_path.'weixin/'.$savepath.$name.'.wav';
$command = $file_path."Audio/silk_v3_decoder.exe $file $type";
exec($command);
$command = $file_path."Audio/ffmpeg.exe -y -f s16le -ar 24000 -ac 1 -i $type -f wav -ar 16000 -b:a 16 -ac 1 $res";
exec($command);
//调用百度语音转换API接口
define('AUDIO_FILE',$res);
$cuid = "";
$apiKey = "";
$secretKey = "";
$auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$apiKey."&client_secret=".$secretKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if(curl_errno($ch))
{
print curl_error($ch);
}
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];
$url = "http://vop.baidu.com/server_api?cuid=".$cuid."&token=".$token;
$url = $url."&lan=zh";
$audio = file_get_contents(AUDIO_FILE);
$content_len = "Content-Length: ".strlen($audio);
$header = array ($content_len,'Content-Type: audio/pcm; rate=16000',);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POSTFIELDS, $audio);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$response = json_decode($response, true);
if($response['err_no'] == 0){
preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $response['result'][0], $matches);//去掉字符串中非汉字
return $matches[0][0];//返回转换出来的汉字
}else{
$data = array(
"msg" => "语音转换失败",
"info" => ""
);
$this->ajaxReturn(fail($data));
}
}
}else{
$data = array(
"msg" => "文件类型错误",
"info" => ""
);
$this->ajaxReturn(fail($data));
}
}
我这里是将微信语音上传,然后转pcm,再转wav,最后请求,如果有什么想问的,可以加我QQ1930666976
仅供参考使用
阅读更多- 微信小程序request请求后台接口php简单实例
- [置顶] 微信小程序支付demo, php开发小程序支付接口实例
- PHP调用百度AI接口人脸识别-语音技术-文本识别-图像识别
- 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)
- 微信小程序request请求后台接口php的实例详解
- 微信公众平台开发模式 微信 语音识别 公众平台 消息接口 语音识别
- 微信小程序发短信,php开发小程序短信接口,获取手机验证码
- PHP版微信小店接口开发实例
- 基于Python与百度的AI接口的语音转文字的简单小程序。
- PHP版微信小店接口开发实例
- 原创:微信小程序调用PHP后台接口,解析纯html文本
- php微信服务端的接口程序
- 百度文字转语音免费接口使用实例
- 微信小程序语音识别开发过程记录 微信小程序silk转mp3 silk转wav 以及ffmpeg使用 微信小程序语音红包
- 微信小程序实例:开发showToast消息提示接口
- 微信小程序实现图片上传功能实例(前端+PHP后端)
- 百度文字转语音免费接口使用实例
- 微信小程序 支付功能实现PHP实例详解
- php 微信 统一下单 接口实例
- 微信小程序 wx.request(接口调用方式)详解及实例