我正在尝试使用mediacodec lib在我的设备上即时录制和播放声音。我知道我应该录制声音,然后传递它作为字节流播放。我很困惑,怎么办? 谁能帮我?如果是,请用代码解释。
我的代码与mediarecord:
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.util.Log;
public class Speak {
//static final int bufferSize = 200000;
//final short[] buffer = new short[bufferSize];
//short[] readBuffer = new short[bufferSize];
private boolean isRecording;
private AudioRecord arec;
private AudioTrack atrack;
public void run() {
isRecording = true;
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
Log.d("buffer",String.valueOf(buffersize));
buffersize +=5000;
arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(44100);
byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
while(isRecording) {
arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
}