DAVIDBALAS1
DAVIDBALAS1

Reputation: 484

Android Media Recorder preview before saving

I am recording video with audio using MediaRecorder, however when you take pictures with Camera you can save the image into a Bitmap and display it before saving, I want to do the same with the video - like Snapchat does, is there a way? I don't want to save the video, display it, and then have the option to delete or keep it.

I am using this code in order to record videos:

    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SelfieLightCamera/";
    File dir = new File(path);
    if (!dir.exists())
        dir.mkdirs();
    String myFile = path + "Video_" + System.currentTimeMillis() + ".mp4";
    mediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mediaRecorder.setOutputFile(myFile);
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        e.printStackTrace();
    } catch (IOException e) {
        releaseMediaRecorder();
        e.printStackTrace();
    }
    mediaRecorder.start();

Upvotes: 0

Views: 198

Answers (1)

Timo Salomäki
Timo Salomäki

Reputation: 7189

The cool thing about Android apps is that you can extract the APK from your phone and decompile the package to see the actual Java code. Although it's usually obfuscated (as is the case with Snapchat), you can still get quite a good view into the inner workings of the application.

When looking into how Snapchat does it, I found out that they also use MediaRecorder just like you but save the file into internal storage. Internal storage means that the file is only accessible by the app that holds it.

Here's a few lines of code that gives you a general idea of Snapchat does behind the scenes:

    mediaRecorder.setCamera(ay$b.b());
    mediaRecorder.setAudioSource(5);
    if (z) {
        mediaRecorder.setVideoSource(2);
    } else {
        mediaRecorder.setVideoSource(1);
    }
    mediaRecorder.setProfile(camcorderProfile);
    mediaRecorder.setVideoSize(i, i2);
    mediaRecorder.setMaxFileSize(bm.a());
    mediaRecorder.setVideoEncodingBitRate(bm.a(camcorderProfile));
    // Removed some code here…
    mediaRecorder.setOrientationHint(this.f);
    mediaRecorder.setMaxDuration(HttpService.DEFAULT_READ_TIMEOUT);
    mediaRecorder.setOutputFile(this.a.toString());
    if (this.c != null) {
        mediaRecorder.setPreviewDisplay(this.c);

What you should do is record the video normally and save it to the internal storage. If you want to "keep" it, you just move it to the external storage where it's publicly available. Discarding it means that you just delete it from the internal storage.

Upvotes: 1

Related Questions