Android Mediarecorder记录的视频时长与预期时长不同 [英] Android Mediarecorder recorded video duration different from expected duration
问题描述
我使用android Mediarecorder录制了视频.
I recorded a video using android Mediarecorder.
(主要问题:我需要知道视频的确切开始时间[系统时间]和结束时间[系统时间],并且[结束时间-开始时间]必须与实际视频的时长相匹配)
MediaRecorder.start
startTime = System.currentTimeMillis()
then on stop
MediaRecorder.stop
endTime = System.currentTimeMillis()
我希望视频的播放时长
expected_duration =(endTime-startTime)
expected_duration = (endTime - startTime)
但是,
expected_duration始终大于实际的持续时间 视频.
expected_duration is always more than the actual duration of the video.
我怀疑MediaRecorder.start的速度很慢,花了一些时间才真正开始将帧写入视频.
My suspicion is that MediaRecorder.start is slow, it took some time before it actually started writing the frames into a video.
那么,现在,MediaRecorder开始将第一帧写入视频时,是否有任何通知吗?还是有什么方法可以弄清视频实际开始录制的确切系统开始时间.
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. ^^
推荐答案
我发现获得真实开始时间的最佳方法(但仍然不确定它是否足够准确)是找到持续时间,然后从中减去像这样的endTime:
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;
这篇关于Android Mediarecorder记录的视频时长与预期时长不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!