Program-Me-Rev
Program-Me-Rev

Reputation: 6634

How to play a local Video file

I am trying to play a video, but nothing at all happens :

Log.v("MyApp", "PATH : " + videoPath);

LinearLayout linearLayout = new LinearLayout(mContext);

LayoutParams videoView_LP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

VideoView videoView = new VideoView(RevLibGenConstantine.REV_CONTEXT);
videoView.setLayoutParams(videoView_LP);

videoView.setVideoPath(videoPath);
videoView.requestFocus();
videoView.start();

linearLayout.addView(videoView);  

What am I not doing right?

The Video file path is /storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4

Upvotes: 2

Views: 2941

Answers (2)

Nirav Bhavsar
Nirav Bhavsar

Reputation: 2233

You can directly set layout param in videoview and use setVideoURI, Please refer below code and check.

RelativeLayout relativeLayout = findViewById(R.id.yourrelativelayout);
LinearLayout linearLayout = new LinearLayout(mContext);

VideoView video = new VideoView(this);
video.setVideoURI(videoUri);

video.setLayoutParams(new LinearLayout.LayoutParams(
                             LinearLayout.LayoutParams.MATCH_PARENT,
                             LinearLayout.LayoutParams.MATCH_PARENT));

video.requestFocus();
video.start();

linearLayout.addView(video);

// please attach above layout to your xml view
relativeLayout.addView(linearLayout); // here you can either relative or linear

To get videoUri from video path then do like below

Uri videoUri = Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/VID_20180212_195520.mp4"));

Upvotes: 2

Lino
Lino

Reputation: 6160

I believe that you don't see any video playing on the screen because the graphical widgets you created programmatically are effectively not associated to the layout of the activity.

The fastest solution might be the following:

  1. define a LinearLayout through xml
  2. get a reference of it and use it to attach the video view

    LinearLayout linearLayout = findViewBy(R.id.yourlinearlayoutid)

    LayoutParams videoView_LP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    VideoView videoView = new VideoView(RevLibGenConstantine.REV_CONTEXT); videoView.setLayoutParams(videoView_LP);

    videoView.setVideoPath(videoPath); videoView.requestFocus(); videoView.start();

    linearLayout.addView(videoView);

Upvotes: 0

Related Questions