You are setting mFilename in your constructor ("SpellRecord") which is called before onCreate. An activity's constructor is called the moment you create an instance of it, whereas onCreate is only to "create" the activity only after its launched.
Trying to use a string from an activity's EditText to use as a filename for a recording activity
-
09-10-2022 - |
Question
Let's say I have an activity(let's call it Activity 1). There is an editText on it which contains a string value, which has been saved. Now if I were to go to an audio recording activity, and record a sound, I would like for the sound recorded to use the string imported from Activity 1 as a filename. (e.g. if the editText word in Act 1 is "meow", then the recorded audio would be called "recmeow".mp3)
I have been able to transfer the string from Activity 1 to the recording activity (tested with toasts). However, when it comes to naming the file under the string itself, it keeps naming the file as 'recnull' instead.
I've tried a lot of things but I just can't get it to work. Can anyone please take a look and see what went wrong? Thanks in advance! This is the code for the recording activity:
public class SpellRecord extends Activity {
//declaration of a variable called namerec to contain imported string
String namerec;
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public SpellRecord() {
//here is where the filename is supposed to be where namerec contains string
//imported from Activity 1
//i have also tried declaring a string variable in here and called out to namerec
//it didn't work
//I have also tried putting String.valueOf(namerec) in place of namerec below
//didn't work too. and etc.
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/rec" + namerec + ".mp3";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//below is where i import the string from activity 1
String recN = getIntent().getStringExtra("dataN");
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
//putting the string value from activity 1 into namerec
namerec = recN;
//toast below shows that namerec will show the string value correctly
Toast.makeText(getApplicationContext(), "show "+ namerec + " show",
Toast.LENGTH_LONG).show();
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
/*View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
root.setBackgroundResource(Color.BLUE);*/
}
Solution