Is there a way to get the duration of ongoing recovery

I am trying to record a video and want to show seconds of recording.

How can I do it?

public void startRecording (View v) {

flipCamera.setVisibility(View.GONE); captureImage.setVisibility(View.GONE); String deviceMan = android.os.Build.MANUFACTURER; this.mediaRecorder = new MediaRecorder(); this.mediaRecorder.setCamera(this.camera); camera.unlock(); this.mediaRecorder.setCamera(camera); this.mediaRecorder.setOrientationHint(90); this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); CamcorderProfile camcorderProfile_HQ = CamcorderProfile .get(CamcorderProfile.QUALITY_480P); this.mediaRecorder.setProfile(camcorderProfile_HQ); this.mediaRecorder.setOutputFile(this.initFile().getAbsolutePath()); this.mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec. this.mediaRecorder.setMaxFileSize(5000000); this.mediaRecorder.setPreviewDisplay(this.cameraPreview.getHolder() .getSurface()); this.mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); try { this.mediaRecorder.prepare(); // start the actual recording // throws IllegalStateException if not prepared this.mediaRecorder.start(); Toast.makeText(this, R.string.recording, Toast.LENGTH_SHORT).show(); this.toggleButtons(true); } catch (Exception e) { Log.wtf(TAG, "Failed to prepare MediaRecorder", e); Toast.makeText(this, R.string.cannot_record, Toast.LENGTH_SHORT) .show(); this.releaseMediaRecorder(); } } 

I am brand new to android, so if someone can help.

+8
source share
2 answers

You can use a timer and a handler to achieve it. In the example below, a textual representation is used to display the duration in the format 00min: 00sec. I use this in the background, but you can also use it in action.

 public TextView timerTextView; private long startHTime = 0L; private Handler customHandler = new Handler(); long timeInMilliseconds = 0L; long timeSwapBuff = 0L; long updatedTime = 0L; private Runnable updateTimerThread = new Runnable() { public void run() { timeInMilliseconds = SystemClock.uptimeMillis() - startHTime; updatedTime = timeSwapBuff + timeInMilliseconds; int secs = (int) (updatedTime / 1000); int mins = secs / 60; secs = secs % 60; if (timerTextView != null) timerTextView.setText("" + String.format("%02d", mins) + ":" + String.format("%02d", secs)); customHandler.postDelayed(this, 0); } }; 

Where do you start recording:

  ...... this.mediaRecorder.start() startHTime = SystemClock.uptimeMillis(); customHandler.postDelayed(updateTimerThread, 0); 

Where do you stop recording:

  mediaRecorder.stop() timeSwapBuff += timeInMilliseconds; customHandler.removeCallbacks(updateTimerThread); 
+11
source

You can use Rxjava:

Declare the observed:

  mTimeLoader = Observable.defer(() -> Observable.interval(0, AMPLITUDE_UPDATE_INTERVAL, TimeUnit.MILLISECONDS) .timeInterval()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); 

Activation:

  mTimeLoaderDisposable = mTimeLoader.subscribe(longTimed -> { mTotalMsRecorded = (int) (longTimed.value() * AMPLITUDE_UPDATE_INTERVAL);} 

When you're done, just recycle.

 mTimeLoaderDisposable.dispose() 
0
source

Source: https://habr.com/ru/post/970828/


All Articles