13 package ai.picovoice.porcupine;
16 import android.content.Context;
17 import android.media.AudioFormat;
18 import android.media.AudioRecord;
19 import android.media.MediaRecorder;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Process;
23 import android.util.Log;
25 import java.util.concurrent.Callable;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.atomic.AtomicBoolean;
62 public void delete() {
83 }
catch (InterruptedException e) {
136 this.sensitivities =
new float[]{sensitivity};
167 private final AtomicBoolean
started =
new AtomicBoolean(
false);
168 private final AtomicBoolean
stop =
new AtomicBoolean(
false);
169 private final AtomicBoolean
stopped =
new AtomicBoolean(
false);
181 Executors.newSingleThreadExecutor().submit(
new Callable<Void>() {
184 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
191 void stop() throws InterruptedException {
208 final int minBufferSize = AudioRecord.getMinBufferSize(
210 AudioFormat.CHANNEL_IN_MONO,
211 AudioFormat.ENCODING_PCM_16BIT);
214 AudioRecord audioRecord =
null;
219 audioRecord =
new AudioRecord(
220 MediaRecorder.AudioSource.MIC,
222 AudioFormat.CHANNEL_IN_MONO,
223 AudioFormat.ENCODING_PCM_16BIT,
225 audioRecord.startRecording();
227 while (!
stop.get()) {
228 if (audioRecord.read(buffer, 0, buffer.length) == buffer.length) {
231 if (keywordIndex >= 0) {
248 Log.e(
"PorcupineManager", e.toString());
255 }
catch (IllegalArgumentException | IllegalStateException e) {
264 Log.e(
"PorcupineManager", e.toString());
267 if (audioRecord !=
null) {
268 audioRecord.release();