1

I want to create app in which when i click one time on button, audio recording should start for particular time (say 20 seconds) and then it should automatically stop the recording and it ask the user to save audio with edit text to enter name of audio.

Now the problem is what i want to implement is when user double click on same button, audio recording should start for 40 seconds and then same thing it should automatically stop after 40 seconds and then it ask the user's to enter the name of audio.

So i am confused in how to implement that when user click one time then recording start for 20 seconds and if it double clicked recording start for 40 seconds.

If any one have any idea or sample application or anything then please help me. I have searched a lot but not getting any solution from any where.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84

4 Answers4

0

You can use this method..

public void recordAudio(String fileName) {
        final MediaRecorder recorder = new MediaRecorder();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile("/sdcard/sound/" + fileName);
        try {
          recorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }

        final ProgressDialog mProgressDialog = new ProgressDialog(MyActivity.this);
        mProgressDialog.setTitle(R.string.lbl_recording);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mProgressDialog.dismiss();
            recorder.stop();
            recorder.release();
            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            public void onCancel(DialogInterface p1) {
                recorder.stop();
                recorder.release();
            }
        });
        recorder.start();
        mProgressDialog.show();
    }

Check this link for your reference 1) http://www.devlper.com/2010/12/android-audio-recording-part-1/

2) http://www.devlper.com/2010/12/android-audio-recording-part-2/ Enjoy..!!!

Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
  • if This code start recording for 20 seconds in one click and in double click it will start recording for 40 seconds. So where to define timing in this code. – InnocentKiller Nov 29 '13 at 06:51
  • you should add this line for the time recorder.setMaxDuration(20000); @user2846106 – Karthi Nov 29 '13 at 06:54
0

You can do that in two way. One you can check double click on the button and another is check long item click :

To use OnItemLongClickListener:

  • Long Press is a recommeded interaction in the UI Guidelines, double touch is not.
  • It's what users expect; a user might not find a double touch action as they won't go looking for it
  • It's already handled in the API.
  • Implementing Double Touch will affect handling of single touches, because you'll have to wait to see if every single touch turns into a double touch before you can process it.

Sample to use Double click listener:

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
private long lastPressTime;

private boolean mHasDoubleClicked = false;

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {    

        // Get current time in nano seconds.
        long pressTime = System.currentTimeMillis();


        // If double click...
        if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
            Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show();
            mHasDoubleClicked = true;
        }
        else {     // If not double click....
            mHasDoubleClicked = false;
            Handler myHandler = new Handler() {
                 public void handleMessage(Message m) {
                      if (!mHasDoubleClicked) {
                            Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show();
                      }
                 }
            };
            Message m = new Message();
            myHandler.sendMessageDelayed(m,DOUBLE_PRESS_INTERVAL);
        }
        // record the last time the menu button was pressed.
        lastPressTime = pressTime;      
        return true;
    }
Pradip
  • 3,189
  • 3
  • 22
  • 27
0

Hope Bellow code to save audio

mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    if (Build.VERSION.SDK_INT >= 10) {
        mRecorder.setAudioSamplingRate(44100);
        mRecorder.setAudioEncodingBitRate(96000);
        mRecorder.setMaxDuration(24000);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    } else {
        mRecorder.setAudioSamplingRate(8000);
        mRecorder.setAudioEncodingBitRate(12200);
        mRecorder.setMaxDuration(24000);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    }
    String path = mFileName + String.valueOf(count) + ".amr";
    mRecorder.setOutputFile(path);
    try {
        mRecorder.prepare();
        mRecorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

in the bellow snippet to change the time . you should give time in millis

    mRecorder.setMaxDuration(24000); 
Karthi
  • 756
  • 4
  • 16
  • Thanks Karthi for quick reply but This code is just to save audio file right, but i also want that user's click event that if it clicked once means audio recording should start for 20 seconds and if it double clicked then it should start for 40 seconds, any idea how to implement this. – InnocentKiller Nov 29 '13 at 07:01
  • yes.. you should handle the click event if single click means set duration 20s and it double click means set max duration 40s ...simple – Karthi Nov 29 '13 at 07:03
  • But how to handle single click and double click and what condition i need to give and where??? – InnocentKiller Nov 29 '13 at 07:06
  • http://stackoverflow.com/questions/9971224/how-to-implement-double-click-in-android see this. hope it is helpful – Karthi Nov 29 '13 at 07:07
0

http://androidcodeexamples.blogspot.in/2012/06/voice-recording-in-android.html

please follow this link.

Made Changes in the Code : For Button start click event, define boolean variable

change boolean variable value on StartButton click event.

if boolean variable is true, set recordTime 20s otherwise recordTime 40s and start recording.

When you again start the recording, please stop the recording and reset the recorder.

Hope this can be useful.

Karan Maru
  • 991
  • 6
  • 17