Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code you need to use the OnRecordPositionUpdateListener and Notification Period. </p> <p>I noticed that in practice it does not send the notification consistently at the same exact time, I want, but it is close enough.</p> <p>About <code>detectAfterEvery</code>:</p> <p>The size of detectEvery needs to be large enough to hold just the amount of data you want. So for this example, we have a sample rate of 44100 Hz, that means we want 44100 samples per second. By setting the <code>setPositionNotificationPeriod</code> to be 44100, the code tells Android to callback after it has recorded 44100 samples, which is about every 1 second.</p> <p>The complete code is <a href="https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/audio/record/AudioClipRecorder.java">here</a>:</p> <pre><code> final int sampleRate = 44100; int bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); //aim for 1 second int detectAfterEvery = (int)((float)sampleRate * 1.0f); if (detectAfterEvery &gt; bufferSize) { Log.w(TAG, "Increasing buffer to hold enough samples " + detectAfterEvery + " was: " + bufferSize); bufferSize = detectAfterEvery; } recorder = new AudioRecord(AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); recorder.setPositionNotificationPeriod(detectAfterEvery); final short[] audioData = new short[bufferSize]; final int finalBufferSize = bufferSize; OnRecordPositionUpdateListener positionUpdater = new OnRecordPositionUpdateListener() { @Override public void onPeriodicNotification(AudioRecord recorder) { Date d = new Date(); //it should be every 1 second, but it is actually, "about every 1 second" //like 1073, 919, 1001, 1185, 1204 milliseconds of time. Log.d(TAG, "periodic notification " + d.toLocaleString() + " mili " + d.getTime()); recorder.read(audioData, 0, finalBufferSize); //do something amazing with audio data } @Override public void onMarkerReached(AudioRecord recorder) { Log.d(TAG, "marker reached"); } }; recorder.setRecordPositionUpdateListener(positionUpdater); Log.d(TAG, "start recording, bufferSize: " + bufferSize); recorder.startRecording(); //remember to still have a read loop otherwise the listener won't trigger while (continueRecording) { recorder.read(audioData, 0, bufferSize); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload