Limit recording time (Mediarecorder)

Asked

Viewed 57 times

1

Hello, as the title says I want to limit the recording of my code:

    private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    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;

    uploadAudio();
}

this is where I give the order to record

        mImageMic.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startRecording();
                    mRelative.setVisibility(View.GONE);
                    mTextGravando.setVisibility(View.VISIBLE);
                    mTextGravando.setText("Gravando...");
                    mCronometro.setVisibility(View.VISIBLE);
                    //Parte Importante
                    mCronometro.setBase(SystemClock.elapsedRealtime());
                    mCronometro.start();

                    return true;
                case MotionEvent.ACTION_UP:
                    stopRecording();
                    mRelative.setVisibility(View.VISIBLE);
                    mTextGravando.setVisibility(View.GONE); 
                    mCronometro.setVisibility(View.GONE);
                    //Parte Importante                       
                    mCronometro.stop();
                    return true;
            }
            return false;
        }
    });

I thought of something like mCronometro is equal to 60000 milesegundos call the stop and the upload.

1 answer

3


Just use the native method setMaxDuration of MediaRecorder when you set your object. In your case:

mRecorder.setMaxDuration(60000);

According to official documentation:

void setMaxDuration(int max_duration_ms) - the Maximum Duration (in ms) of the Recording Session. Call this after setOutFormat() but before prepare(). After Recording Reaches the specified Duration, a notification will be sent to the Mediarecorder.Oninfolistener with a "what" code of MEDIA_RECORDER_INFO_MAX_DURATION_REACHED and Recording will be stopped. Stopping Happens asynchronously, there is no Uarantee that the Recorder will have stopped by the time the Listener is notified.

https://developer.android.com/reference/android/media/MediaRecorder.html#setMaxDuration(int)

  • I had arrived at this result here, but ta accepts the answer, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.