如何延迟播放AudioRecord录制的音频

5
我正在实现一个应用程序,它将重复我所说的一切。 我需要做的就是将我录制的声音播放到缓冲区中,只有一秒的延迟, 这样我会听到自己说话,但是会有1秒的延迟。

这是Recorder类的run方法:

public void run()
    {           
        AudioRecord recorder = null;
        int ix = 0;
        buffers = new byte[256][160];

        try
        {
            int N = AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT);
            recorder = new AudioRecord(AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, N*10);               
            recorder.startRecording();
            Timer t = new Timer();              
            SeekBar barra = (SeekBar)findViewById(R.id.barraDelay);
            t.schedule(r = new Reproductor(), barra.getProgress());           

            while(!stopped)
            {                   
                byte[] buffer = buffers[ix++ % buffers.length];                 
                N = recorder.read(buffer,0,buffer.length);                  
            }
        }
        catch(Throwable x)
        {               
        }

        finally
        { 
            recorder.stop();
            recorder.release();
            recorder = null;                        
        }

这是我的播放器的第一次运行:

public void run() {
        reproducir = true;

        AudioTrack track = null;            
        int jx = 0;

        try
        {
            int N = AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT);               
            track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, 
                    AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);              
            track.play();
            /*
             * Loops until something outside of this thread stops it.
             * Reads the data from the recorder and writes it to the audio track for playback.
             */
            while(reproducir)
            {                   
                byte[] buffer = buffers[jx++ % buffers.length];                 
                track.write(buffer, 0, buffer.length);
            }
        }
        catch(Throwable x)
        {               
        }
        /*
         * Frees the thread's resources after the loop completes so that it can be run again
         */
        finally
        {               
            track.stop();
            track.release();
            track = null;               
        }

    }

Reproductor是一个内部类,扩展了TimerTask并实现了"run"方法。

非常感谢!


1
你可以联系 Idiotizer 的作者,它可以实现你想要达到的目标。 - Adam Schmideg
1个回答

0
至少你应该修改你的播放器下面这一行代码。
int N = AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT);               

int N = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT);

因为API要求如此(尽管常量值相同),所以必须这样做。

但这只是一个边角点。主要问题在于,您并没有真正提出解决问题的方法,而只是两种通用方法。

工作解决方案的核心是使用大小为1秒的环形缓冲区,并使用AudioRecord通过AudioTrack在写入新数据之前读取其块,两者采样率相同。

我建议在单个线程中执行此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接