다음 코드를 실행하면 소리가 출력되지 않고 소리가납니다.혼합 된 사운드 대신 출력으로 노이즈가 발생합니다.
내 리소스 폴더에 2 개의 오디오 파일이 있고 1 입력 스트림을 사용하면 bytearray로 변환됩니다. 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);
Log.i(tag,"inside mixSound");
InputStream in1=getResources().openRawResource(R.raw.cut1); s
InputStream in2=getResources().openRawResource(R.raw.cut2);
byte[] music1 = null;
music1= new byte[in1.available()];
Log.i(tag,"in1");
music1=convertStreamToByteArray(in1);
in1.close();
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);
}
public static byte[] convertStreamToByteArray(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[10240];
int i = Integer.MAX_VALUE;
Log.i(tag,"in csb");
while ((i = is.read(buff, 0, buff.length)) > 0) {
baos.write(buff, 0, i);
}
return baos.toByteArray();
}
미리 도움을 주셔서 감사합니다.
오디오 파일 형식은 어느 것입니까? – Michael
나는 이것을 위해 wav 형식의 파일을 사용했다. 그러나 여전히 효과가 없었습니다. – ank
나는 그것보다 구체적으로 말했습니다. 파일에 헤더 데이터가 포함되어 있습니까? 오디오 압축 또는 원시 PCM입니까? 그것은 샘플 당 8 비트 또는 16 비트를 사용합니까? – Michael