We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
微信的录音文件上传到微信服务器上,只能保存三天。 因此需要做一个转存到自己服务器,或者七牛云的操作。
... /** * 开始录音[省略了一部分代码] */ startRecord: function() { var that = this; if (!that._startRecordFlag) { typeof wx !== "undefined" && wx.startRecord({ success: function(res) { Logger.log("res", res) if (res.errMsg == 'startRecord:ok') { Logger.log("正在开始录音....") that._startTime = new Date().getTime(); } } }); } }, /** * 结束录音,并上传 */ stopRecord: function() { that._startRecordFlag = false; typeof wx !== "undefined" && wx.stopRecord({ success: function(res) { //上传录音 wx.uploadVoice({ localId: res.localId, isShowProgressTips: 1, success: function(resUpload) { //下载录音文件到服务器,转存起来 Model.downloadRecordAudio(resUpload.serverId, function(result) { console.log(resUpload.serverId, result.path) that.attachment = result.path; // that.attachment = resUpload.serverId; that.stopRecordCallback && that.stopRecordCallback(); }) } }); } }); }, ...
<?php //处理方法, upload(); //media_id为微信jssdk接口上传后返回的媒体id function upload(){ $media_id = $_POST["media_id"]; $access_token = getAccessToken(); $path = "./weixinrecord/"; //保存路径,相对当前文件的路径 $outPath = "./php/weixinrecord/"; //输出路径,给show.php 文件用,上一级 if(!is_dir($path)){ mkdir($path); } //微信上传下载媒体文件 $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}"; $fileName = "wxupload_".time().rand(1111,9999); $mp3Path = $path."/".$fileName.".mp3"; $amrPath = $path."/".$fileName.".amr"; //下载微信录音文件 downAndSaveFile($url,$amrPath); //转换成mp3文件,TODO:一直没有转换成mp3,麻烦定位下 if(file_exists($mp3Path) != true){ // $command = "ffmpeg -i $amrPath $mp3Path & rm $mp3Path"; $command = "ffmpeg -i $amrPath $mp3Path"; system($command,$error); } $data["path"] = $outPath.$filename; $data["msg"] = "download record audio success!"; // $data["url"] = $url; echo json_encode($data); } //获取Token function getAccessToken() { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("./access_token.json")); if ($data->expire_time < time()) { $appid = "youappid"; //自己的appid $appsecret = "youappsecret"; //自己的appsecret $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; $res = json_decode(httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("./access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } //HTTP get 请求 function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } //根据URL地址,下载文件 function downAndSaveFile($url,$savePath){ ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp = fopen($savePath, 'a'); fwrite($fp, $img); fclose($fp); } ?>
目前没有使用七牛云,因此该部分代码,参考七牛云官网
The text was updated successfully, but these errors were encountered:
最好对下载微信录音文件的时候,做一个判断,如果下载的不是 amr格式的文件,或者者下载的时候报错了,报 token失效啥的, 单独做一些处理。 昨晚因为token失效,搞了一个多小时。
Sorry, something went wrong.
No branches or pull requests
转存到自己服务器
步骤1代码
步骤2代码
步骤3代码【略】
The text was updated successfully, but these errors were encountered: