这两天研究音频合并,找了很多方法,都不太好玩,amr和MP3文件合并还是比较简单的,这里就不多说,wav还是有点麻烦,最后总计封装了方法,本着奉献的精神
直接看源码吧,比较简单,亲测可用。可能还会有bug,目前我没有测出来,
Activity界面调用方法
public class Test {
static ArrayList<String> lists;
public static void main(String[] args) throws Exception {
String s1 = "D:/aaa/aaa.wav";
String s2 = "D:/aaa/bbb.wav";
String s3 = "D:/aaa/ccc.wav";
String s4 = "D:/aaa/ddd.wav";
String s5 = "D:/aaa/eee.wav";
lists = new ArrayList<String>();
lists.add(s1);
lists.add(s2);
lists.add(s3);
lists.add(s4);
lists.add(s5);
//很多人好奇,为什么这里没有写输出文件的全路径,因为我采用的事亮亮合并,
所以会产生很多文件出来,所以这里不能写死,不过不用担心,在合并成功后,
我会把文件的路径打印出来。
String outPath = "D:/aaa/";
WavTransUtil u = new WavTransUtil(lists, outPath);
u.merge(new MergeListener() {
public void unuseMerge() {
System.out.println("============不需要合并==");
}
public void success() {
System.out.println("=======合并成功=======");
}
public void start() {
System.out.println("==========合并开始====");
}
public void progress(int progress) {
System.out.println("=======progress=======" + progress);
}
public void faild() {
System.out.println("============合并失败==");
}
@Override
public void success(String path) {
System.out.println("==合并成功== " + path);
}
});
}
}
=========================================================================================
合并核心代码
package asfg;
import java.io.File;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.UUID;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
/**
* wav音频文件合并类
*
* @author dinlong 思路,两两合并,第一次使用前两个文件进行合并 后边的用刚合成的文件一次和后边的文件合并
*
*/
public class WavTransUtil {
int i = 0;
ArrayList<String> paths;
String outPath;
MergeListener listener;
/**
* 初始化类
*
* @param paths
* 需要和合并的集合String类型ArrayList
* @param outPath
* 输出文件全路径 "D:/reeman/"----- 注意有"---/---"
*/
public WavTransUtil(ArrayList<String> paths, String outPath) {
super();
this.paths = paths;
this.outPath = outPath;
}
/**
* 合并方法 合并成功之后,移除数组前两个数据 继续合并
*/
public void merge(MergeListener listener) {
String name = UUID.randomUUID().toString();
name = name.substring(0, 8);
listener.start();
listener.progress(paths.size());
if (paths.size() > 1) {
File in = new File(paths.get(0));
File in1 = new File(paths.get(1));
File out = new File(outPath + name + ".wav");
String namem = outPath + name + ".wav";
boolean iscc = comby(in, in1, out);
if (iscc) {
paths.remove(0);
paths.remove(0);
listener.progress(paths.size());
mergenext(namem, listener);
}
} else {
// 提示一个不需要合并
listener.unuseMerge();
}
}
/**
* 合并文件
*
* @param putname
* 刚才合并文件的路径,这个文件和剩余的文件一一合并
*/
String pathModify = "";
public void mergenext(String putname, MergeListener listener) {
String name = UUID.randomUUID().toString();
name = name.substring(0, 8);
listener.progress(paths.size());
if (paths.size() > 0) {
File f3 = new File(paths.get(0));
File f4 = new File(putname);
String outpath = outPath + name + ".wav";
File outfile = new File(outpath);
pathModify = outpath;
boolean iscc = comby(f3, f4, outfile);
if (iscc) {
paths.remove(0);
mergenext(outpath, listener);
}
} else {
listener.progress(paths.size());
listener.success(pathModify);
}
}
public boolean comby(File file1, File file2, File out) {
try {
AudioFileFormat aff = AudioSystem.getAudioFileFormat(file1);
AudioInputStream ais1 = AudioSystem.getAudioInputStream(file1);
AudioInputStream ais2 = AudioSystem.getAudioInputStream(file2);
SequenceInputStream sis = new SequenceInputStream(ais1, ais2);
AudioSystem.write(new AudioInputStream(sis, aff.getFormat(), ais1.getFrameLength() + ais2.getFrameLength()),
aff.getType(), out);
if (ais1 != null)
ais1.close();
if (ais2 != null)
ais2.close();
if (sis != null)
sis.close();
return true;
} catch (Exception e) {
}
return false;
}
public interface MergeListener {
/**
* 开始合并
*/
void start();
/**
* 合并进度
*
* @param progress
*/
void progress(int progress);
/**
* 合并成功
*/
void success(String path);
/**
* 合并失败
*/
void faild();
/**
* 不需要合并当前只有一个文件
*/
void unuseMerge();
}
}