Alexander
Alexander

Reputation: 48262

Keeping preview callback while recording video?

I'm currently using onPreviewCallback so I can capture frames from camera when in preview and stream them via http.

That works but then I issue a command to start recording and, it seems, I do not have a preview callback anymore.

So, how do I keep the preview callback so I can both send the frames from the surface to my server AND record the video on the device?

Upvotes: 6

Views: 4272

Answers (3)

Hanyee
Hanyee

Reputation: 19

You can call these methods after your media recorder.start() being called as following :

camera.reconnect();
camera.setPreviewCallback();
surfaceview.getHolder().addCallback();

The reasons:

  1. After camera.unlock() is called, another process(here is the media recorder process) may use the camera; when the process is done, you must reconnect to the camera, which will re-acquire the lock and allow you to continue using the camera.
  2. And then re-register the surfaceview frame data callback after camera reconnected, because its some state may be changed after reconnected.

I have ever been the same issue as yours in my application, and i fixed it by this. Hope it can resolve your problems!

Upvotes: 1

leorleor
leorleor

Reputation: 582

Once I got the camera and MediaRecorder to start and stop recording without crashing (was not easy) I still had a problem like you described, where the preview callback would stop getting called.

The fix I finally found was adding a call to setPreviewCallback after mediaRecorder.start(), and another after mediaRecorder.stop(). Not sure if this is correct, but it worked on the Razr M I am testing on.

Upvotes: 0

Victor Ronin
Victor Ronin

Reputation: 23268

I didn't work for quite long time with Android Camera. However, as I remember

1) onPreviewCallback isn't called while you are recording

It's mentioned in couple of questions:

Camera onPreviewFrame not called How to show real time filtered camera preview while recording videos?

2) I saw that it was handled in SipDroid and couple of other Android SIP clients following way (this was a 1-2 years ago, so this method could be outdates):

  • A pipe was created
  • Receiving socket of the pipe was wrapped in FileDescriptor and passed to MediaRecorder setOutputFile
  • Sending socket of the pipe was constantly read in a thread.
  • This way you can receive a content which is written to a file
  • Now, the issue how to deal with the content (since, it's H.263 or H.264 encoded and could be mixed with the sound, if you record video with the sound).
  • There were some heuristical algorithms which parsed the content (however, it's pain in the ass)

3) You can use onPreviewFrame + start AudioRecorder and encode it yourself (using ffmpeg or something like that) to mp4 file. This way you don't need to start MediaRecorder recording.

Upvotes: 5

Related Questions