2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > amr转换成mp3 java_微信开发-将amr格式转换为mp3格式

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

时间:2021-02-24 22:17:47

相关推荐

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

importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;.HttpURLConnection;.URL;/*** Describe:从微信服务器下载音频

* Author:陆小不离

* Age:Eighteen

* Time:5月18日 下午1:21:07*/

public classVoiceDownload {/*** 根据文件id下载文件

*@parammediaId

* 媒体id

*

*@throwsException*/

public staticInputStream getInputStream(String accessToken, String mediaId) {

InputStream is= null;

String url= "http://file.api./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(".client.defaultConnectTimeout", "30000");//连接超时30秒

System.setProperty(".client.defaultReadTimeout", "30000"); //读取超时30秒

http.connect();//获取文件转化为byte流

is =http.getInputStream();

}catch(Exception e) {

e.printStackTrace();

}returnis;

}/***

* 获取下载图片信息(jpg)

*

*

*

*@parammediaId

*

* 文件的id

*

*@throwsException*/

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();

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。