2013-10-21 120 views
2
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個字節。可能我缺少一個簡單的步驟。無法合併兩個mp3文件

回答

1

三件事情讓這件事情起作用^^

創建FILE不是目錄!

newFile.createNewFile(); 

然後另一個重要的部分是:創建fileoutputstream後創建文件!

第三,似乎序列輸入流不適合我,當我使用two-arguemnt構造函數,而是使用構造函數與枚舉數。

這裏的總結^^

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這裏工作......