有个需求是要在微信浏览器录音,然后上传,此处使用jssdk提供的录音接口,录完后会上传微信服务器并返回音频id,由于微信服务器只存3天时间,所以我们需要把文件下载到自己服务器进行维护,下载后的格式是.amr的,由于我需要在页面使用audio标签进行播放,但是有音频格式限制,需要转换为mp3,所以记录下.
下载音频代码:
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Describe:从微信服务器下载音频 * Author:陆小不离 * Age:Eighteen * Time:2017年5月18日 下午1:21:07 */ public class VoiceDownload { /** * 根据文件id下载文件 * @param mediaId * 媒体id * * @throws Exception */ public static InputStream getInputStream(String accessToken, String mediaId) { InputStream is = null; String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + accessToken + "&media_id=" + mediaId; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet .openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); // 获取文件转化为byte流 is = http.getInputStream(); } catch (Exception e) { e.printStackTrace(); } return is; } /** * * 获取下载图片信息(jpg) * * * * @param mediaId * * 文件的id * * @throws Exception */ public static void saveImageToDisk(String accessToken, String mediaId, String picName, String picPath) throws Exception { InputStream inputStream = getInputStream(accessToken, mediaId); String filePath = picPath+picName+".amr"; byte[] data = new byte[10240]; int len = 0; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath); while ((len = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //生成对应mp3格式 ChangeAudioFormat.changeToMp3(filePath, picPath+picName+".mp3"); } } public static void main(String[] args) { String token = "HlEl1p9pJ3oe1EnKZa5bz7R1-qdkoI9OCkvy2v4geOhBY60o0-z3s4vybzR_WztYyuGSEPZh8dnWd2zukCq-YVsRNfdfkYkKKyhxTgZAYV-nYFBly7nRwKyY-uj4MHGEBNQgAEANZC"; String mediaId = "r64ELwHiJndHHyhD94X887mLVEPXyw2RLoer8Nr3JkaI_tYc4J7uw2lOl55Hv8hI"; try { saveImageToDisk(token,mediaId,"test2","D:\ttt\"); } catch (Exception e) { e.printStackTrace(); } } }
将amr转换为mp3,需要依赖一个jar,此jar的下载地址为: http://www.sauronsoftware.it/projects/jave/download.php ,使用此jar进行格式转换会因系统不同而需要不用版本的jar,具体请下载对应版本.
import it.sauronsoftware.jave.AudioAttributes; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.EncodingAttributes; import it.sauronsoftware.jave.InputFormatException; import java.io.File; public class ChangeAudioFormat { public static void main(String[] args) throws Exception { String path1 = "D:\apache-tomcat-8.0.36\webapps\upload\voice\start.amr"; String path2 = "D:\apache-tomcat-8.0.36\webapps\upload\voice\end.mp3"; changeToMp3(path1, path2); } public static void changeToMp3(String sourcePath, String targetPath) { File source = new File(sourcePath); File target = new File(targetPath); AudioAttributes audio = new AudioAttributes(); Encoder encoder = new Encoder(); audio.setCodec("libmp3lame"); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); try { encoder.encode(source, target, attrs); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InputFormatException e) { e.printStackTrace(); } catch (EncoderException e) { e.printStackTrace(); } } }
转换时会抛异常,不用管我们只要关注结果就好,结果就是转换成功并且成功使用audio播放~
it.sauronsoftware.jave.EncoderException: Duration: N/A, bitrate: N/A at it.sauronsoftware.jave.Encoder.encode(Encoder.java:863) at it.sauronsoftware.jave.Encoder.encode(Encoder.java:713) at com.infogather.util.ChangeAudioFormat.changeToMp3(ChangeAudioFormat.java:29) at com.infogather.util.ChangeAudioFormat.main(ChangeAudioFormat.java:14)
页面这么写:
<audio controls="" preload="auto" style="100%;padding:6px 0">
<source src="/upload/voice/end.mp3" type="audio/mp3">
<source src="/upload/voice/end.amr">
</audio>
因为chrome,firefox等等一些浏览器对于这个audio标签的支持解析的音频格式还不一样,所以我们需要罗列出多种类型以便确保浏览器兼容.
原创-转载请联系huage