本文主要是介绍语音amr文件转换为mp3文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.安卓前端生成amr语音文件,经过base64加密变成加密字符串
2.把加密后的字符串解密成amr文件
生成amr文件目录
String fileUrl = System.getProperty("user.dir").replace(
"bin", "webapps")
+ File.separator
+ "wfCloudRes"
+ File.separator
+ "image"
+ File.separator
+ dateStr
+ File.separator + "topic" + File.separator + maxId;
public static String base64ToIo(String strBase64,String fileUrl) throws IOException {
String string = strBase64;
String fileName = fileUrl+File.separator+"audio.amr";
String saveDir = fileUrl;
File dir = new File(saveDir);
if(!dir.exists()){
dir.mkdirs();
}
try {
// 解码,然后将字节转换为文件
byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
FileOutputStream out = new FileOutputStream(fileName);
out.write(bytes);
out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fileName;
}
3.amr文件转换为MP3文件
这个转化要使用第三方的jar包jave-1.0.2.jar
http://www.sauronsoftware.it/projects/jave/download.php
// mp3文件目录
String fileTargetUrl = System.getProperty("user.dir")
.replace("bin", "webapps")
+ File.separator
+ "wfCloudRes"
+ File.separator
+ "image"
+ File.separator
+ dateStr
+ File.separator
+ "topic"
+ File.separator
+ maxId
+ File.separator + "audio.mp3";
public static void changeToMp3(String sourcePath, String targetPath) {
File source = new File(sourcePath);
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
// audio.setBitRate(new Integer(64000));
// audio.setChannels(new Integer(2));
// audio.setSamplingRate(new Integer(22050));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
try {
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InputFormatException e) {
e.printStackTrace();
} catch (EncoderException e) {
e.printStackTrace();
}
}
4.删除amr文件
FileUtils.deleteFile(fileUrl, "amr");
public static boolean deleteFile(String path, String suffixType) {
boolean flag = false;
if (StringUtils.isEmpty(path)) {
return flag;
}
File file = new File(path);// 里面输入特定目录
File temp = null;
File[] filelist = file.listFiles();
for (int i = 0; i < filelist.length; i++) {
temp = filelist[i];
if (temp.getName().endsWith(suffixType)) {// 获得文件名,如果后缀为“”,这个你自己写,就删除文件
temp.delete();// 删除文件
flag = true;
}
}
return flag;
}
Base64加密和解密工具类
mport java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Utils {
public static void main(String[] args) throws Exception {
// String strBase64 = Base64Utils.ioToBase64("d:/bubugao.amr"); // 将 io 转换为 base64编码
// System.out.println(">>> " + strBase64);
// System.out.println(Base64Utils.base64ToIo(strBase64,"d:/bubugao.mp3")); // 将 base64编码转换为
// 文件流,生成一幅新图片
// System.out.println(Base64Utils.encodeBase64File("d:/bubugao.amr"));
}
public static String ioToBase64(String path) throws IOException {
// String fileName = "D:/u18.jpg"; // 源文件
String source = path;
String strBase64 = null;
try {
InputStream in = new FileInputStream(source);
// in.available()返回文件的字节长度
byte[] bytes = new byte[in.available()];
// 将文件中的内容读入到数组中
in.read(bytes);
strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
in.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return strBase64;
}
public static String base64ToIo(String strBase64,String fileUrl) throws IOException {
String string = strBase64;
String fileName = fileUrl+File.separator+"audio.amr";
String saveDir = fileUrl;
File dir = new File(saveDir);
if(!dir.exists()){
dir.mkdirs();
}
try {
// 解码,然后将字节转换为文件
byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组
FileOutputStream out = new FileOutputStream(fileName);
out.write(bytes);
out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fileName;
}
}
文件转换类的代码如下:
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 Mp3Convert {
public static void changeToMp3(String sourcePath, String targetPath) {
File source = new File(sourcePath);
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
// audio.setBitRate(new Integer(64000));
// audio.setChannels(new Integer(2));
// audio.setSamplingRate(new Integer(22050));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
try {
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InputFormatException e) {
e.printStackTrace();
} catch (EncoderException e) {
e.printStackTrace();
}
}
}
这篇关于语音amr文件转换为mp3文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!