微信的录音文件上传到微信服务器上,只能保存三天。 因此需要做一个转存到自己服务器,或者七牛云的操作。

转存到自己服务器

  1. 调用微信JSSDK API 录音, 录音结束,上传到微信服务器,获取录音文件的 media_id
  2. 根据 media_id 下载录音文件(amr)格式
  3. 转存到自己服务器(amr需要转码成mp3) 或者 七牛云(有转码功能)

步骤1代码

     ...
/**
* 开始录音[省略了一部分代码]
*/
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();
})
}
});
}
});
},
...

步骤2代码

<?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).".amr";
downAndSaveFile($url,$path."/".$filename); $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);
}
?>

步骤3代码【略】

目前没有使用七牛云,因此该部分代码,参考七牛云官网

最新文章

  1. [翻译]Primer on Cognitive Computing(认知计算入门)
  2. 第六节:Vue过滤器的用法和自定义过滤器
  3. Scala 深入浅出实战经典 第78讲:Type与Class实战详解
  4. 用cart(分类回归树)作为弱分类器实现adaboost
  5. Tautology
  6. HDU 3966 Aragorn&#39;s Story 动态树 树链剖分
  7. iOS APP之间到跳转,以及热门应用,手机自带到应用跳转
  8. @Transient 理解
  9. java无需解压zip压缩包直接读取包内的文件名(含中文)
  10. install MariaDB 10.2 on Ubuntu 18
  11. [机器学习] --- Getting Started With MachineLearning
  12. Tesseract_ocr 字符识别基础及训练字库、合并字库
  13. h5屏幕旋转的时间和样式的设置
  14. c++刷题(30/100)
  15. OpenGL tutorial资源在mac yosemite下的cmake生成工程问题
  16. [转] 关于QT的系统总结
  17. JIT编译器
  18. nGrinder工具进行接口性能测试
  19. I/O流+统计文件词频
  20. MySQL查询优化器工作原理解析

热门文章

  1. 通过 Ansible 创建 Jenkins Server
  2. Tomcat启动时项目重复加载的问题
  3. python实现定时任务
  4. spring-boot-2.0.3启动源码篇二 - run方法(一)之SpringApplicationRunListener
  5. FFmpeg编解码处理4-音频编码
  6. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
  7. 146. LRU缓存机制
  8. [JZOJ5970] Space
  9. ext js 4.0 grid表格根据列值的不同给行设置不同的背景颜色
  10. 解决默写浏览器中点击input输入框时,placeholder的值不消失的方法