Reputation: 3031
I have a recorder class to record voice:
public class MeeRecorder {
private static String mpathName = null;
private MediaRecorder mRecorder = null;
private String pName = "";
private String fileName = "";
public static final String TEST_PATH = "/test/";
public MeeRecorder() {
}
public void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
createPath();
mpathName = Environment.getExternalStorageDirectory().getAbsolutePath();
mpathName += TEST_PATH+"/"+getFileName()+VoiceRecorder.FILE_EXT;
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mpathName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("DEBUG", "prepare() failed");
}
mRecorder.start();
}
private void createPath(){
File recordersDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+TEST_PATH);
recordersDirectory.mkdirs();
}
public void stopRecording() {
if(mRecorder!=null){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
public String getmPathName() {
return mpathName;
}
public void setmPathName(String mPathName) {
MeasureRecorder.mpathName = mPathName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
I create object this class in my activity and from activity record voice. Now I have a problem, because when I change orientation My recording was stopped. I can't use configChanges orientation because I change layout when orientation changed. How can I save recording in change orientation?
Upvotes: 1
Views: 359
Reputation: 16043
Perhaps a better approach would be to use a IntentService
and do the recording there.
Having your recording logic in the service will simplify the things, because the service won't be affected by the orientation changes as it runs in background decoupled from the activity life cycle.
Another option would be to use Fragments. Fragments have the ability to retain their instance variables just by calling setRetainInstance(true)
, for example in onCreate()
method of the fragment.
Lastly, you could use the onRetainNonConfigurationInstance()
to save a plain object when a orientation change occurs. But this method is deprecated and the use of fragments is preferred instead of it.
Upvotes: 2
Reputation: 6096
there are two solution
1. fix your orientation to landscape or portrait
android:orientation="vertical"
or
android:orientation="horizontal"
2. trigger a service from activity to record the audio
Upvotes: 1