Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android detecting real recording, MediaRecorder has delay

I would like to synchronize data from phone with elapsed time recording video. I would like to get information (in ms) about elapsed time recording video. I tried resolve it via:
1. Run myself timing.
Pseudo:

mMediaRecorder.start(); //start recording
startTime = System.currentTimeMillis(); //save start recording time
timerHandler.postDelayed(timerRunnable, 0); //for save elapsed time and timerHandler.postDelayed(this, 20); for next 20ms

but there is problem with MediaRecorder, because real recording not starts after MediaRecorder.start() immediately, but real video recording have different delay (depends on device, ...). I need "some" callback or catch event when MediaRecorder really start recording.

enter image description here Information about stop recording is not need it, because i can check real video duration via:

FileInputStream stream = new FileInputStream(storage.getVideoFile());
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(stream.getFD());
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

I tried get actual duration via MediaMetadataRetriever while recording, but it works only on closed video file.

I tried to detect via FileObserver and onEvent, but it is accurate.

If it possible, i would like to solve via Android SDK. But how?
[min SDK: API21]

Thank you.

like image 505
t0m Avatar asked Feb 13 '16 23:02

t0m


1 Answers

I know that probably you solved this problem after these 7 months but... By looking at the picture you posted you can compute the REAL VIDEO LENGTH with MediaMetadataRetriever an then, by defining "a" as the elapsed time from Mediarecorder.start() to the first acquired frame and "b" the elapsed time from the last acquired frame to Mediarecorder.stop() and assuming that a=b you can compute the start recording instant by:

startTime + a

where:

a = b = ((stopTime - startTime) - REAL VIDEO LENGTH)/2

startTime = System.nanoTime() (called right after MediaRecorder.start())

stopTime = System.nanoTime() (called right after MediaRecorder.stop())
like image 77
Alberto Lanaro Avatar answered Oct 06 '22 01:10

Alberto Lanaro