微信识别语音
wechat.html
<!DOCTYPE html>
<html lang="en">
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
var _title=document.title;
//分享 debug----true 会弹出对话框 false---不会
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: \'{$signPackage["appId"]}\',
timestamp: \'{$signPackage["timestamp"]}\',
nonceStr: \'{$signPackage["nonceStr"]}\',
signature: \'{$signPackage["signature"]}\',
jsApiList: [
\'checkJsApi\',
\'onMenuShareTimeline\',
\'onMenuShareAppMessage\', //分享给朋友
\'onMenuShareQQ\',
\'onMenuShareWeibo\',
\'onMenuShareQZone\',
"startRecord",
"stopRecord",
"translateVoice",
"uploadVoice",
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
//分享给朋友
var title=document.title;
var reg=/index\.php\/$/;
var _url="";
if(reg.test(location.href)){
_url=location.href+"index/shareopenid/{$openid}";
}else{
_url=location.href+"/shareopenid/{$openid}";
}
wx.onMenuShareAppMessage({
title: \'标-----题-----\'+_title+_url, // 分享标题
desc:"{$openid}", // 分享描述
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\', // 分享图标
type: \'\', // 分享类型,music、video或link,不填默认为link
dataUrl: \'\', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: \'测试---朋友圈----\'+_title, // 分享标题
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\', // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
}
});
//分享到QQ
wx.onMenuShareQQ({
title: \'qq----------分享\'+_title, // 分享标题
desc: \'qq----描述\', // 分享描述
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\',
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
})
$(\'#luyin\').on(\'touchstart\', function(event){
event.preventDefault();
START = new Date().getTime();
recordTimer = setTimeout(function(){
wx.startRecord({
success: function(){
alert(\'开始录音\');
localStorage.rainAllowRecord = \'true\';
},
cancel: function () {
alert(\'用户拒绝授权录音\');
}
});
},300);
});
//松手结束录音
$(\'#luyin\').on(\'touchend\', function(event){
event.preventDefault();
END = new Date().getTime();
if((END - START) < 300){
alert("结束1")
END = 0;
START = 0;
//小于300ms,不录音
clearTimeout(recordTimer);
}else{
alert("结束2")
wx.stopRecord({
success: function (res) {
alert("success")
alert("----生成了Id="+res.localId);
var _localId=res.localId;
shibie(_localId);
// uploadVoice1(_localId);
// uploadVoice();
},
fail: function (res) {
alert(res);
}
});
}
});
function uploadVoice1(_localId){
//上传录音
//调用微信的上传录音接口把本地录音先上传到微信的服务器
//不过,微信只保留3天,而我们需要长期保存,我们需要把资源从微信服务器下载到自己的服务器
wx.uploadVoice({
localId: _localId, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
alert("开始上传微信服务器"+JSON.stringify(res))
//把录音在微信服务器上的id(res.serverId)发送到自己的服务器供下载。
$.ajax({
url: \'{$Think.config.url}/savevoice\',
type: \'post\',
data: {
res:JSON.stringify(res)
},
dataType: "json",
success: function (data) {
alert(\'文件已经保存到自己的服务器\');//
},
error: function (xhr, errorType, error) {
console.log(error);
}
});
}
});
}
function uploadvoice(_localId){
wx.uploadVoice({
localId: _localId, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
var serverId = res.serverId; // 返回音频的服务器端ID
}
});
}
function downloadVoice(){
wx.downloadVoice({
serverId: \'\', // 需要下载的音频的服务器端ID,由uploadVoice接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
var localId = res.localId; // 返回音频的本地ID
}
});
}
//识别语音
function shibie(_localId){
wx.translateVoice({
localId: _localId, // 需要识别的音频的本地Id,由录音相关接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
alert(res.translateResult); // 语音识别的结果
}
});
}
</script>
</html>
yuyin.html
<!DOCTYPE html>
<html lang="en">
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
var _title=document.title;
//分享 debug----true 会弹出对话框 false---不会
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: \'{$signPackage["appId"]}\',
timestamp: \'{$signPackage["timestamp"]}\',
nonceStr: \'{$signPackage["nonceStr"]}\',
signature: \'{$signPackage["signature"]}\',
jsApiList: [
\'checkJsApi\',
\'onMenuShareTimeline\',
\'onMenuShareAppMessage\', //分享给朋友
\'onMenuShareQQ\',
\'onMenuShareWeibo\',
\'onMenuShareQZone\',
"startRecord",
"stopRecord",
"translateVoice",
"uploadVoice",
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
//分享给朋友
var title=document.title;
var reg=/index\.php\/$/;
var _url="";
if(reg.test(location.href)){
_url=location.href+"index/shareopenid/{$openid}";
}else{
_url=location.href+"/shareopenid/{$openid}";
}
wx.onMenuShareAppMessage({
title: \'标-----题-----\'+_title+_url, // 分享标题
desc:"{$openid}", // 分享描述
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\', // 分享图标
type: \'\', // 分享类型,music、video或link,不填默认为link
dataUrl: \'\', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: \'测试---朋友圈----\'+_title, // 分享标题
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\', // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
}
});
//分享到QQ
wx.onMenuShareQQ({
title: \'qq----------分享\'+_title, // 分享标题
desc: \'qq----描述\', // 分享描述
link: _url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: \'http://box.bdimg.com/static/fisp_static/common/img/searchbox/logo_news_276_88_1f9876a.png\',
success: function () {
// 用户确认分享后执行的回调函数
alert("--分享成功");
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
})
$(\'#luyin\').on(\'touchstart\', function(event){
event.preventDefault();
START = new Date().getTime();
recordTimer = setTimeout(function(){
wx.startRecord({
success: function(){
alert(\'开始录音\');
localStorage.rainAllowRecord = \'true\';
},
cancel: function () {
alert(\'用户拒绝授权录音\');
}
});
},300);
});
//松手结束录音
$(\'#luyin\').on(\'touchend\', function(event){
event.preventDefault();
END = new Date().getTime();
if((END - START) < 300){
alert("结束1")
END = 0;
START = 0;
//小于300ms,不录音
clearTimeout(recordTimer);
}else{
alert("结束2")
wx.stopRecord({
success: function (res) {
alert("success")
alert("----生成了Id="+res.localId);
var _localId=res.localId;
shibie(_localId);
// uploadVoice1(_localId);
// uploadVoice();
},
fail: function (res) {
alert(res);
}
});
}
});
function uploadVoice1(_localId){
//上传录音
//调用微信的上传录音接口把本地录音先上传到微信的服务器
//不过,微信只保留3天,而我们需要长期保存,我们需要把资源从微信服务器下载到自己的服务器
wx.uploadVoice({
localId: _localId, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
alert("开始上传微信服务器"+JSON.stringify(res))
//把录音在微信服务器上的id(res.serverId)发送到自己的服务器供下载。
$.ajax({
url: \'{$Think.config.url}/savevoice\',
type: \'post\',
data: {
res:JSON.stringify(res)
},
dataType: "json",
success: function (data) {
alert(\'文件已经保存到自己的服务器\');//
},
error: function (xhr, errorType, error) {
console.log(error);
}
});
}
});
}
function uploadvoice(_localId){
wx.uploadVoice({
localId: _localId, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
var serverId = res.serverId; // 返回音频的服务器端ID
}
});
}
function downloadVoice(){
wx.downloadVoice({
serverId: \'\', // 需要下载的音频的服务器端ID,由uploadVoice接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
var localId = res.localId; // 返回音频的本地ID
}
});
}
//识别语音
function shibie(_localId){
wx.translateVoice({
localId: _localId, // 需要识别的音频的本地Id,由录音相关接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
alert(res.translateResult); // 语音识别的结果
}
});
}
</script>
</html>