question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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;
}

}`

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hobbydevscommented, Jul 3, 2017

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

0reactions
westongangercommented, Feb 18, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Video Recorder – Record Video with your Webcam
Video Recorder is our online app for recording video and taking pictures right in your browser. This simple app has flexible video and...
Read more >
Loom: Async Video Messaging for Work | Loom
Record your screen and camera. Start recording your screen and camera easily. Works on any device using Loom's desktop and mobile apps or...
Read more >
Panopto Express: Screen & Video Recorder
Free online video and screen recorder that works instantly in your web browser, no watermarks, limitations, software to install or sign-up required.
Read more >
Screen Recorder Video Recorder - Apps on Google Play
V Recorder is a stable screen recorder/game recorder/video saver for android, also a powerful all-in-one video editor and photo editor.
Read more >
Video Recorder - Amazon.com
Video Camera Camcorder Full HD 1080P 30FPS 24.0 MP IR Night Vision Vlogging Camera Recorder 3.0 Inch IPS Screen 16X Zoom Camcorders Camera...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found