录制回耳机的音频有延迟

时间:2020-03-06 07:05:21

标签: android mediarecorder audiotrack

我想构建一个像starMaker这样的应用程序。这是我的代码。我正在启动执行以下任务的异步任务

  1. 记录音频
  2. 另存为.Wav文件
  3. 将音频添加回耳机。

问题

回到我的耳机时会有延迟。如何消除这种延迟?有人可以帮我消除此延迟吗?

protected Object[] doInBackground(File... files) {
        AudioRecord audioRecord = null;
        FileOutputStream wavOut = null;
        AudioTrack aud;
        long startTime = 0;
        long endTime = 0;

        try {
            audioRecord = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_MASK, ENCODING, BUFFER_SIZE);
            wavOut = new FileOutputStream(files[0]);

            aud = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, ENCODING, BUFFER_SIZE, AudioTrack.MODE_STREAM);
            aud.play();
            // Write out the wav file header
            writeWavHeader(wavOut, CHANNEL_MASK, SAMPLE_RATE, ENCODING);

            // Avoiding loop allocations
            byte[] buffer = new byte[BUFFER_SIZE];
            boolean run = true;
            int read;
            long total = 0;

            startTime = SystemClock.elapsedRealtime();
            audioRecord.startRecording();
            while (run && !isCancelled()) {
                read = audioRecord.read(buffer, 0, buffer.length);

                // WAVs cannot be > 4 GB due to the use of 32 bit unsigned integers.
                if (total + read > 4294967295L) {
                    // Write as many bytes as we can before hitting the max size
                    for (int i = 0; i < read && total <= 4294967295L; i++, total++) {
                        aud.write(buffer, 0, read);
                        wavOut.write(buffer[i]);
                    }
                    run = false;
                } else {
                    // Write out the entire read buffer
                    aud.write(buffer, 0, read);
                    wavOut.write(buffer, 0, read);
                    total += read;
                }
            }
        } catch (IOException ex) {
            return new Object[]{ex};
        } finally {
            if (audioRecord != null) {
                try {
                    if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
                        audioRecord.stop();
                        endTime = SystemClock.elapsedRealtime();
                    }
                } catch (IllegalStateException ex) {
                    //
                }
                if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
                    audioRecord.release();
                }
            }
            if (wavOut != null) {
                try {
                    wavOut.close();
                } catch (IOException ex) {
                    //
                }
            }
        }

        try {
            // This is not put in the try/catch/finally above since it needs to run
            // after we close the FileOutputStream
            updateWavHeader(files[0]);
        } catch (IOException ex) {
            return new Object[]{ex};
        }

        return new Object[]{files[0].length(), endTime - startTime};
    }

我在某处读到,由于最初添加了不需要的字节,因此增加了延迟,因此在写入时会增加延迟

0 个答案:

没有答案