问题
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.
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.
回答1:
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())
来源:https://stackoverflow.com/questions/35386663/android-detecting-real-recording-mediarecorder-has-delay