在android中混合音频

在android中混合音频,android,Android,我试着点击这个链接: 但在混合音频文件后,SD卡上的文件(mixed.wav)无法播放,我不知道为什么。 你能帮助我吗?。非常感谢 这是我的代码: 公共类MainActivity扩展了活动{ public static final int FREQUENCY = 44100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

我试着点击这个链接:

但在混合音频文件后,SD卡上的文件(mixed.wav)无法播放,我不知道为什么。 你能帮助我吗?。非常感谢

这是我的代码:

公共类MainActivity扩展了活动{

 public static final int FREQUENCY = 44100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        mixSound();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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.media_b);
    InputStream in2 = getResources().openRawResource(R.raw.media_c);

    byte[] arrayMusic1 = null;
    arrayMusic1 = new byte[in1.available()];
    arrayMusic1 = createMusicArray(in1);
    in1.close();

    byte[] arrayMusic2 = null;
    arrayMusic2 = new byte[in2.available()];
    arrayMusic2 = createMusicArray(in2);
    in2.close();


    byte[] output = new byte[arrayMusic1.length];

    audioTrack.play();

    for (int i = 0; i < output.length; i++) {
        float samplef1 = arrayMusic1[i] / 128.0f; 
        float samplef2 = arrayMusic2[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;   
    }

    audioTrack.write(output, 0, output.length);
    convertByteToFile(output);
}

public static byte[] createMusicArray(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

}

public static void convertByteToFile(byte[] fileBytes) throws FileNotFoundException {

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/mixed.wav"));
    try {
        bos.write(fileBytes);
        bos.flush();
        bos.close();    
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
公共静态最终整数频率=44100;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
试一试{
混音();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有void mixSound()引发IOException{
AudioTrack AudioTrack=新的AudioTrack(AudioManager.STREAM\u MUSIC,44100,AudioFormat.CHANNEL\u OUT\u立体声,AudioFormat.ENCODING\u PCM\u 16位,44100,AudioTrack.MODE\u STREAM);
InputStreamin1=getResources().openRawResource(R.raw.media_b);
InputStream in2=getResources().openRawResource(R.raw.media_c);
字节[]arrayMusic1=null;
arrayMusic1=新字节[in1.available()];
arrayMusic1=createMusicArray(in1);
in1.close();
字节[]arrayMusic2=null;
arrayMusic2=新字节[in2.available()];
arrayMusic2=createMusicArray(in2);
in2.close();
字节[]输出=新字节[arrayMusic1.length];
音轨播放();
for(int i=0;i1.0f)混合=1.0f;
如果(混合<-1.0f)混合=-1.0f;
字节输出样本=(字节)(混合*128.0f);
输出[i]=输出样本;
}
audioTrack.write(输出,0,输出,长度);
convertByteToFile(输出);
}
公共静态字节[]createMusicArray(InputStream is)引发IOException{
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]buff=新字节[10240];
int i=整数的最大值;
而((i=is.read(buff,0,buff.length))>0){
写入(buff,0,i);
}
返回baos.toByteArray();//确保在调用函数时关闭InputStream
}
公共静态void convertbyteofile(byte[]fileBytes)引发FileNotFoundException{
BufferedOutputStream bos=新的BufferedOutputStream(新文件输出流(Environment.getExternalStorageDirectory().getPath()+“/mixed.wav”);
试一试{
写入(文件字节);
bos.flush();
bos.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

}

您输出的只是PCM数据。有效的WAV文件还需要一个标头:

Offset    Size  Name           Description
------------------------------------------------------------------------
0         4     ChunkID        Contains the letters "RIFF" in ASCII form
                               (0x52494646 big-endian form).
4         4     ChunkSize      36 + SubChunk2Size, or more precisely:
                               4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
                               This is the size of the rest of the chunk 
                               following this number.  This is the size of the 
                               entire file in bytes minus 8 bytes for the
                               two fields not included in this count:
                               ChunkID and ChunkSize.
8         4     Format         Contains the letters "WAVE"
                               (0x57415645 big-endian form).

12        4     Subchunk1ID    Contains the letters "fmt "
                               (0x666d7420 big-endian form).
16        4     Subchunk1Size  16 for PCM.  This is the size of the
                               rest of the Subchunk which follows this number.
20        2     AudioFormat    PCM = 1 (i.e. Linear quantization)
                               Values other than 1 indicate some 
                               form of compression.
22        2     NumChannels    Mono = 1, Stereo = 2, etc.
24        4     SampleRate     8000, 44100, etc.
28        4     ByteRate       == SampleRate * NumChannels * BitsPerSample/8
32        2     BlockAlign     == NumChannels * BitsPerSample/8
                               The number of bytes for one sample including
                               all channels. I wonder what happens when
                               this number isn't an integer?
34        2     BitsPerSample  8 bits = 8, 16 bits = 16, etc.
          2     ExtraParamSize if PCM, then doesn't exist
          X     ExtraParams    space for extra parameters

36        4     Subchunk2ID    Contains the letters "data"
                               (0x64617461 big-endian form).
40        4     Subchunk2Size  == NumSamples * NumChannels * BitsPerSample/8
                               This is the number of bytes in the data.
                               You can also think of this as the size
                               of the read of the subchunk following this 
                               number.
在此之后,您将写入PCM数据


()。

怎么修,我不知道怎么修。你能帮助我吗?非常感谢:)您编写了标题中指定的所有字段。首先是字符串
RIFF
,然后是包含音频数据
36+大小的32位值,然后是字符串
WAVE
,依此类推。非常感谢。我的成功好坏参半。但混合了带噪波的.wav文件。如何修复它,消除白噪声。如何处理wav字节数组。你能帮我吗?你应该在页眉后立即写入PCM数据。您可以通过将PCM数据转储到一个没有任何头的文件中,然后将其作为原始PCM数据导入您的PC上的Audacity,来再次检查您的PCM数据是否正常。在我切换到short[]后,它工作得非常糟糕:(我不知道我错在哪里)。