【问题标题】:Unable to merge two mp3 files无法合并两个 mp3 文件
【发布时间】:2013-10-21 08:16:21
【问题描述】:
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 字节。可能我缺少一个简单的步骤。

【问题讨论】:

    标签: java android mp3 fileinputstream fileoutputstream


    【解决方案1】:

    三件事让这个东西正常工作^^

    创建文件而不是目录!

    newFile.createNewFile();
    

    那么另一个重要的部分是:创建文件后创建文件输出流!

    第三,当我使用双参数构造函数时,sequenceinputstream 似乎不适合我,而是使用带有枚举器的构造函数。

    这里是总结^^

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

    它在我的环境中工作......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2011-12-06
      • 2011-10-07
      • 2012-05-16
      • 1970-01-01
      相关资源
      最近更新 更多