简体   繁体   English

无法合并两个mp3文件

[英]Unable to merge two mp3 files

public class MainActivity extends Activity {
    FileInputStream fistream2,fistream1;


    File newFile=new File(Environment.getExternalStorageDirectory()
            +File.separator
            +"newfolder" //folder name
            +File.separator
            +"media"
            +File.separator
            +"player"+File.separator+"theonkar10.mp3"); 


    File newFile1=new File(Environment.getExternalStorageDirectory()
            +File.separator
            +"newfolder" //folder name
            +File.separator
            +"media"
            +File.separator
            +"player"+File.separator+"1.mp3"); 

    File newFile2=new File(Environment.getExternalStorageDirectory()
            +File.separator
            +"newfolder" //folder name
            +File.separator
            +"media"
            +File.separator
            +"player"+File.separator+"2.mp3"); 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            myMethod();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }





    public  void myMethod() throws IOException
    {

        FileInputStream fistream1 = new FileInputStream(newFile1.getAbsolutePath());  // first source file
        FileInputStream fistream2= new FileInputStream(newFile2.getAbsolutePath());//second source file
        //SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        // FileOutputStream fostream = new FileOutputStream("C:\\Temp\\final.mp3");//destinationfile
        FileOutputStream fostream=new FileOutputStream(newFile.getAbsolutePath(),true);

        if(!newFile.exists()){
            newFile.mkdirs();
            int temp;

            while( ( temp = sistream.read() ) != -1)
            {
                System.out.print( (char) temp ); // to print at DOS prompt
                fostream.write(temp);   // to write to file
            }
            fostream.close();
            sistream.close();
            fistream1.close();
            fistream2.close();
        }
    }

}

I am getting a new file theonkar10.mp3 but the file is of 0 bytes.Probably I am missing a simple step. 我正在获取一个新文件theonkar10.mp3,但是该文件的大小为0个字节。可能我错过了一个简单的步骤。

three things to get this thing working ^^ 使这件事正常工作的三件事^^

create the FILE not the directory! 创建FILE而不是目录!

newFile.createNewFile();

then another important part is: create the fileoutputstream AFTER you created the file! 那么另一个重要的部分是:创建文件后创建文件输出流!

and third, it seems the sequenceinputstream works not properly for me, when i use the two-arguemnt-constructor, instead use the constructor with the enumerator. 第三,当我使用two-arguemnt-constructor而不是将构造器与枚举器一起使用时,sequenceinputstream对我来说似乎工作不正常。

here's the summary ^^ 这是摘要^^

public  void myMethod() throws IOException
{
    FileInputStream fistream1 = new FileInputStream(newFile1 );  // first source file
    FileInputStream fistream2= new FileInputStream(newFile2 );//second source file
    Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(fistream1);
    v.add(fistream2);
    SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);

    if(!newFile.exists()){
        newFile.createNewFile();
        FileOutputStream fostream=new FileOutputStream(newFile, true);
        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write((byte)temp);   // to write to file
        }

        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}

it's working here with my env... 它正在与我的环境一起工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM