2011-05-26 77 views
1

我录制了一些音频文件,存储在SD卡中。我需要将所有录制的文件合并成一个音频文件。我使用了下面的代码。我的问题是组合文件只包含第一个录制的文件。任何建议...在readAudioAsStream()方法,我试图结合这些文件。合并音频文件

public void readAudioAsStream() { 
    getFullAudioPath() 

    File f; 
    FileInputStream ins = null; 

    try 
    { 
     String comfile=getCombineFile(); 
     //FileOutputStream fos=new FileOutputStream(comfile); 
     Log.d("combined file",comfile); 
     File file=new File(comfile); 
     RandomAccessFile raf = new RandomAccessFile(file, "rw"); 
     Log.d("path size",Integer.toString(audFullPath.size())); 
     for (int i=0;i<audFullPath.size();i++) 
     { 
      String filepath=audFullPath.get(i); 
      Log.d("Filepath",filepath); 
      f=new File(audFullPath.get(i)); 
      fileContent = new byte[(int)f.length()]; 
      ins=new FileInputStream(audFullPath.get(i)); 
      int numofbytes=ins.read(fileContent); 
      System.out.println("Number Of Bytes Read===========>>>"+numofbytes); 
      raf.seek(file.length()); 
      raf.write(fileContent); 
     } 
    } 
    catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    }   
} 
public ArrayList<String> getFullAudioPath() { 
    ArrayList<String> fullPath=new ArrayList<String>(); 
    fullPath.clear(); 

    String path=filePath(); 
    File f=new File(path); 
    if(f.isDirectory()) 
    { 
     File[] files=f.listFiles(); 
     for(int i=0;i<files.length;i++) 
     { 
      String fpath=path+File.separator+files[i].getName().toString().trim(); 
      System.out.println("File Full Path======>>>"+fpath); 
      fullPath.add(fpath); 
     } 
    }  
    return fullPath; 
} 
public String filePath() { 
    String newFolderName="/MyAudio"; 
    String extstoredir=Environment.getExternalStorageDirectory().toString(); 
    String newPath=extstoredir+newFolderName; 
    return newPath; 
} 

public String getCombineFile() { 
    String newFolderName="/MyComAudio"; 
    String extstoredir=Environment.getExternalStorageDirectory().toString(); 
    String path=extstoredir+newFolderName; 
    File myNewPath=new File(path); 
    if(!myNewPath.exists()) { 
     myNewPath.mkdir(); 
    } 
    String audname="ComAudio"; 
    String ext=".3gp"; 
    File audio=new File(myNewPath,audname+ext);    

    if(audio.exists()) { 
     audio.delete(); 
    } 
    String audpath=path+"/"+audname+ext; 
    Log.d("Combined audio file",audpath); 
    return audpath; 
} 
+0

什么是您的音频文件的格式? – inazaruk 2011-05-26 11:16:34

+0

我正在录制3gp文件 – Manikandan 2011-05-26 11:19:33

回答

-1

你不能仅仅通过在另一.3gp文件的末尾写一个.3gp文件的内容合并两个.3gp文件。

+0

然后,它可以用于任何其他格式......或任何解决方案 – Manikandan 2011-05-26 11:29:26

+0

您可以使用'AudioRecorder'将音频录制到文件。这会给你原始PCM格式的音频文件。你可以将它们合并在一起。但结果文件不会被播放。您需要在该文件的开头添加正确的'WAV'标题以使其可以播放。请注意'AudioRecorder'很难使用MediaRecorder。 – inazaruk 2011-05-26 11:42:23

+0

我在开发人员站点找不到关于AudioRecorder的任何信息。你能否建议我任何有用的链接或任何样本编码。 – Manikandan 2011-05-26 11:57:56

相关问题