问题描述
我想在录像打开时每5秒保存一次视频.
I want to save the video every 5 seconds while the video recording is ON.
我尝试了许多解决方案,但遇到了小故障,也就是说,最后保存的帧"仍处于预览状态约300毫秒.
I have tried many solutions but I am facing a Glitch that is, the Last Saved Frame remains in preview for around 300ms.
我认为原因是在MediaRecorder类中记录器一旦停止,将需要完全重新配置和准备,然后才能重新启动."
I think the reason is in MediaRecorder class "Once a recorder has been stopped, it will need to be completely reconfigured and prepared before being restarted."
谢谢
推荐答案
您可以使用多个mediaMuxer
来编码单独的文件.
You can use multiple mediaMuxer
's to encode separate files.
-
摄像机应发送数据以填充MediaMuxer对象(它本身会生成
.mp4
文件).
在需要时,您可以开始将Camera数据写入第二个(不同的)MediaMuxer,从而自动创建第二个新的.mp4
文件(在开始使用多路复用器时).
When needed, you can start writing the Camera data to a second (different) MediaMuxer thus automatically creating a second new .mp4
file (on begin usage of the muxer).
然后,第一个MediaMuxer可以关闭并保存其文件.您的第一段已经准备好了……
The first MediaMuxer can then close and save its file. Your first segment is ready...
如果需要,请尝试研究此代码,以获取有关将Camera
与mediaMuxer
结合使用的指南:
https://bigflake.com/mediacodec/CameraToMpegTest.java.txt
If needed, try to study this code for a guide on using Camera
with mediaMuxer
:
https://bigflake.com/mediacodec/CameraToMpegTest.java.txt
那么您有一个函数可以在5秒间隔过去后处理事情?在该功能中,可以在两个复用器之间循环记录,从而给一个机会关闭其文件,而另一个则记录下一个片段,反之亦然.
So you have a function that handles things when the 5 second interval has passed? In that function, could cycle the recording between two muxers, giving one a chance to close its file, while the other records the next segment and then vice-versa).
代替以下内容(使用MediaRecorder.OutputFormat.MPEG_4
):
Instead of something like below (using MediaRecorder.OutputFormat.MPEG_4
):
this.mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
您将改为创建一个新的多路复用器(使用MUXER_OUTPUT_MPEG_4
):
You will instead create a new muxer (with MUXER_OUTPUT_MPEG_4
):
//# create a new File to ssave into
File outputFile = new File(OUTPUT_FILENAME_DIR, "/yourFolder/Segment" + "-" + mySegmentNum + ".mp4");
String outputPath = outputFile.toString();
int format = MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4;
try { mMuxer = new MediaMuxer(outputPath, format); }
catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); }
然后您使用以下命令停止多路复用器:
And you stop a muxer with:
mMuxer1.stop(); mMuxer1.release();
PS:
另一个选择是使用 Threads 来运行多个MediaRecorder.这可能会帮助您解决问题.
请参见Android 背景流程指南.
Another option is to use Threads to run multiple MediaRecorders. It might help your situation.
See the Android Background Process guide.
这篇关于启用视频记录功能后,每隔5秒保存一次视频(Android OS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!