I have two method in my activity one is setOnClickListener()
and other is setOnLongClickListener()
For the same button which used to start audio recording.
Now i don't know how to use condition, if i use setOnClickListener()
then recording should start for 1 minute and stop automatically, and if i use setOnLongClickListener()
then recording start for 2 minute and then stop automatically.
buttonStart.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(Record_Audio.this,
"Start Recording With LongClick", Toast.LENGTH_SHORT)
.show();
enableButtons(true);
startRecording();
return true;
}
});
private View.OnClickListener btnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
Toast.makeText(Record_Audio.this, "Start Recording",
Toast.LENGTH_SHORT).show();
enableButtons(true);
startRecording();
break;
}
}
});
private void startRecording() {
displayAlertDialog();
}
private void displayAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Record_Audio.this);
alertDialog.setTitle("Would you Like to save your Recording");
alertDialog.setMessage("Enter Audio Name");
alertDialog.setIcon(R.drawable.save_icon);
final EditText editTextAudioName = new EditText(Record_Audio.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
editTextAudioName.setLayoutParams(lp);
alertDialog.setView(editTextAudioName);
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Code.audioName = editTextAudioName.getText().toString()
.trim();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
myChronometer.setBase(SystemClock.elapsedRealtime());
myChronometer.start();
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
alertDialog.show();
}
I know i have to use setMaxDuration
for particular time interval, but i don't know how to use with this two method.
Thanks in advance.
Anyhow the MediaRecorder Api is not good way to use record audio.Use the AudioRecorder API.
Let me explain
1.This is class for recording audio
public class Taukyrecorder {
}
Start and stop the record
MainActivity
If you want stop automatically after few second please create one thread.
Then call the thread using handler