gpt4 book ai didi

java - 在android中顺序合并2个音频文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:44 24 4
gpt4 key购买 nike

如何在 android 平台上将两个音频文件合并为一个文件。也就是说,如果我有两个音频文件,即 A 和 B。请给我创建一个新的音频文件 C 的代码,该文件由 B 附加在 A 之后。是否需要将音频文件转换为字节/短数组??...

我确实喜欢这个,但它只播放第一个文件。

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

提前致谢。

最佳答案

要合并 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();

关于java - 在android中顺序合并2个音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com