Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday 12 March 2016

VideoFramingExample

Hello gyes today we are learn about how to divide video framing.

crate a new fresh project in android studio and follow below instruction and if any confusion contact by email.

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"
    android:orientation="vertical">


    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </FrameLayout>


    <Button
        android:layout_margin="2dp"
        android:id="@+id/getdata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/container"
        android:text="Pick video"
        style="@style/MultiGradient"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />


    <Button
        android:layout_margin="2dp"
        android:id="@+id/shows"
        style="@style/SingleGradient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/getdata"
        android:text="Show frame"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />


    <Button
        android:layout_margin="2dp"
        android:id="@+id/capture"
        style="@style/MultiGradient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/shows"
        android:text="capture video image"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />


    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/capture" />

</RelativeLayout>

ActivityMain.java

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {


    //Component
    Button buttonCapture, pick, showFrame;
    VideoView myVideoView;


    //VideoFraming class
    MediaMetadataRetriever mediaMetadataRetriever;
    MediaController myMediaController;




    //String viewSource = "/storage/emulated/0/Video/Afgan.mp4";


    int VideoDuration;
    private String path = null;
    public static ArrayList<Bitmap> rev = new ArrayList<Bitmap>();
    public static final String TAG = "SecondActivity";
    public static final int REQUEST_TAKE_GALLERY_VIDEO = 101;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_frame);
        setView();


        mediaMetadataRetriever = new MediaMetadataRetriever();


        pick.setOnClickListener(this);
        showFrame.setOnClickListener(this);
        buttonCapture.setOnClickListener(this);
    }


    private void setView() {
        myVideoView = (VideoView) findViewById(R.id.videoview);
        buttonCapture = (Button) findViewById(R.id.capture);
        showFrame = (Button) findViewById(R.id.shows);
        pick = (Button) findViewById(R.id.getdata);
    }




    private void getDuration() {/*
    int duration = 0;
    String dur = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    if (dur != null) {
        duration = Integer.parseInt(dur);
    }
    mDuration = duration;
    long h = duration / 3600;
    long m = (duration - h * 3600) / 60;
    long s = duration - (h * 3600 + m * 60);*/
    }


    /***********************************************************************************************/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO && resultCode == RESULT_OK) {
            if (data.getData() != null) {
                Uri selectedImage = data.getData();
                path = getPath(selectedImage);
                startVideo(path);


            } else {
                Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);


    }


    public String getPath(Uri uri) {
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);


        int columnindex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String file_path = cursor.getString(columnindex);
        Log.e(getClass().getName(), "file_path" + file_path);
        Uri fileUri = Uri.parse("file://" + file_path);
        cursor.close();
        return file_path;


    }


    @Override
    public void onClick(View v) {
        if (v == pick) {
            getVideoData();
        } else if (v == buttonCapture) {
            captureFrame();
        } else if (v == showFrame) {
            Log.e(getClass().getName(), "Call0 ");
            Bundle bundle = new Bundle();
            myVideoView.pause();
            //bundle.putSerializable("data", rev);
            PassData.getInstance().setData(rev);
            getSupportFragmentManager().beginTransaction().replace(R.id.container, FrameDisplayFragment.getinstance()).commit();
            //startActivity(new Intent(SecondActivity.this, ShowFrameActivity.class));


            Log.e(getClass().getName(), "Call ");
        }


    }


    private void startVideo(String videoPath) {
        new FramingAsyntask().execute(videoPath);
        myVideoView.setVideoURI(Uri.parse(videoPath));
        myMediaController = new MediaController(this);
        myVideoView.setMediaController(myMediaController);


        myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
        myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
        myVideoView.setOnErrorListener(myVideoViewErrorListener);


        myVideoView.requestFocus();
        myVideoView.start();


    }




    public class FramingAsyntask extends AsyncTask<String, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }


        @Override
        protected Void doInBackground(String... params) {
            try {
                mediaMetadataRetriever.setDataSource(SecondActivity.this, Uri.parse(params[0]));
                String time = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                VideoDuration = Integer.parseInt(time);


                //int millis = mp.getDuration();


                for (int i = 1000000; i < VideoDuration * 1000; i += 1000000) {
                    Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(i, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                    rev.add(bitmap);
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, "Exception " + e.toString());
            }
            mediaMetadataRetriever.release();
            Log.e(TAG, "Video durartion " + VideoDuration);
            Log.e(TAG, "Video frame size " + rev.size());
            return null;
        }
    }


    private void getVideoData() {
        Intent intent;
        /*if (Build.VERSION.SDK_INT < 19) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image*//* video*//*");
            startActivityForResult(photoPickerIntent, REQUEST_TAKE_GALLERY_VIDEO);
        } else {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            photoPickerIntent.setType("/*");
            startActivityForResult(photoPickerIntent, REQUEST_TAKE_GALLERY_VIDEO);
        }
*/










      if (Build.VERSION.SDK_INT < 19) {
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            } else {
                intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
            }
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            //intent.putExtra("return-data", true);
            startActivityForResult(intent, REQUEST_TAKE_GALLERY_VIDEO);
        } else {
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            } else {
                intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
            }
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, REQUEST_TAKE_GALLERY_VIDEO);
        }




    }


    private void captureFrame() {
        int currentPosition = myVideoView.getCurrentPosition(); //in millisecond
        Toast.makeText(SecondActivity.this, "Current Position: " + currentPosition + " (ms)", Toast.LENGTH_LONG).show();


        Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(currentPosition * 1000); //unit in microsecond


        if (bmFrame == null) {
            Toast.makeText(SecondActivity.this, "bmFrame == null!",
                    Toast.LENGTH_LONG).show();
        } else {
            AlertDialog.Builder myCaptureDialog =
                    new AlertDialog.Builder(SecondActivity.this);
            ImageView capturedImageView = new ImageView(SecondActivity.this);
            capturedImageView.setImageBitmap(bmFrame);
            ViewGroup.LayoutParams capturedImageViewLayoutParams =
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            capturedImageView.setLayoutParams(capturedImageViewLayoutParams);


            myCaptureDialog.setView(capturedImageView);
            myCaptureDialog.show();
        }


    }


    MediaPlayer.OnCompletionListener myVideoViewCompletionListener =
            new MediaPlayer.OnCompletionListener() {


                @Override
                public void onCompletion(MediaPlayer arg0) {
                    Toast.makeText(SecondActivity.this, "End of Video",
                            Toast.LENGTH_LONG).show();
                }
            };


    MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
            new MediaPlayer.OnPreparedListener() {


                @Override
                public void onPrepared(MediaPlayer mp) {


                    long duration = myVideoView.getDuration(); //in millisecond
                    Toast.makeText(SecondActivity.this,
                            "Duration: " + duration + " (ms)",
                            Toast.LENGTH_LONG).show();


                }
            };


    MediaPlayer.OnErrorListener myVideoViewErrorListener = new MediaPlayer.OnErrorListener() {


        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {


            Toast.makeText(SecondActivity.this,
                    "Error!MediaPlayer!!",
                    Toast.LENGTH_LONG).show();
            return true;
        }
    };


}

I hope helps this blog if really helps this blog please like and comment.

Find full source code

https://github.com/SamsetDev/VideoFraming-master









  

No comments:

Post a Comment