0
votes

I wand to record some audio using AudioRecord. In order to initialize the AudioRecord object you must provide several arguments e.g( rate, channel, encoding) and since different combinations of arguments are supported from hardware devices, I went on and check functional apps like

Ringdroid: Audio recording done in Ringdroid

and Rehersal Assistant: Audio recording in Rehersal Assistant

As mentioned in the documentation of the AudioRecord class, the configuration that will always work is rate = 44100 and channel = CHANNEL_IN_MONO.

I am using the same arguments when initializing my AudioRecord object but I still get a runtime error saying that my object is uninitialized. Since RingDroind is working fine on my device (Nexus 5) I have used the same configuration when creating my AudioObject.

package com.example.android.visualizeaudio;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    int mSampleRate = 44100;

    Button startButton;
    boolean started = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = (Button) this.findViewById(R.id.start_button);

    }

    private void RecordAudio() {


        int minBufferSize = AudioRecord.getMinBufferSize(
                mSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        // make sure minBufferSize can contain at least 1 second of audio (16 bits sample).
        if (minBufferSize < mSampleRate * 2) {
            minBufferSize = mSampleRate * 2;
        }
        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.MIC,
                mSampleRate,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                minBufferSize
        );


        audioRecord.startRecording();

        //Do some stuff here with the recorded data

        audioRecord.stop();
        audioRecord.release();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void startRec(View view) {
        if (started) {
            started = false;
            startButton.setText("Start");

        } else {
            started = true;
            startButton.setText("Stop");
            Toast.makeText(this, "Recording started", Toast.LENGTH_LONG);

            RecordAudio();
        }
    }
}

I am attaching the object inspection during debugging in case it provides more insight enter image description here

Thank you

1

1 Answers

0
votes

Switching to SDK target version 22 made the trick. With target SDK 23 I had these errors. I don't know why but it seems that the resource I am trying to access is used by the OS.