问题描述
我想将手机中的数据与录制视频所花费的时间进行同步.我想获取有关录制视频所用时间的信息(以毫秒为单位).我尝试通过以下方式解决它:
1.为自己计时.
伪:
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
但是MediaRecorder存在问题,因为真实记录不会在MediaRecorder.start()之后立即开始,但是真实视频记录具有不同的延迟(取决于设备,...).MediaRecorder真正开始记录时,我需要一些"回调或catch事件.
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);
我尝试在录制时通过MediaMetadataRetriever获取实际持续时间,但是它仅适用于关闭的视频文件.
I tried get actual duration via MediaMetadataRetriever while recording, but it works only on closed video file.
我试图通过FileObserver和onEvent进行检测,但这是准确的.
I tried to detect via FileObserver and onEvent, but it is accurate.
如果可能的话,我想通过Android SDK解决.但是如何?
[min SDK:API21]
If it possible, i would like to solve via Android SDK. But how?
[min SDK: API21]
谢谢.
推荐答案
我知道您可能在这7个月后解决了这个问题,但是...通过查看您发布的图片,您可以使用MediaMetadataRetriever和,通过将"a"
定义为从 Mediarecorder.start()
到第一个获取的帧,以及"b"
从最后一个获取的帧到 Mediarecorder.stop()
的时间假设a = b,您可以通过以下方式计算开始录制的瞬间:
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 fromMediarecorder.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
其中:
a = b = ((stopTime - startTime) - REAL VIDEO LENGTH)/2
startTime = System.nanoTime() (called right after MediaRecorder.start())
stopTime = System.nanoTime() (called right after MediaRecorder.stop())
这篇关于Android检测到实际录制,MediaRecorder延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!