How to get video frames in Android VideoView

In this android programming source code example, we are going to get video frames in Android VideoView.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to get video frames in Android VideoView.

activity_main.xml

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

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:id="@+id/capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="capture frame" />

</RelativeLayout>

MainActivity.java

import android.app.ActionBar;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;


public class VideoViewActivity5 extends AppCompatActivity {
    private VideoView videoView;
    private Button buttonCapture;
    private MediaMetadataRetriever mediaMetadataRetriever;
    private MediaController myMediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view5);
        videoView = (VideoView) findViewById(R.id.videoView);
        buttonCapture = (Button) findViewById(R.id.capture);
        mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(VideoViewActivity5.this, Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test));
        videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test));
        myMediaController = new MediaController(this);
        videoView.setMediaController(myMediaController);

        videoView.setOnCompletionListener(videoViewCompletionListener);
        videoView.setOnPreparedListener(videoViewPreparedListener);
        videoView.setOnErrorListener(videoViewErrorListener);

        videoView.requestFocus();
        videoView.start();
        buttonCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                int currentPosition = videoView.getCurrentPosition(); //in millisecond
                Toast.makeText(VideoViewActivity5.this, "Current Position: " + currentPosition + " (ms)", Toast.LENGTH_LONG).show();
                Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(currentPosition * 1000); //unit in microsecond
                if (bmFrame == null) {
                    Toast.makeText(VideoViewActivity5.this, "null!", Toast.LENGTH_LONG).show();
                } else {
                    AlertDialog.Builder myCaptureDialog = new AlertDialog.Builder(VideoViewActivity5.this);
                    ImageView capturedImageView = new ImageView(VideoViewActivity5.this);
                    capturedImageView.setImageBitmap(bmFrame);
                    ActionBar.LayoutParams capturedImageViewLayoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
                    capturedImageView.setLayoutParams(capturedImageViewLayoutParams);
                    myCaptureDialog.setView(capturedImageView);
                    myCaptureDialog.show();
                }

            }
        });
    }

    MediaPlayer.OnCompletionListener videoViewCompletionListener = new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    Toast.makeText(VideoViewActivity5.this, "End of Video", Toast.LENGTH_LONG).show();
                }
            };

    MediaPlayer.OnPreparedListener videoViewPreparedListener = new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    long duration = videoView.getDuration(); //in millisecond
                    Toast.makeText(VideoViewActivity5.this, "Duration: " + duration + " (ms)", Toast.LENGTH_LONG).show();

                }
            };

    MediaPlayer.OnErrorListener videoViewErrorListener = new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Toast.makeText(VideoViewActivity5.this, "Error!!!", Toast.LENGTH_LONG).show();
                    return true;
       }
    };
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.