1

i`m new to android development and i wanted to make an audio recorder, when i want to access the method for starting of recording from my main activity it always gives an error. Below is my code. I hope you can help me:

This is the mainActivity:

public class MainActivity extends AppCompatActivity {

Boolean isRecording = false;
Record record;

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

    record = new Record();
}

public void recordAudio(View view){
    if(!isRecording)
    {
        isRecording = true;
        record.startRecording();
    }

    else{
        isRecording = false;
        record.stopRecording();
    }
}

Below is the subclass:

public class Record extends MainActivity {

public void startRecording() {
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, getBufferSize());

    int i = recorder.getState();
    if (i == 1)
        recorder.startRecording();

    isRecording = true;

    recordingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");

    recordingThread.start();
    buttonRecord.setText(R.string.button_stop_record);

}

Thanks for your help!

Here is the exact error code:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com, PID: 31778
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6199)
                  at android.widget.TextView.performClick(TextView.java:11090)
                  at android.view.View$PerformClick.run(View.java:23647)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6682)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6199) 
                  at android.widget.TextView.performClick(TextView.java:11090) 
                  at android.view.View$PerformClick.run(View.java:23647) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6682) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
               Caused by: java.lang.IllegalArgumentException: Invalid audio buffer size.
                  at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:751)
                  at android.media.AudioRecord.<init>(AudioRecord.java:385)
                  at android.media.AudioRecord.<init>(AudioRecord.java:289)
                  at com.Record.startRecording(Record.java:63)
                  at com.MainActivity.recordAudio(MainActivity.java:35)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6199) 
                  at android.widget.TextView.performClick(TextView.java:11090) 
                  at android.view.View$PerformClick.run(View.java:23647) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6682) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Florian
  • 33
  • 1
  • 8

2 Answers2

1

The error says com.Record.startRecording(Record.java:63) is the cause of: java.lang.IllegalArgumentException: Invalid audio buffer size.

Please call AudioRecord.getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) to make sure your buffer size is big enough before you instantiate a new AudioRecord. Just like it says in the documentation.

Please note that you should also check if your AudioRecord instance was initialized correctly by calling getState() (and checking that it returns STATE_INITIALIZED).

KompjoeFriek
  • 3,572
  • 1
  • 22
  • 35
  • I also tried it with different classes where I didn't use the audio recorder and there it also didn't work, when I had everything in the main activity everything worked – Florian Mar 24 '18 at 13:34
1

In Android Activity is usually stared by the user code but the object creation is not the responsibility of the user, rather it is the Android framework which does this.

Your case has Record extends MainActivity and MainActivity being an Activity makes Record also an Activity. So you need to start it either make it the launcher main activity in manifest file or use startActivity() (or startActivityForResult()).

The code record = new Record(); Here you create the instance of Record yourself that too in the parent class MainActivity. This is not a very good idea from both programming in Android and Java Object oriented point of view. (Hence you can but should not choose to do so)

Refer one answer from another post https://stackoverflow.com/a/14956056/504133

I suggest you to make simple Android application with two or three activities, with each having simple UI layout. You can learn there and then apply the concepts for more complex application as AudioRecorder.

nits.kk
  • 5,204
  • 4
  • 33
  • 55