如何控制声音延迟?

时间:2018-02-28 18:21:53

标签: java android android-audiomanager android-audiorecord

我尝试使用此代码接收声音并同时广播 有一些延迟,这不会打扰我

但我想控制Melisecond的延迟吗?我怎么能这样做?

这是我的代码:

int num;
short[] lin;
AudioManager am = null;
AudioRecord record =null;
AudioTrack track =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();
            recordAndPlay();

}

private void init() {
    int min = AudioRecord.getMinBufferSize(44100  , AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 44100  , AudioFormat.CHANNEL_IN_VOICE_UPLINK,
            AudioFormat.CHANNEL_IN_VOICE_UPLINK, min);

    int maxJitter = AudioTrack.getMinBufferSize(44100  , AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
    track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 44100  , AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
}

private void recordAndPlay() {
   lin = new short[1024];
   num = 0;
    am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    am.setMode(AudioManager.MODE_IN_CALL);
    record.startRecording();

    track.play();
    while (true) {



        track.write(lin, 0, num);
    }
}
}

0 个答案:

没有答案