Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play video in fragment

I want to display video in fragment I have used this code to play it, it worked

   View rootView = inflater.inflate(R.layout.quranic_section, container, false);

    VideoView view = (VideoView)rootView.findViewById(R.id.video_view);
    String path = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.howtopray;
    view.setVideoURI(Uri.parse(path));
    view.start();

but i want to add button like play stop resume .. how can I do it ?

like image 730
Hassan Echafaii Avatar asked Mar 16 '23 18:03

Hassan Echafaii


1 Answers

Use MediaController with VideoView:

View rootView = inflater.inflate(R.layout.quranic_section, container, false);
MediaController mc= new MediaController(getActivity());

VideoView view = (VideoView)rootView.findViewById(R.id.video_view);
String path = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.howtopray;
view.setVideoURI(Uri.parse(path));
view.setMediaController(mc);
view.start();

Edit

MediaController constructor takes Context as a parameter. Added getActivity() method to provide Context to MediaController constructor.

like image 65
Giorgio Antonioli Avatar answered Apr 02 '23 17:04

Giorgio Antonioli