Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video Recording #135

Open
pavelupward opened this issue Sep 22, 2016 · 6 comments
Open

Video Recording #135

pavelupward opened this issue Sep 22, 2016 · 6 comments
Labels

Comments

@pavelupward
Copy link

I made the service that launches the camera and record video in the background, but the video quality is very low, I assigned mMediaRecorder.setVideoSize (1920, 1080); but nothing has changed. If I add here this mMediaRecorder.setProfile line (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH)); then logs see such a warning System.err: at com.hidecamera.camera.RecorderService.startRecording (RecorderService.java:115) and the camera does not start, and if I delete this line, the camera starts and record video in a very poor quality

`public class MainActivity extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "Recorder";
public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mCameraBack;
private Button changeCamera;

public  boolean currentCameraId = false;

/**
 * Called when the activity is first created.
 */
@SuppressLint("WrongViewCast")
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);

    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    changeCamera = (Button) findViewById(R.id.changeCamera);
    changeCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            currentCameraId = true;
        }
    });
    Button btnStart = (Button) findViewById(R.id.StartService);
    btnStart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, RecorderService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

            startService(intent);
            finish();
        }
    });

    Button btnStop = (Button) findViewById(R.id.StopService);
    btnStop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            stopService(new Intent(MainActivity.this, RecorderService.class));
        }
    });
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

}`

`public class RecorderService extends Service {
private static final String TAG = "RecorderService";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private static Camera mServiceCamera;
public static boolean mPreviewRunning;
private boolean mRecordingStatus;
private MediaRecorder mMediaRecorder;

@Override
public void onCreate() {
    mRecordingStatus = false;
    mServiceCamera = MainActivity.mCameraBack;
    mSurfaceView = MainActivity.mSurfaceView;
    mSurfaceHolder =
            MainActivity.mSurfaceHolder;// для обновления изображения из фоновых потоков.

    super.onCreate();
}

@Override
public IBinder onBind(
        Intent intent) {//возвращает канал связи, который используют клиенты, чтобы взаимодействовать со службой.
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    if (mRecordingStatus == false) {
        startRecording();
    }

    return START_STICKY;//будет вызываться при повторном запуске сервиса после преждевременного завершения работы.
}

@Override
public void onDestroy() {
    stopRecording();
    mRecordingStatus = false;

    super.onDestroy();
}

@SuppressWarnings("deprecation")
public boolean startRecording() {

    try {
        Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();

           // mServiceCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
            mServiceCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

        //mServiceCamera = Camera.open();
        Camera.Parameters params =
                mServiceCamera.getParameters();//получить текущие настройки камеры.
        params.set("cam_mode", 1);
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        mServiceCamera.setParameters(params);
        Camera.Parameters p = mServiceCamera.getParameters();

        final List<Size> listSize = p.getSupportedPreviewSizes();
        Size mPreviewSize = listSize.get(1);
        mPreviewSize.width = 1920;
        mPreviewSize.height = 1080;
        Log.v(TAG, "use: width = " + mPreviewSize.width + " height = " + mPreviewSize.height);

        p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        mServiceCamera.setParameters(p);

        try {
            mServiceCamera.setPreviewDisplay(mSurfaceHolder);
            mServiceCamera.startPreview();
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        }
        mServiceCamera.lock();
        mServiceCamera.unlock();

        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        String externalFilesDir = new String(Environment.DIRECTORY_DOWNLOADS);
        //mMediaRecorder.setOutputFile(getExternalFilesDir(externalFilesDir) + "BLE_Video_Record_" +  System.currentTimeMillis()+".mp4");
        mMediaRecorder.setOutputFile(
                Environment.getExternalStorageDirectory().getPath() + "/video.mp4");
          // mMediaRecorder.setProfile(CamcorderProfile.get( CamcorderProfile.QUALITY_1080P));




        mMediaRecorder.setVideoFrameRate(30);
        // mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
        mMediaRecorder.setVideoSize(1920, 1080);

        // mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

        mMediaRecorder.prepare();
        mMediaRecorder.start();

        mRecordingStatus = true;

        return true;
    } catch (IllegalStateException e) {
        String err = (e.getMessage() == null) ? "SD Card failed" : e.getMessage();
        Log.e("sdcard-err2:", err);

        e.printStackTrace();
        return false;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        e.printStackTrace();
        return false;
    }
}

public void stopRecording() {
    Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
    try {
        mServiceCamera.reconnect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (!mRecordingStatus) {
        return;
    }
    mMediaRecorder.stop();
    mMediaRecorder.reset();

    mServiceCamera.stopPreview();
    mMediaRecorder.release();

    mServiceCamera.release();
    mServiceCamera = null;
}

}`

@Xwalk-Company
Copy link

Hi pavelupward,
Have you tried with the autofocus of the camera's parameter?
Like this
` // get Camera parameters
Camera.Parameters params = mCamera.getParameters();

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
        // Autofocus mode is supported
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        mCamera.setParameters(params);
    }`

Otherwise on the touch event of the camera

getActivity().runOnUiThread(new Runnable() { @Override public void run() { mCamera.cancelAutoFocus(); mCamera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { Log.d(TAG, "FOCUS"); } }); } });

@KamranAliKhan01
Copy link

@pavelupward Can you please guide me how to record video with the current plugin and where to add the above code?

@erperejildo
Copy link
Collaborator

@pavelupward can you provide more info about video recording?

@westonganger westonganger changed the title A bad video recording quality when using the camera Video Recording Mar 7, 2017
@hartherbert
Copy link
Contributor

Is there anybody with a plugin version where it is possible to record a video ?

@westonganger
Copy link
Collaborator

@vipulsharma144 has submitted PR #587 which completes the work done from @icetec-solutions implementation. I would appreciate if anyone has any comments on that PR.

@westonganger
Copy link
Collaborator

Video recording functionality for Android has been implemented in master via #587

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants