6

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?

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

3 Answers3

5

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.

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • 1. all this answers won't work https://stackoverflow.com/questions/52314822/video-recording-and-onpreviewframe-callback-at-the-same-time 2. I don't think it will help to solve this issue (to record local video+audio and get raw frames at the same time) 3. - all external libs works ugly, don't ever use FFmpeg for video recording on Android – user924 Sep 14 '18 at 06:54
1

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!

Hanyee
  • 19
  • 4
  • no, it doesn't work https://stackoverflow.com/questions/52314822/video-recording-and-onpreviewframe-callback-at-the-same-time – user924 Sep 14 '18 at 06:48
  • and what if I use SurfaceTexture? anyway it's not about SurfaceView or TextureView at all.. we're talking about onPreviewFrame callback and not about visualization – user924 Sep 14 '18 at 06:49
  • so your third line isn't important, though first two lines `camera.reconnect(); camera.setPreviewCallback();` won't help to solve this issue – user924 Sep 14 '18 at 06:51
0

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.

leorleor
  • 554
  • 5
  • 14
  • doesn't work https://stackoverflow.com/questions/52314822/video-recording-and-onpreviewframe-callback-at-the-same-time – user924 Sep 14 '18 at 06:52
  • @user924 Pretty sure getting preview frames during recording does work, because preview frames were the only way to give a live view on the device screen of what is being recorded. That said, there are a lot of places where this can go wrong. Many phones had quirks too and would even crash if the API was not used just so. And the API may have changed in newer Android releases (this answer is 5 years old). I will take a look at your link and help if I can there k.. – leorleor Sep 15 '18 at 14:00