如何将两个音频文件合并到android平台中的单个文件中。 那就是如果我有两个音频文件即A和B.请给我代码来创建一个新的音频文件C,在A之后由B附加。 是否需要将音频文件转换为字节/短数组?? ...
我确实喜欢这个,但它只播放第一个文件。
File firstFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict136.3gp");
File secondFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict139.3gp");
System.out.println("1 file:::::"+firstFile+"\n 2nd file::"+secondFile);
FileInputStream fistream1;
try {
fistream1 = new FileInputStream(firstFile);
FileInputStream fistream2 = new FileInputStream(secondFile);//second source file
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream("/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp");//destinationfile
int temp;
while( ( temp = sistream.read() ) != -1)
{
System.out.print( (char) temp ); // to print at DOS prompt
fostream.write(temp); // to write to file
}
filePath="/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp";
playAudio(filePath);
fostream.close();
fistream1.close();
fistream2.close();
提前感谢。
答案 0 :(得分:5)
要合并3gp或mp4容器中的音频文件,您应该能够使用https://github.com/sannies/mp4parser 看看样品: https://github.com/sannies/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/AppendExample.java
获取mp4parser jar(通过maventral)并执行此操作:
File firstFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict136.3gp");
File secondFile=new File("/mnt/sdcard/Android/data/Dictation/Audio/Dict139.3gp");
Movie movieA = MovieCreator.build(firstFile);
Movie movieB = MovieCreator.build(secondFile);
final Movie finalMovie = new Movie();
List<Track> audioTracks = new ArrayList<>();
for (Movie movie : new Movie[] { movieA, movieB}) {
for (Track track : movie.getTracks()) {
if (track.getHandler().equals("soun")) {
audioTracks.add(track);
}
}
}
finalMovie.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
final Container container = new DefaultMp4Builder().build(finalMovie);
File mergedFile = new File("/mnt/sdcard/Android/data/Dictation/Audio/merged.3gp");
final FileOutputStream fos = new FileOutputStream(mergedFile);
FileChannel fc = new RandomAccessFile(mergedFile, "rw").getChannel();
container.writeContainer(fc);
fc.close();
答案 1 :(得分:0)
您可以尝试使用File Input/output Stream
。要读取字节数组中的任何文件,请使用
byte[] buffer = new byte[FILE_SIZE_HERE];
FileInputStream fs = new FileInputStream(file);
int bytesRead = 0;
bytesRead = fs.read(buffer, 0, FILE_SIZE_HERE);
这里buffer
包含文件的字节数组。通过这种方式,您可以获取文件的字节数组。现在你已经采取了bot文件的字节数组的第二步,你需要将两个字节数组复制到一个单独的数组中,然后你可以使用这个(第三个新数组)将它写入一个新文件
FileOutputStream fout = new FileOutputStream(tempFile, true);
fout.write(data);
fout.close();
data
是合并两个数组后的新第3个数组
编辑: See this how to combine two arrays
希望这会对你有所帮助。