我怎样才能连接两个文件。
我有两个音频部分(每个包含来自同一来源的大约 3 秒的音频)。 我正在尝试合并这两个文件并使用 android 媒体播放器播放主题。 目前我正在使用下面的方法,它工作正常但需要很多时间(在我的 galaxy nexus 上大约 13 秒)。
所以我的问题是,有没有办法更快地做到这一点?
public static void merge(File audioFile1, File audioFile2, File outputFile){
long timeStart= System.currentTimeMillis();
try {
FileInputStream fistream1 = new FileInputStream(audioFile1);
FileInputStream fistream2 = new FileInputStream(audioFile2);
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream(outputFile);
int temp;
while( ( temp = sistream.read() ) != -1)
{
fostream.write(temp);
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long timeEnd= System.currentTimeMillis();
Log.e("merge timer:", "milli seconds:" + (timeEnd - timeStart));
}
最佳答案
替换
int temp;
while((temp = sistream.read()) != -1) {
fostream.write(temp);
}
带有缓冲副本:
int count;
byte[] temp = new byte[4096];
while((count = sistream.read(temp)) != -1) {
fostream.write(temp, 0, count);
}
这将一次最多读取 4096 个字节,而不是一次读取 1 个字节。
BufferedReader
/BufferedWriter
可能会进一步提高性能。
关于android - 如何连接2个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743678/