0

I am currently developing a Voice Recorder app for Android. I am trying to access a few methods in my MainActivity from my Settings activity, in order to change some settings for my MediaRecorder.

I have the method below, which sets up the Audio Settings for the recording, in my MainActivity.

// set up all audio settings
private void setAudioSettings() {
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setAudioSamplingRate(44100);
    mediaRecorder.setAudioEncodingBitRate(96000);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}

In my Settings activity, I have a standard preferences screen that I would like to show options to change the audio codec, sampling rate, etc. of the media recorder in MainActivity.

How can I access the setAudioSettings method from MainActivity here in order to do so?

If you need to see more code or screenshots, please let me know.

Bashir
  • 2,057
  • 5
  • 19
  • 44
olsxn
  • 27
  • 7
  • By Only making that method public you can access it, In advance if you want to access it with out creating a Object then you can make that method as static also public void setAudioSettings() {} – Thakkar Darshan Mar 16 '20 at 11:45
  • Since `mediaRecorder` most likely is a field variable (tied to an instance of your activity) making the method static would not work so you'll have to access the activity instance in some way or in turn make the field variable static which has its own issues.. –  Mar 16 '20 at 11:47
  • Use [`startActivityForResult`](https://developer.android.com/reference/android/app/Activity.html#StartingActivities) and efficient way for such operations – ॐ Rakesh Kumar Mar 16 '20 at 11:54

4 Answers4

1

Make that method as static so you can call without creating the class object

public static void yourMethod(){
//Write your code here
}

And call your method like this way:

MainActivity.yourMethod();
Ali
  • 3,346
  • 4
  • 21
  • 56
1

The short answer is you should not use the functions of your one activity into another activity.

For your case, I would suggest you to have a singleton object or shared preference to store your data of settings screen. Then in onStart of MainActivity, read the singleton object or shared preference and call #setAudioSettings method accordingly.

Ashok
  • 2,411
  • 1
  • 13
  • 23
0

save setting i.e values in shared preferences and then get from preferences in Main Activity.

0

You can make your method static by:

  public static void setAudioSettings() {
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setAudioSamplingRate(44100);
    mediaRecorder.setAudioEncodingBitRate(96000);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
}

But to do that mediaRecorder needs to be static also.

Later you can call this method from any activity by:

MainActivity.setAudioSettings();

You can learn more about static keyword for example here.

But, I am not sure that use of static method is the best solution for exactly your problem, maybe will be better to set SharedPreferences in your SettingActivity and later in onResume() of your MainActivity call setAudioSettings() method and get there values from SharedPreferences?

Panicum
  • 794
  • 1
  • 9
  • 32