4

I want to mix two mp3 files into one audio file( I want to impose one audio over another NOT concatenating ) I searched for third libraries
but i could not find . Can you help me to find any library ?

I used this code but I do not hear any thing ? Also, how to save this new file to device ?

Below code is:

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
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Mina Farid
  • 5,041
  • 4
  • 39
  • 46

0 Answers0