【问题标题】:Merging pcm audio files合并 pcm 音频文件
【发布时间】:2012-06-20 13:11:18
【问题描述】:

我使用 AudioRecorder 录制了音频。我需要将录制的文件合并到一个文件中。任何建议。

getAudioPath() -- 音频文件的路径。 getCombineFile()---组合文件的路径。我的问题是单独播放第一个文件,而不是该目录中的整个文件

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

            }

【问题讨论】:

  • 合并是指按顺序制作一个文件,还是同时播放?
  • 我想将它们作为单个文件按顺序播放,没有暂停

标签: android audio


【解决方案1】:

如果您使用的是AudioRecord.read(),我假设您将 PCM 数据保存在短数组或字节数组中。如果是这种情况,您需要做的就是创建一个与两个原始数组一样大的新数组,然后一个接一个地复制数据。像这样的:

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];

然后你有一个包含所有 PCM 数据的数组,你可以随心所欲地使用它。

【讨论】:

  • 感谢 Geobits,但我可以记录尽可能多的文件。那么我应该在 short[] 的长度中给出什么。
  • 输出的 short[] 长度是所有输入长度的总和
  • Geobits,我添加了用于将文件组合成单个文件的代码。
  • 我不建议在内存不足的手机上复制这样的字节。考虑使用固定大小的缓冲区(大约 8k 左右),然后从输入流读取到数组并写入输出流。
  • @Justin,因为你覆盖了文件,所以使用 FileOutputStream(File file, boolean append), with append = true
猜你喜欢
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2014-09-12
  • 1970-01-01
相关资源
最近更新 更多