Limit recording time (MediaRecorder)

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 write

        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 equals 60000 milliseconds to call stop and upload .

    
asked by anonymous 16.07.2017 / 23:02

1 answer

3

Just use the native method setMaxDuration of MediaRecorder when defining 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   specified duration, 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 guarantee that   the recorder will have stopped by the time the listener is notified.

link

    
17.07.2017 / 00:48