2
我試圖從麥克風錄音,並將錄製的數據發送到只有左聲道,分別在右聲道有零,但我的技術似乎不工作.. 我正在使用音頻記錄和PCM 16和單聲道音頻軌道模式我似乎做錯了什麼?左右聲道與麥克風錄音分離
package com.example.leftrighttest;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int freq=44100;
final int bufferSize = (AudioRecord.getMinBufferSize(freq,AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT));
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq,AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
(int) freq,AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
audioTrack.setPlaybackRate(freq);
final byte[] buffer = new byte[bufferSize];
audioRecord.startRecording();
byte[] byteBufferFinal = new byte[bufferSize*2];
//LL RR LL RR LL RR
while(true)
{
audioRecord.read(buffer, 0, bufferSize);
for(int k = 0, index = 0; index < byteBufferFinal.length - 4; k=k+2){
byteBufferFinal[index] = buffer[k]; // LEFT {0,1/4,5/8,9/12,13;...}
//System.out.println(byteBufferFinal[index]);
byteBufferFinal[index+1] = buffer[k+1];
// System.out.println(byteBufferFinal[index+1]);
index = index + 2;
byteBufferFinal[index] =0; //byteBuffer2[k]; // RIGHT {2,3/6,7/10,11;...}
//System.out.println(byteBufferFinal[index]);
byteBufferFinal[index+1] =0;// byteBuffer2[k+1];
// System.out.println(byteBufferFinal[index+1]);
index = index + 2;
}
audioTrack.write(byteBufferFinal, 0, bufferSize*2);
audioTrack.play();
}
}
}
您正在創建單聲道「AudioTrack」,因此它只有一個聲道。 – Michael
是的,你是對的,謝謝(y) – Dina