Is there any way to get duration of ongoing recording
You can use a timer and an a handler to achieve it. In the example below a text view is used to display the duration in the format 00min:00sec. I use this in background service but you can use in an activity too.
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 you Start Recording:
......
this.mediaRecorder.start()
startHTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
Where you Stop Recording:
mediaRecorder.stop()
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);