3

I have recorded audio using AudioRecorder. I need to merge the recorded files into a single file. Any suggestion.

getAudioPath() -- the path of the audiofiles. getCombineFile()--- the path of the combined file. My problem is the first file alone play, not the entire file in that directory

public void readAudioAsStream()
            {
                getAudioPath();
                File f=null;
                FileInputStream ins = null;
                ReadSDDatas rds=new ReadSDDatas();
                try 
                {
                    String comfile=rds.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[read];
                            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)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                /*String path=audFullPath.get(val);
                playAudio(path);*/
                playAudio();
                /*
                for(int i=0;i<audFullPath.size();i++)
                {
                    Log.d("fullpathsize",Integer.toString(audFullPath.size()));
                    playAudio(audFullPath.get(i));
                }*/

            }
Manikandan
  • 1,479
  • 6
  • 48
  • 89

1 Answers1

0

If you're using AudioRecord.read(), I'm assuming you have the PCM data in a short or byte array. If that's the case, all you need to do is create a new array as large as both of the originals, and copy the data over, one after the other. Something like this:

short[] newData = new short[dataOne.length + dataTwo.length];
for(int i=0;i<dataOne.length;i++)
    newData[i] = dataOne[i];
for(int i=0;i<dataTwo.length;i++)
    newData[i+dataOne.length] = dataTwo[i];

Then you have one array with all the PCM data, and you can do with that what you will.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Thanks Geobits, but I can record as many files. Then what should be I give in the length of the short[]. – Manikandan May 30 '11 at 08:30
  • The output short[] length would be the sum of all the input lengths added together – Geobits May 30 '11 at 08:54
  • Geobits, I have added the code which I used to combine the files into a single file. – Manikandan May 30 '11 at 11:05
  • 4
    I would not recommend copying bytes like this on a cell phone where memory is scarce. Consider using a fixed size buffer (something like 8k or so), and then read from your input stream to your array and write to an output stream.. – Matt Wolfe Nov 02 '11 at 19:03
  • 1
    @Justin, because you overwrite file, use FileOutputStream(File file, boolean append), with append = true – pawegio Jul 26 '15 at 14:26