Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Video Recording in Android with MediaRecorder

Tech 1

Incorrect ordering of MediaRecorder configuration methods can lead too initialization failures. The following example demonstrates proper setup for video recording in a Android application.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.videorecorder"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".VideoCaptureActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Layout File (activity_video_capture.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <Button
        android:id="@+id/btn_start_recording"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="beginRecording"
        android:text="Start Recording" />

    <Button
        android:id="@+id/btn_stop_recording"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="endRecording"
        android:text="Stop Recording" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="launchSystemRecorder"
        android:text="Use System Camera" />

    <SurfaceView
        android:id="@+id/surface_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Activity File (VideoCaptureActivity.java)

package com.example.videorecorder;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

public class VideoCaptureActivity extends Activity {
    private MediaRecorder videoRecorder;
    private SurfaceView previewSurface;
    private SurfaceHolder surfaceHolder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_capture);
        previewSurface = (SurfaceView) findViewById(R.id.surface_preview);
        surfaceHolder = previewSurface.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void beginRecording(View view) {
        try {
            videoRecorder = new MediaRecorder();
            videoRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            videoRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            videoRecorder.setOutputFile("/sdcard/video.3gp");
            videoRecorder.setVideoSize(640, 480);
            videoRecorder.setVideoFrameRate(5);
            videoRecorder.setPreviewDisplay(surfaceHolder.getSurface());
            videoRecorder.prepare();
            videoRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endRecording(View view) {
        if (videoRecorder != null) {
            videoRecorder.stop();
            videoRecorder.release();
            videoRecorder = null;
        }
    }

    public void launchSystemRecorder(View view) {
        Intent systemIntent = new Intent();
        systemIntent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        systemIntent.addCategory(Intent.CATEGORY_DEFAULT);
        File outputFile = new File("/sdcard/system_record_" + System.currentTimeMillis() + ".3gp");
        systemIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
        startActivityForResult(systemIntent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, "Recording completed", Toast.LENGTH_SHORT).show();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.