天天看点

amr转换成mp3 java_微信开发-将amr格式转换为mp3格式

importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.URL;

public classVoiceDownload {

public staticInputStream 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= newURL(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();

}returnis;

}

public static voidsaveImageToDisk(String accessToken, String mediaId, String picName, String picPath)throwsException {

InputStream inputStream=getInputStream(accessToken, mediaId);

String filePath= picPath+picName+".amr";byte[] data = new byte[10240];int len = 0;

FileOutputStream fileOutputStream= null;try{

fileOutputStream= newFileOutputStream(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 voidmain(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();

}

}

}