我想将两个mp3文件混合成一个音频文件(我想将一个音频强加给另一个不连接的音频)我搜索了第三个库 但我找不到。你能帮我找一个图书馆吗?
我使用了这段代码,但我没有听到任何声音?另外,如何将这个新文件保存到设备?
代码如下:
private void mixSound() throws IOException
{
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, 44100, AudioTrack.MODE_STREAM);
InputStream in1=getResources().openRawResource(R.raw.naweha);
InputStream in2=getResources().openRawResource(R.raw.youmat2belna);
byte[] music1 = null;
music1= new byte[in1.available()];
music1=convertStreamToByteArray(in1);
in1.close();
InputStream str;
byte[] music2 = null;
music2= new byte[in2.available()];
music2=convertStreamToByteArray(in2);
in2.close();
byte[] output = new byte[music1.length];
audioTrack.play();
for(int i=0; i < output.length; i++)
{
float samplef1 = music1[i] / 128.0f; // 2^7=128
float samplef2 = music2[i] / 128.0f;
float mixed = samplef1 + samplef2;
// reduce the volume a bit:
mixed *= 0.8;
// hard clipping
if (mixed > 1.0f) mixed = 1.0f;
if (mixed < -1.0f) mixed = -1.0f;
byte outputSample = (byte)(mixed * 128.0f);
output[i] = outputSample;
} //for loop
audioTrack.write(output, 0, output.length);
audioTrack.play();
}
public static byte[] convertStreamToByteArray(InputStream is) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[10240];
int i = Integer.MAX_VALUE;
while ((i = is.read(buff, 0, buff.length)) > 0)
{
baos.write(buff, 0, i);
}
return baos.toByteArray(); // be sure to close InputStream in calling function
}