【问题标题】:How to play back AudioRecord with some delay如何延迟播放 AudioRecord
【发布时间】:2011-12-29 20:13:27
【问题描述】:

我正在实现一个应用程序,它将重复我所说的一切。 我需要的是播放我在缓冲区上录制的声音,只需一秒钟的延迟 这样我就可以自己听了,但延迟了 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”方法的内部类。

非常感谢!

【问题讨论】:

  • 您可以联系Idiotizer的作者,它可以实现您的目标

标签: android audio audio-recording android-audiorecord


【解决方案1】:

至少您应该更改播放器的以下行

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 的环形缓冲区和 AudioTrack 在通过 AudioRecord 将新数据写入同一块之前读取它的块,两者都在同一个样本中率。

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

【讨论】:

    猜你喜欢
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2018-05-12
    相关资源
    最近更新 更多