简体   繁体   English

Android:录制和收听音频

[英]Android: Record and listen audio

I try to implement a record and listen a audio from differents post, but I have a delay for 40 ms. 我尝试实现一条记录并收听来自其他帖子的音频,但是我有40毫秒的延迟。

This is my code. 这是我的代码。

public class record {
    private AudioRecord recorder = null;
    private boolean isRecording = false;
    private int SAMPLERATE = 8000;
    private int CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
    private int bufferSize = AudioRecord.getMinBufferSize(SAMPLERATE, CHANNELS,
                    AUDIO_FORMAT);
    private Thread recordingThread = null;

    public void startRecording() {
            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE,
                            CHANNELS, AUDIO_FORMAT, bufferSize);

            recorder.startRecording();
            isRecording = true;

            recordingThread = new Thread(new Runnable()

            {
                    public void run() {
                            writeAudioData();
                    }

            });
            recordingThread.start();

    }

    public void stopRecording() {
            isRecording = false;
            recorder.stop();
            recorder.release();
            recorder = null;
            recordingThread = null;
    }

    private void writeAudioData() {

            byte data[] = new byte[bufferSize];

            int buff = 8000;
            int minBufferSize = AudioTrack.getMinBufferSize(buff,
                            AudioFormat.CHANNEL_CONFIGURATION_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

            Log.i("AUDIO", "BufferSize:" + minBufferSize);

            AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, buff,
                            AudioFormat.CHANNEL_CONFIGURATION_MONO,
                            AudioFormat.ENCODING_PCM_16BIT, minBufferSize,
                            AudioTrack.MODE_STREAM);

            at.play();

            while (isRecording) {
                    recorder.read(data, 0, bufferSize);
                    at.write(data, 0, bufferSize);
            }
            at.stop();
            at.release();
    }

} }

How can I reduce the delay?. 如何减少延迟? I try reducing the buffer, but the problem still continue. 我尝试减少缓冲区,但问题仍然存在。

Depending on the measurement object and method, there could be tens of milliseconds delay or hundreds milliseconds delay in Android audio system. 根据测量对象和方法的不同,Android音频系统可能会有数十毫秒的延迟或数百毫秒的延迟。

If measuring the delay between the playing and the recording, 40ms delay is a normal value, actually I see 30~50ms delay value in my this test on Android 4.4.3: 如果测量播放和录制之间的延迟,则40ms延迟是一个正常值,实际上我在Android 4.4.3的此测试中看到30〜50ms延迟值:

https://github.com/garyyu/AudioTracker-Test https://github.com/garyyu/AudioTracker-Test

Also, if you check your printed "minBufferSize" value, you will find this minimum buffer size is 40ms (on my Nexus7 2013). 另外,如果您检查打印的“ minBufferSize”值,则会发现此最小缓冲区大小为40ms(在我的Nexus7 2013上)。 And there's frame delay when you play, we can't expect the immediate playing when calling "at.write()", I guess internally there's another buffer to store your "writing" data. 而且播放时会有帧延迟,我们不能指望在调用“ at.write()”时立即播放,我想内部还存在另一个缓冲区来存储“写入”数据。 And same in the recording side. 在录音方面也一样。 So there're much more delay than 40ms if measuring from the playing start function call to the real sound output. 因此,如果从播放开始功能调用到真实声音输出进行测量,则比40ms的延迟要多得多。
If need more suggestion, you need describe the detail about how do you measure your delay. 如果需要更多建议,则需要描述有关如何衡量延迟的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM