如何Concat的2档 [英] How to concat 2 files

查看:157
本文介绍了如何Concat的2档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何Concat的两个文件。

我有两个音频部分(各含有大约3秒音频的来自同一来源)。
我试图合并这两个文件,​​并与Android的媒体播放器播放的主题。
目前我使用下面的方法,它工作正常,但需要大量的时间(大约13秒我的Galaxy Nexus)。

所以我的问题是,有没有办法这样快呢?

 公共静态无效的合并(文件audioFile1,文件audioFile2,文件OUTPUTFILE){
    长timeStart = System.currentTimeMillis的();
    尝试{        的FileInputStream fistream1 =新的FileInputStream(audioFile1);
        的FileInputStream fistream2 =新的FileInputStream(audioFile2);
        的SequenceInputStream sistream =新的SequenceInputStream(fistream1,fistream2);
        FileOutputStream中fostream =新的FileOutputStream(OUTPUTFILE);        INT温度;        而((TEMP = sistream.read())!= -1)
        {            fostream.write(临时);
        }        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    长timeEnd = System.currentTimeMillis的();    Log.e(合并定时器:,毫秒:+(timeEnd - timeStart));
}


解决方案

替换

  INT温度;
而((TEMP = sistream.read())!= -1){
    fostream.write(临时);
}

与缓冲副本:

 诠释计数;
字节[] TEMP =新的字节[4096];
而((计数= sistream.read(临时))!= - 1){
    fostream.write(温度,0,计数);
}

这将一次读取最多4096字节,而不是一次1个字节。

的BufferedReader / 的BufferedWriter 可以提高性能,甚至更多。

How can i concat two files.

I have two audio parts (each contains about 3 seconds of audio from the same source). I'm trying to merge those two files and play theme with the android mediaplayer. At the moment i'm using the method below, which works fine but takes a lot of time (around 13 seconds on my galaxy nexus).

So my question is, is there a way to do this faster?

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

解决方案

Replace

int temp;
while((temp = sistream.read()) != -1) {
    fostream.write(temp);  
}

with a buffered copy:

int count;
byte[] temp = new byte[4096];
while((count = sistream.read(temp)) != -1) {
    fostream.write(temp, 0, count);  
}

This will read at most 4096 bytes at once, not 1 byte at a time.

BufferedReader / BufferedWriter may improve performance even more.

这篇关于如何Concat的2档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆