Recording from JavaCameraView

asked 2016-10-13 18:46:31 -0600

Ftahir192 gravatar image

I've been attempting to record the JavaCameraView for a few days now, to no avail. I'm trying to use MediaRecorder to record.

I have a JavaCameraView in my layout file, have added a CvCameraViewListener as follows:

mCameraView.enableView();
mCameraView.setCvCameraViewListener(this);

where mCameraView = (CameraBridgeViewBase) findViewById(R.id.cameraPreview);

and R.id.camera_preview is

<org.opencv.android.JavaCameraView
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        opencv:show_fps="false"
        opencv:camera_id="back"/>

I am therefore getting a preview on the screen correctly, and onCameraFrame is being called.

The problem I am having with using MediaRecorder and the Camera API to record is that JavaCameraView.java already has an instance of the camera open, and so I cannot open another one to record with.

The mCamera object in JavaCameraViewis protected, so I've attempted to make a class CameraView that extends JavaCameraView, and through there, call the mCamera.lock()/mCamera/unlock() methods needed. This isn't working either.

My last resort was to record the surface instead, and this is being done as follows:

// Step 1: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT );
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

// Step 2: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

// Step 3: Set output file (handled in CameraHelper)
String date = CreateFiles.getDate();
mRootFolder = CreateFiles.getOutputFolder(date);
Log.i(TAG, "Output folder is " + mRootFolder.toString());

mMediaFile = CreateFiles.getOutputMediaFile(
            CreateFiles.MEDIA_TYPE_VIDEO, mRootFolder, date);
mDataFile = CreateFiles.getOutputDataFile(mRootFolder, date);

mMediaRecorder.setVideoSize(mCameraView.getmFrameWidth(), mCameraView.getmFrameHeight());
mMediaRecorder.setOutputFile(mMediaFile.getPath());

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

However when I stop the MediaRecorder, it crashes with the following stack trace:

**10-14 00:28:48.239 23590-23590/uk.ac.objecttracker E/MediaRecorder: stop failed: -1007
10-14 00:28:48.239 23590-23590/uk.ac.objecttracker D/AndroidRuntime: Shutting down VM
10-14 00:28:48.249 23590-23590/uk.ac.objecttracker E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: uk.ac.ed.faizan.objecttracker, PID: 23590
                                                                           java.lang.AssertionError: java.lang.RuntimeException: stop failed.
                                                                               at uk.ac.objecttracker.CameraPreview.releaseMediaRecorder(CameraPreview.java:122)
                                                                               at uk.ac.objecttracker.TrackingActivity.onClick(TrackingActivity.java:127)
                                                                               at android.view.View.performClick(View.java:5702)
                                                                               at android.view.View$PerformClick.run(View.java:22541)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                            Caused by: java.lang.RuntimeException: stop failed.**

And I have no idea what gives. So now I'm asking for help here and would greatly appreciate anybody give some hints as to what I might be doing wrong in my code above, or how I can use the Camera to record (even though it's being used by JavaCameraView).

Thanks a lot in advance.

edit retag flag offensive close merge delete