Android: How to initialize MediaRecorder without a valid surface for video preview?

孤街醉人 提交于 2019-12-30 03:15:11

问题


I am building a camera app, where videos are continuously being captured and saved to the SD card. The videos are short (few minutes), and their length are preset with setMaxDuration().

The whole process works fine, while the main activity is in the foreground. But, when I go to another activity (e.g. settings), the video recording works in the background only until max duration is reached. The file is saved, but a new sequence can not be started because prepare() fails, apparently because setPreviewDisplay() doesn't like not having a proper surface to attach to.

I tried to use a dummy Surface, a dummy SurfaceHolder, lockCanvas(), and various other tricks, but nothing works. Is there a way to initialize MediaRecorder without a valid surface?


回答1:


Unfortunately it is still a requirement for you to preview onto a valid surface in order to record video (Android SDK 1.6 thru 2.2). There is an enhancement request logged to remove this restriction http://code.google.com/p/android/issues/detail?id=9570




回答2:


I used a service to do so, and a 1px * 1px wide surface. A how-to can be found here: http://pickerwengs.blogspot.de/2011/05/android-background-video-recorder-on.html




回答3:


I think the IP Webcam app can do this, though I don't know how. Maybe it only works on >=2.3. I'm not sure what version I saw it running on. I asked a question to find how how it's working.




回答4:


You just have to create a surface with MediaCodec by adding the following line:

Surface  mySurface = MediaCodec.createPersistentInputSurface();
mMediaRecorder.setPreviewDisplay(mySurface);

...

mMediaRecorder.prepare();



回答5:


Are you implementing Surface callbacks?

Why do you need to initialize MediaRecorder without a valid surface?

Are you supposed to be recording even when you leave the main activity? or does recording stop before the other acitivty is spawned?



来源:https://stackoverflow.com/questions/1252172/android-how-to-initialize-mediarecorder-without-a-valid-surface-for-video-previ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!