最近实现录音功能,主要涉及到录音的上传和下载,以及转码问题。微信,QQ默认的的音频文件是amr格式的,而播放器却不识别amr格式的音频,必须尽行转码。amr文件分为两种,一种是通用的amr格式,这种文件可以用任意播放器打开播放。但是还有另外一种amr文件,这种文件的后缀虽然是amr,但其实其内部并不是真正的amr格式。有不对的地方有请各方大神批评指正,具体实现如下:
1.调用微信录音接口(开始录音,暂停录音,上传录音,)
具体接口请你参考 http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
(1)record.jsp $(function(){ //调用微信开始录音接口 wx.ready(function(){
//开始录音 alert("开始录音"); wx.startRecord(); }); //结束录音 wx.ready(function(){ wx.stopRecord({ success: function (res) { var localId = res.localId; alert("录音结束"); saveAlert(localId); } }); }); //声音标题,声音描述 var voiceTitle = $("#voiceTitle").val(); var voiceDesc = $("#voiceDesc").val(); //将录音上传腾讯服务器 wx.ready(function(){ wx.uploadVoice({ localId: id, // 需要上传的音频的本地ID,由stopRecord接口获得 isShowProgressTips: 1, // 默认为1,显示进度提示 success: function (res) { var serverId = res.serverId; // 返回音频的服务器端ID saveIntoLocation(serverId,voiceTitle,voiceDesc); } }); }); } function saveIntoLocation(id,title,desc){ var channelId = $("#channelIdResult").val(); var proId = $("#procastIdResult").val(); //alert(proId); var url = "/taiyang/fm/saveInfoJson.action"; var postData = { "en1.channelId" : channelId, "en1.proTitle" : title, "en1.proDesc" : desc, "en1.podcastId" : proId, "serverId" : id, "appid" : appId }; $.post(url, postData, function (data) { if(data.success){ openSuccessLayer(data.message); }else{ openFailureLayer(data.message); } },‘json‘); }
(2)WXFM.XML <package name="xiaovFmJson" extends="wxJsonData" namespace="/xiaov/fm"> !--微电台:保存录音标题到本地数据库--> <action name="saveInfoJson" class="com.jerehsoft.taiyang.fm.action.weixin.WxRadioStationAction" method="saveInfo"> <result type="json"></result> </action> </package>
(3)WxRadioStationAction.java
public class WxRadioStationAction extends ActionSupport {
Log log = LogFactory.getLog(this.getClass());
private Map<String, Object> map1 = new HashMap<String, Object>();
ActionContext ac = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
private Long memberid;
private String appid;
private String openid;
private String serverId;
private boolean success;
private String message;
@EntityInject
private WxFmProgramComment en;
@EntityInject
private WxFmProgrem en1;
@Resource
IWxFmProgremService programService;
/**
* 微电台录音:保存录音标题到本地数据库
*
* @return
* @throws Exception
*/
public String saveInfo() throws Exception {
//设置播客id
try {
//下载腾讯素材
if (serverId != null && !serverId.equals("")) {
CommAttachmentDetail file = mediaSer.downLoadFile(getAppid(), serverId);
if (file != null) {
//picPath:amr文件存放的全路径
String path = SystemConfig.get("sys.upload.dir");
String picPath = path + File.separator + file.getPathName() + file.getFileName();
//音频转换
Conversion conversion = new Conversion();
conversion.ToMp3("D:" + File.separator + "Program Files", picPath);
String fileName = file.getFileName().substring(0, file.getFileName().indexOf("."));
String picPathDataBase = file.getPathName() + fileName + ".mp3";
en1.setFileAddr(picPathDataBase);
}
}
en1.setAppId(getAppid());
//时间
WxFmProgrem Progrem = programService.save(en1);
success = true;
map1.put("success", true);
map1.put("message", "提交成功");
} catch (Exception e) {
message = "提交失败";
success = false;
e.getStackTrace();
e.printStackTrace();
}
return SUCCESS;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getServerId() {
return serverId;
}
public void setServerId(String serverId) {
this.serverId = serverId;
}
}
(4) Conversion.java public class Conversion { /** * 将上传的录音转为mp3格式 * @param webroot 项目的根目录 * @param sourcePath 文件的相对地址 */ public static void ToMp3(String webroot, String sourcePath){ //File file = new File(sourcePath); String [] a =sourcePath.split("\\."); String targetPath = a[0]+".mp3";//转换后文件的存储地址,直接将原来的文件名后加mp3后缀名 Runtime run = null; try { run = Runtime.getRuntime(); long start=System.currentTimeMillis(); //Process p=run.exec(webroot+"files/ffmpeg -i "+webroot+sourcePath+" -acodec libmp3lame "+webroot+targetPath); // 执行ffmpeg.exe,前面是ffmpeg.exe的地址,中间是需要转换的文件地址,后面是转换后的文件地址。-i是转换方式,意思是可编码解码,mp3编码方式采用的是libmp3lame Process p=run.exec(webroot+"/ffmpeg/bin/ffmpeg -i "+sourcePath+" -acodec libmp3lame "+targetPath); //释放进程 p.getOutputStream().close(); p.getInputStream().close(); p.getErrorStream().close(); p.waitFor(); long end=System.currentTimeMillis(); //System.out.println(sourcePath+" convert success, costs:"+(end-start)+"ms"); //删除原来的文件 /*if(file.exists()){ file.delete(); }*/ } catch (Exception e) { e.printStackTrace(); }finally{ //run调用lame解码器最后释放内存 run.freeMemory(); } } public static void main(String[] args) { ToMp3("D:\\Program Files","E:\\a.amr"); } }
(5)注意点:
3个地址:webroot,sourcePath为绝对路径,webroot+"/ffmpeg/bin/ffmpeg"是自己电脑安装的ffmpeg的安装路径
电脑要安装FFMPEG,FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
FFMPEG的安装请参考:http://www.bubuko.com/infodetail-786878.html