Android的AudioRecord类 - 工艺直播麦克风音频迅速,成立了回调函数麦克风、回调、函数、音频

由网友(心如荒岛)分享简介:我想记录从麦克风音频和访问它的播放在接近实时。我不确定如何使用Android AudioRecord类记录一些麦克风的音频和快速访问它。 I want to record audio from the mic and access it for possible playback in near real-time....

我想记录从麦克风音频和访问它的播放在接近实时。我不确定如何使用Android AudioRecord类记录一些麦克风的音频和快速访问它。

I want to record audio from the mic and access it for possible playback in near real-time. I am unsure of how to use the Android AudioRecord class to record some mic audio and quickly access it.

对于AudioRecord类,在官方网站表示,该应用程序选举在时间上AudioRecord对象和缓冲区的大小,填充之前决定了录制的时间长度上运行的未读数据。后来它表明一个更大的缓冲区,应经常使用轮询时少。他们从来没有真正表现出code的例子。

For the AudioRecord class, the official site says 'the app polls the AudioRecord object in time', and 'the size of the buffer being filled determines the time-length of the recording before over-running unread data'. Later it's suggested that a larger buffer should be used when polling less frequently. They never actually show an example in code.

我已经看到了一本书,一个例子使用AudioRecord类连续读取缓冲区新鲜填充直播麦克风音频,然后该应用程序将数据写入到SD文件。伪code看起来像 -

One example I've seen in a book uses the AudioRecord class to continuously read a buffer freshly populated with live mic audio, and then the app writes this data to an SD file. The pseudo-code looks something like -

set up AudioRecord object with buffer size and recording format info
set up a file and an output stream
myAudioRecord.startRecording();
while(isRecording)
{
    // myBuffer is being filled with fresh audio
    read audio data into myBuffer
    send contents of myBuffer to SD file
}
myAudioRecord.stop();

这怎么code同步其读数,记录的速度尚不清楚 - 是布尔isRecording测序和关闭正确别处?看来这code既可以读出过于频繁或过于频繁,时间长短的阅读和写作需要。

How this code synchronizes its reading with the rate of recording is unclear - is the boolean "isRecording" sequenced on and off properly elsewhere? It seems this code could either read too frequently or too infrequently, depending on how long the reading and writing takes.

该网站文档也称AudioRecord类具有被定义为接口名为OnRecordPositionUpdateListener的嵌套类。这些信息表明,不知何故,你指定的记录的进步,你的事件处理程序的名称被通知需要的时间,以及呼叫会自动发到您的事件处理程序在指定的频率。我认为,结构,伪code会是这样的 -

The site doc also says the AudioRecord class has a nested class named OnRecordPositionUpdateListener which is defined as an interface. The information suggests that somehow, you specify the period you want for being notified of the progress of the recording, and the name of your event handler, and a call is automatically made to your event handler at the specified frequency. I think the structure, in pseudo-code would be something like -

set target of period update message = myListener
set period to be about every 250 ms
other code

myListener()
{
    if(record button was recently tapped)
        handle message that another 250 ms of fresh audio is available
        ie, read it and send it somewhere
)

我需要找一些特定的code,让我捕捉和处理麦克风音频小于500毫秒的延迟。 Android提供了另一个类称为MediaRecorder,但它不支持流,我可能要流媒体直播麦克风音频通过Wi-Fi网络中的近实时的。我在哪里可以找到一些具体的例子吗?

I need to find some specific code which allows me to capture and process mic audio with a delay of less than about 500 ms. Android offers another class called MediaRecorder, but it doesn't support streaming, and I may want to stream live mic audio over a Wi-Fi network in near real-time. Where can I find some specific examples?

推荐答案

试验批次的通知和一堆我对这个code结算等技术后:

After experimenting lots with the notifications and a bunch of other techniques I settled on this code:

private class AudioIn extends Thread { 
     private boolean stopped    = false;

     private AudioIn() { 

             start();
          }

     @Override
     public void run() { 
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            AudioRecord recorder = null;
            short[][]   buffers  = new short[256][160];
            int         ix       = 0;

            try { // ... initialise

                  int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);

                   recorder = new AudioRecord(AudioSource.MIC,
                                              8000,
                                              AudioFormat.CHANNEL_IN_MONO,
                                              AudioFormat.ENCODING_PCM_16BIT,
                                              N*10);

                   recorder.startRecording();

                   // ... loop

                   while(!stopped) { 
                      short[] buffer = buffers[ix++ % buffers.length];

                      N = recorder.read(buffer,0,buffer.length);
                      //process is what you will do with the data...not defined here
                      process(buffer);
                  }
             } catch(Throwable x) { 
               Log.w(TAG,"Error reading voice audio",x);
             } finally { 
               close();
             }
         }

      private void close() { 
          stopped = true;
        }

    }

到目前为止,它的工作pretty的强劲上半打Android手机,我试了一下。

So far it's working pretty robustly on the half a dozen Android phones I've tried it on.

阅读全文

相关推荐

最新文章