有個需求是要在微信瀏覽器錄音,然后上傳,此處使用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="width:100%;padding:6px 0">
<source src="/upload/voice/end.mp3" type="audio/mp3">
<source src="/upload/voice/end.amr">
</audio>
因為chrome,firefox等等一些瀏覽器對於這個audio標簽的支持解析的音頻格式還不一樣,所以我們需要羅列出多種類型以便確保瀏覽器兼容.
原創-轉載請聯系huage