如何播放AudioRecord有一些延迟

时间:2011-11-15 19:44:58

标签: android audio audio-recording android-audiorecord

我正在实施一个应用程序,它将重复我告诉它的所有内容。 我需要的是播放我在缓冲区录制的声音,只是延迟了一秒钟 所以我会自己听,但延迟1秒

这是我的Recorder类的运行方法

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 个答案:

答案 0 :(得分: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要求(尽管常量值相同)。

但这只是一个边缘点。重点是你并没有真正提出解决问题的方法,只有两种通用方法。

工作解决方案的核心是您使用大小为1s且AudioTrack的环形缓冲区在通过AudioRecord将新数据写入同一块之前读取其块。以相同的采样率。

我建议在单个帖子中做到这一点。