Error opening android camera for streaming video

浪子不回头ぞ 提交于 2019-11-28 06:46:50

问题


I'm trying to write video stream from my Galaxy Tab to server. according to this manual 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 all fails on step recorder.start(); with strange error

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

what does that mean and what should I do to start recorder?

UPD: Trouble happens because of my getFrontCamera method. when I replace it with camera.open() all works correct.

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 setting of format and encoders solved the trouble -

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

Maybe because of pre-build formats are for back camera... But strange anyway.


回答1:


I don't see output format setup, so try adding to recorder set up:

 recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);



回答2:


Have a look

And though it is streaming video, so that set -

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

Have fun.




回答3:


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




回答4:


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

So the profile is indeed dependent on the camera (for high-end phones it appears to work fine without the distinction, since they all support 1080p by now, but low-end phones may crash otherwise)



来源:https://stackoverflow.com/questions/9098216/error-opening-android-camera-for-streaming-video

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