合并 pcm 音频文件 [英] Merging pcm audio files
问题描述
我使用 AudioRecorder 录制了音频.我需要将录制的文件合并为一个文件.任何建议.
I have recorded audio using AudioRecorder. I need to merge the recorded files into a single file. Any suggestion.
getAudioPath() -- 音频文件的路径.getCombineFile()---合并文件的路径.我的问题是第一个文件单独播放,而不是那个目录下的整个文件
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));
}*/
}
推荐答案
如果你使用 AudioRecord.read()
,我假设你有一个短数组或字节数组的 PCM 数据.如果是这种情况,您需要做的就是创建一个与原始数组一样大的新数组,然后一个接一个地复制数据.像这样:
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];
然后你就有了一个包含所有 PCM 数据的数组,你可以随意使用它.
Then you have one array with all the PCM data, and you can do with that what you will.
这篇关于合并 pcm 音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!