Search code examples
androidandroid-camerasurfaceviewandroid-mediarecorder

Surface view Camera not saving video


 private boolean prepareMediaRecorder(){
    myCamera = getCameraInstance();

    // set the orientation here to enable portrait recording.
    setCameraDisplayOrientation(this,0,myCamera);

    mediaRecorder = new MediaRecorder();

    myCamera.unlock();

    mediaRecorder.setCamera(myCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    String pathh=Environment.getExternalStorageDirectory().getPath();
    Toast.makeText(getApplicationContext(), "Path is "+pathh,Toast.LENGTH_LONG).show();
    mediaRecorder.setOutputFile("/sdcard/myvideo1.mp4");
    //mediaRecorder.setOutputFile("/storage/sdcard0/myvideo1.mp4");
    mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50Mb

mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
    mediaRecorder.setOrientationHint(MainActivity.orientation);
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;

}

My task is to capture video using surface view and send that to server. I found 1st solution to capture video in surface and save it in memory using some stack overflow link. One is below.

Switch To Front Camera and Back Camera Android SurfaceView

At first the app starts and worked perfectly saved the video also. Then I deleted the video and then tried video was not saving in memory. I tried with renaming the file also not worked.

"lrwxrwxrwx" what is this code value mean in android. I find this in DDMS


Solution

  • The code is missing MediaScannerConnection.scanFile, which updates gallery. The code may be saving the videos, but the gallery will not show the videos. Restarting the phone will scan the gallery, and if videos were saved they will appear. Also, the files app will probably list the videos.

    If the program is saving videos, add code at or near the end of the program, or override onPause: MediaScannerConnection.scanFile(this, new String[]{videoPathName}, null, null); videoPathName is a String you need to set to the path and name of the saved video. If still not working, the following code should work, but you will still need to add the MediaScanner: http://sandyandroidtutorials.blogspot.com/2013/05/android-video-capture-tutorial.html