Android的无法合并两个波形文件 [英] Android Unable to merge two wave files
问题描述
我在android系统工作。我想合并两个波形文件,并希望创建第三个文件。为此,我创建了两个输入流,然后试着写这些输入流到一个输出流文件。
I am working in android. I want to merge two wave files and want to create third file. For this I create two input streams and then trying to write those input streams into one output stream file.
这是我的code: -
File file = new File("/sdcard/z.wav");
File file1 = new File("/sdcard/zz.wav");
InputStream is = new FileInputStream(file);
InputStream is1 = new FileInputStream(file1);
// Get the size of the file
long length = file.length();
long length1 = file1.length();
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
byte[] bytes1 = new byte[(int)length1];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < (int)length &&( numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
int offset1 = 0;
int numRead1 = 0;
while (offset1 < (int)length1 &&( numRead1=is1.read(bytes1, offset1, bytes1.length-offset1)) >= 0) {
offset1 += numRead1;
}
FileOutputStream fos=new FileOutputStream("sdcard/guruu.wav");
Log.v("Trying Activity","before first write");
fos.write(bytes);
fos.write(bytes1,0,bytes1.length);
fos.write(bytes);
fos.close();
is.close();
is1.close();
当我玩输出文件guruu.wav那么文件1 ,它是不是在玩file2的内容这只正在播放的文件。请给我建议我做了什么错误。是任何其他方式做到这一点?
when I play output file guruu.wav then this only playing file of file1, it is not playing content of file2. Please suggest me what mistake I have done. Is any other way to do this ?
我对这个很新,所以请不要投下来了。
I am very new in this so please do not down vote it.
感谢您提前。
推荐答案
您不合并数据,你只是串联的。
You are not merging the data, you are simply concatenating it.
因此,你最终会包含一个文件:
Thus you end up with one file containing:
- 文件1 WAV文件头
- 文件1组音频数据
- 文件2 WAV文件头
- 文件2的音频数据
理解的是,当有人读取该文件回来打它,他们认为这是
Understandably, when someone reads this file back to play it, they see it as
- WAV文件头
- 音频数据
- 在文件的结尾,他们甚至没有得到考虑的一些垃圾数据。
要真正合并这些文件,则需要适当考虑的WAV文件的内部结构。
To truly merge these files, you will need to properly consider the internal structure of the WAV files.
这篇关于Android的无法合并两个波形文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!