<script language="JavaScript">
jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']);} || { jsApiList:[] };
jssdkconfig.jsApiList = ['startRecord','stopRecord','uploadVoice','playVoice','downloadVoice'];
wx.config(jssdkconfig);
//按下开始录音
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
$('#talk_btn').on('touchstart', function(event){
//document.getElementById("aa").val("sdfsdfdsfsd");
event.preventDefault();
START = new Date().getTime();
$("#bfyy").css("display","block");
recordTimer = setTimeout(function(){
wx.startRecord({
success: function(){
//alert(123);
localStorage.rainAllowRecord = 'true';
},
cancel: function () {
alert('用户拒绝授权录音');
}
});
},300);
});
//松手结束录音
var yuyin = "";
$('#talk_btn').on('touchend', function(event){
event.preventDefault();
END = new Date().getTime();
$("#bfyy").css("display","none");
if((END - START) < 300){
END = 0;
START = 0;
//小于300ms,不录音
clearTimeout(recordTimer);
}else{
wx.stopRecord({
success: function (res) {
//alert(JSON.stringify(res.localId));
//alert(res);
yuyin = res.localId;
//alert(yuyin);
//alert("录音成功");
$("#yu-box").css("display","block");
$("#succse").click(function () {
uploadVoice(yuyin);
$("#yu-box").css("display","none");
});
//
},
fail: function (res) {
//alert("dsf");
alert(JSON.stringify(res));
}
});
}
});
$('#close').click(function () {
$("#yu-box").css("display","none")
});
var bool = true;
$("#img").click(function () {
if(bool){
//语音播放
// wx.downloadVoice({
// // serverId: record.serverId,
// serverId:yuyin,
// isShowProgressTips: 1,
// success: function (res) {
// record.localId = res.localId;
// alert(res.localId);
wx.playVoice({
localId: yuyin
});
// }
//
// });
$("#fir").css('display','none');
$("#sec").css('display','block');
bool=!bool;
}else {
$("#fir").css('display','block');
$("#sec").css('display','none');
bool=!bool;
}
})
//上传录音
function uploadVoice(){
//调用微信的上传录音接口把本地录音先上传到微信的服务器
//不过,微信只保留3天,而我们需要长期保存,我们需要把资源从微信服务器下载到自己的服务器
wx.uploadVoice({
localId: yuyin, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
document.getElementById('voice').value = res.serverId;
alert("录音成功");
// id = res.serverId;
// downloadVoice(id);
//alert(JSON.stringify(res));
//把录音在微信服务器上的id(res.serverId)发送到自己的服务器供下载。
// var url = "{php echo $this->createMobileUrl('uploadnew');}&serverId=" + res.serverId ;
// $.ajax({
// url:url,
// type: 'post',
// dataType: "json",
// success: function (data) {
// alert(data);
// console.log(data);//
// },
// error: function (xhr, errorType, error) {
// console.log(error);
// }
// });
}
});
}
//function downloadVoice(id){
// wx.downloadVoice({
// serverId: id, // 需要下载的音频的服务器端ID,由uploadVoice接口获得
// isShowProgressTips: 1,// 默认为1,显示进度提示
// success: function(res){
// //var localId = res.localId; // 返回音频的本地ID
// alert(JSON.stringify(res));
// }
// });
//}
//注册微信播放录音结束事件【一定要放在wx.ready函数内】
wx.onVoicePlayEnd({
success: function (res) {
stopWave();
}
});
</script>
jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']);} || { jsApiList:[] };
jssdkconfig.jsApiList = ['startRecord','stopRecord','uploadVoice','playVoice','downloadVoice'];
wx.config(jssdkconfig);
//按下开始录音
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
$('#talk_btn').on('touchstart', function(event){
//document.getElementById("aa").val("sdfsdfdsfsd");
event.preventDefault();
START = new Date().getTime();
$("#bfyy").css("display","block");
recordTimer = setTimeout(function(){
wx.startRecord({
success: function(){
//alert(123);
localStorage.rainAllowRecord = 'true';
},
cancel: function () {
alert('用户拒绝授权录音');
}
});
},300);
});
//松手结束录音
var yuyin = "";
$('#talk_btn').on('touchend', function(event){
event.preventDefault();
END = new Date().getTime();
$("#bfyy").css("display","none");
if((END - START) < 300){
END = 0;
START = 0;
//小于300ms,不录音
clearTimeout(recordTimer);
}else{
wx.stopRecord({
success: function (res) {
//alert(JSON.stringify(res.localId));
//alert(res);
yuyin = res.localId;
//alert(yuyin);
//alert("录音成功");
$("#yu-box").css("display","block");
$("#succse").click(function () {
uploadVoice(yuyin);
$("#yu-box").css("display","none");
});
//
},
fail: function (res) {
//alert("dsf");
alert(JSON.stringify(res));
}
});
}
});
$('#close').click(function () {
$("#yu-box").css("display","none")
});
var bool = true;
$("#img").click(function () {
if(bool){
//语音播放
// wx.downloadVoice({
// // serverId: record.serverId,
// serverId:yuyin,
// isShowProgressTips: 1,
// success: function (res) {
// record.localId = res.localId;
// alert(res.localId);
wx.playVoice({
localId: yuyin
});
// }
//
// });
$("#fir").css('display','none');
$("#sec").css('display','block');
bool=!bool;
}else {
$("#fir").css('display','block');
$("#sec").css('display','none');
bool=!bool;
}
})
//上传录音
function uploadVoice(){
//调用微信的上传录音接口把本地录音先上传到微信的服务器
//不过,微信只保留3天,而我们需要长期保存,我们需要把资源从微信服务器下载到自己的服务器
wx.uploadVoice({
localId: yuyin, // 需要上传的音频的本地ID,由stopRecord接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (res) {
document.getElementById('voice').value = res.serverId;
alert("录音成功");
// id = res.serverId;
// downloadVoice(id);
//alert(JSON.stringify(res));
//把录音在微信服务器上的id(res.serverId)发送到自己的服务器供下载。
// var url = "{php echo $this->createMobileUrl('uploadnew');}&serverId=" + res.serverId ;
// $.ajax({
// url:url,
// type: 'post',
// dataType: "json",
// success: function (data) {
// alert(data);
// console.log(data);//
// },
// error: function (xhr, errorType, error) {
// console.log(error);
// }
// });
}
});
}
//function downloadVoice(id){
// wx.downloadVoice({
// serverId: id, // 需要下载的音频的服务器端ID,由uploadVoice接口获得
// isShowProgressTips: 1,// 默认为1,显示进度提示
// success: function(res){
// //var localId = res.localId; // 返回音频的本地ID
// alert(JSON.stringify(res));
// }
// });
//}
//注册微信播放录音结束事件【一定要放在wx.ready函数内】
wx.onVoicePlayEnd({
success: function (res) {
stopWave();
}
});
</script>