Error opening android camera for streaming video

I am trying to record a video stream from my Galaxy tab to a server. according to this guide i should do something like this:

frontCamera = getFrontCamera(); if((socket!= null)&&(frontCamera!=null)) { try { frontCamera.setPreviewDisplay(cameraPreview.getHolder()); } catch (IOException e1) { // TODO Auto-generated catch block Log.e("","",e1); } frontCamera.startPreview(); recorder = new MediaRecorder(); frontCamera.unlock(); recorder.setCamera(frontCamera); recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); recorder.setProfile(CamcorderProfile.get( CamcorderProfile.QUALITY_HIGH)); pfd = ParcelFileDescriptor.fromSocket(socket); recorder.setOutputFile(pfd.getFileDescriptor()); recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface()); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block Log.e("","",e); } catch (IOException e) { // TODO Auto-generated catch block Log.e("","",e); } 

but everything crashes in step recorder.start(); with a strange mistake

 02-01 19:03:39.265: E/MediaRecorder(11922): start failed: -19 

What does it mean and what should I do to start the recorder?

UPD: The problem is due to my getFrontCamera method. when i replace it with camera.open () everything works correctly.

 protected Camera getFrontCamera() { Camera.CameraInfo inf = new Camera.CameraInfo(); for(int i = 0; i< Camera.getNumberOfCameras(); i++) { Camera.getCameraInfo(i, inf); if(inf.facing==Camera.CameraInfo.CAMERA_FACING_FRONT) { return Camera.open(i); } } return null; } 

Upd2 - yes, explicit format and encoder settings solved the problem -

  recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 

Perhaps because of the pre-build formats for the rear camera ... But it's weird.

0
source share
4 answers

I do not see the settings for the output format, so try adding a setting to the recorder:

  recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 
0
source

Take a look.

And although it's streaming video, so set is

 recorder.setOutputFormat(8); recorder.setOutputFile(socketFd); 

Good luck.

0
source

I have a hack extending the media recorder class and removing super.setVideoFrameRate (rate) solves the problem for me.

0
source

If you still want to use CamcorderProfile.QUALITY_HIGH with the front camera, you can use the following:

 CamcorderProfile camcorderProfile = CamcorderProfile.get(currentCameraId, CamcorderProfile.QUALITY_HIGH); recorder.setProfile(camcorderProfile); 

where int currentCameraId is Camera.CameraInfo.CAMERA_FACING_BACK or ...FRONT

Thus, the profile really depends on the camera (for high-quality phones, it works fine without distinction, since they all support 1080p by now, but junior phones may crash otherwise)

0
source

Source: https://habr.com/ru/post/915183/


All Articles