Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Mediarecorder recorded video duration different from expected duration

I recorded a video using android Mediarecorder.

(Main Problem: I need to know the exact startTime[System time] and endTime[System time] of the video and the [endTime - startTime] must match the duration of the actual video)

MediaRecorder.start
startTime = System.currentTimeMillis()

then on stop

MediaRecorder.stop
endTime = System.currentTimeMillis()

I am expecting the video to have this duration

expected_duration = (endTime - startTime)

However, the

expected_duration is always more than the actual duration of the video.

My suspicion is that MediaRecorder.start is slow, it took some time before it actually started writing the frames into a video.

So now, is there anyway to get notified when the MediaRecorder started writing the first frame into a video? or is there any way I can figure out the exact System startTime of when video actually started recording.

thanks for reading, and appreciate any comments, opinions or suggestions. ^^

like image 885
xiaowoo Avatar asked Feb 05 '14 19:02

xiaowoo


1 Answers

The best way I found to get real start time (and still I'm not sure it's accurate enough) is to find the duration and then subtract it from the endTime like that:

MediaRecorder.stop
endTime = System.currentTimeMillis()
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
retriever.setDataSource(this, Uri.fromFile(file));
String time = 
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long movieDurationInMillis = Long.parseLong(time );
    long startCaptureTimeMillis = endTime - movieDurationInMillis;
like image 106
Gilad Levinson Avatar answered Sep 30 '22 12:09

Gilad Levinson