Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

android - Merging pcm audio files

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));
                }*/

            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...