1

I am trying to record some audio with my android app.

First I followed How do you enable a microphone input in the android emulator tutorial and set the virtual microphone uses host input to ON

Then I used some code to record audio

It enters the IF statement, so permissions are set correctly, but the max amplitude returned is always 0, so it never records any audio

My problem is that I cannot check if this is an issue with the code or the emulator

I tested the microphone on my laptop works just fine.

But I cannot test microphone in the emulator

When I try to start the Camera App, it immediately crashes with Camera keeps stopping error When I try to type something with Gboard speech to text inside of the emulator, it shows initializing then immediately turns off the speech recording.

It seems there may be something wrong with the emulator here. How can I fix it?

private AudioRecord ar = null;
private int minSize;
private boolean permitted = false;
public void start(Context main_context) {
    if (ContextCompat.checkSelfPermission(main_context, android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
        permitted = true;
        minSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize);
        ar.startRecording();
    }
}

public void stop() {
    if (ar != null && permitted) {
        ar.stop();
    }
}

public double getAmplitude() {
    short[] buffer = new short[minSize];
    ar.read(buffer, 0, minSize);
    int max = 0;
    for (short s : buffer)
    {
        if (Math.abs(s) > max)
        {
            max = Math.abs(s);
        }
    }
    return max;
}
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Mich
  • 3,188
  • 4
  • 37
  • 85

0 Answers0