无法合并两个mp3文件 [英] Unable to merge two mp3 files

查看:166
本文介绍了无法合并两个mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

我得到一个新的文件theonkar10.mp3但该文件是0 bytes.Probably我缺少一个简单的步骤。

I am getting a new file theonkar10.mp3 but the file is of 0 bytes.Probably I am missing a simple step.

推荐答案

三件事让这件事的工作^^

three things to get this thing working ^^

创建该文件不是目录!

newFile.createNewFile();

然后另一个重要组成部分是:创建FileOutputStream中后您创建的文件

then another important part is: create the fileoutputstream AFTER you created the file!

第三,它似乎工作的SequenceInputStream无法正常对我来说,当我使用两arguemnt构造函数,而不是使用构造函数与枚举。

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.

这里的总结^^

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

它的工作在这里与我的ENV ...

it's working here with my env...

这篇关于无法合并两个mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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