Hi,

Last I checked the AudioRecord read is a blocking operation, you don't
need to have to "poll" for data..just set up a second thread that
constantly reads. It will read as fast as possible. The amount of
"blocking" in the read is the size of the buffer chosen....


-niko

On Feb 15, 7:35 am, Wanderer <[email protected]> wrote:
> Hello Android Developers!
>
> My Android Java Application needs to record audio data into the RAM
> and process it. This is why I use the class "AudioRecord" and not the
> "MediaRecorder" (records only to file).
>
> Till now, I used a busy loop polling with "read()" for the audio data.
> this has been working so far, but it peggs the CPU too much. Between
> two polls, I put the thread to sleep to avoid 100% CPU usage. However,
> this is not really a clean solution, since the time of the sleep is
> not guaranteed and you must subtract a security time in order not to
> loose audio snippets. This is not CPU optimal. I need as many free CPU
> cycles as possible for a parallel running thread.
>
> Now I implemented the recording using the
> "OnRecordPositionUpdateListener". This looks very promising and the
> right way to do it according the SDK Docs. Everything seems to work
> (opening the audio device, read()ing the data etc.) but the Listner is
> never called.
>
> Does anybody know why?
>
> Info: I am working with a real Device, not under the Emulator. The
> Recording using a Busy Loop basically works (however not satifiying).
> Only the Callback Listener is never called.
>
> Here is a snippet from my Sourcecode:
>
> ______________________________________________ snip!
> public class myApplication extends Activity {
>
>   /* audio recording */
>   private static final int AUDIO_SAMPLE_FREQ = 16000;
>   private static final int AUDIO_BUFFER_BYTESIZE = AUDIO_SAMPLE_FREQ *
> 2 * 3; // = 3000ms
>   private static final int AUDIO_BUFFER_SAMPLEREAD_SIZE =
> AUDIO_SAMPLE_FREQ  / 10 * 2; // = 200ms
>
>   private short[] mAudioBuffer = null; // audio buffer
>   private int mSamplesRead; // how many samples are recently read
>   private AudioRecord mAudioRecorder; // Audio Recorder
>
>   ...
>
>   private OnRecordPositionUpdateListener mRecordListener = new
> OnRecordPositionUpdateListener() {
>
>     public void onPeriodicNotification(AudioRecord recorder) {
>       mSamplesRead = recorder.read(mAudioBuffer, 0,
> AUDIO_BUFFER_SAMPLEREAD_SIZE);
>       if (mSamplesRead > 0) {
>
>         // do something here...
>
>       }
>     }
>
>     public void onMarkerReached(AudioRecord recorder) {
>       Error("What? Hu!? Where am I?");
>     }
>   };
>
>   ...
>
>   public void onCreate(Bundle savedInstanceState) {
>
>   try {
>       mAudioRecorder = new AudioRecord(
>           android.media.MediaRecorder.AudioSource.MIC,
>           AUDIO_SAMPLE_FREQ,
>           AudioFormat.CHANNEL_CONFIGURATION_MONO,
>           AudioFormat.ENCODING_PCM_16BIT,
>           AUDIO_BUFFER_BYTESIZE);
>     } catch (Exception e) {
>       Error("Unable to init audio recording!");
>     }
>
>     mAudioBuffer = new short[AUDIO_BUFFER_SAMPLEREAD_SIZE];
>
> mAudioRecorder.setPositionNotificationPeriod(AUDIO_BUFFER_SAMPLEREAD_SIZE);
>     mAudioRecorder.setRecordPositionUpdateListener(mRecordListener);
>     mAudioRecorder.startRecording();
>
>     /* test if I can read anything at all... (and yes, this here
> works!) */
>     mSamplesRead = mAudioRecorder.read(mAudioBuffer, 0,
> AUDIO_BUFFER_SAMPLEREAD_SIZE);
>
>   }
>
> }
>
> ______________________________________________ snip!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to