1

Edit: I think I may have found the solution. Instead of using VideoView, I switched to this implementation of TextureView:

https://github.com/sprylab/texturevideoview

But can someone explain why this seems to have fixed my problem?


I have a simple fragment that loads a video (from an external URL) into a VideoView.

The problem is that, when the fragment is called upon, it doesn't show until the video is fully loaded. It usually takes 1-3 seconds for the fragment to finally load. I believe it might have something to do with the VideoView loading blocking the main UI thread? I'm not 100% sure though.

Here is my fragment:

public class MyFragment extends DialogFragment {

    private User user;

    private ViewPager viewPager;

    private MyViewPagerAdapter myViewPagerAdapter;

    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main, container, false);

        viewPager = (ViewPager) v.findViewById(R.id.viewPager);

        user = Parcels.unwrap(getArguments().getParcelable("user"));

        myViewPagerAdapter = new MyViewPagerAdapter();
        viewPager.setAdapter(myViewPagerAdapter);

        setCurrentItem(selectedPosition);

        return v;
    }

    private void setCurrentItem(int position) {
        viewPager.setCurrentItem(position, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }

    public class MyViewPagerAdapter extends PagerAdapter {

        private LayoutInflater layoutInflater;

        private VideoView videoView;

        public MyViewPagerAdapter() {
            //
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(R.layout.video_layout, container, false);

            UserVideo video = user.getVideos().get(position);

            videoView = (VideoView) view.findViewById(R.id.video);

            Uri videoUri = Uri.parse(video.getUrl());

            videoView.setVideoURI(videoUri);
            videoView.setMediaController(null);
            videoView.requestFocus();
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {
                    videoView.start();
                    mp.setLooping(true);
                }
            });

            container.addView(view);

            return view;
        }

        @Override
        public int getCount() {
            return user.getVideos().size();
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == ((View) obj);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}

What should I change to stop the fragment from taking so long to load?

I tried following this solution in another thread, but it still takes 1-3 seconds for the fragment to load/show up:

https://gist.github.com/anonymous/b4fb728816962c7d208f8d85e7bdba26

Community
  • 1
  • 1

2 Answers2

0

instead of setting the mediacontroller to null, add your media controller to the view. Check the example at https://examples.javacodegeeks.com/android/android-videoview-example/

Fusselchen
  • 382
  • 1
  • 4
  • 12
0

Not sure if this will help but shouldn't the videoView set the mediaController:

    videoView.setMediaController(mediaController);
Chester Cobus
  • 701
  • 4
  • 12
  • I set it to null so that the video controls don't show. – user7752683 Mar 22 '17 at 17:36
  • Then remove it, the VideoView should work without it. – Chester Cobus Mar 22 '17 at 17:43
  • The video could be buffering see this http://stackoverflow.com/questions/15941976/how-to-detect-if-videoview-is-playing-video-or-buffering. – Chester Cobus Mar 22 '17 at 17:53
  • Again, none of those solutions are related. I believe, like I said in the question, that the main UI thread is being blocked while the video is being loaded. I need a solution to fix that. – user7752683 Mar 22 '17 at 17:57
  • Ok hope this helps : http://stackoverflow.com/questions/24529090/android-videoview-setvideouri-blocks-ui-thread – Chester Cobus Mar 22 '17 at 18:01
  • I tried that, but it's still taking 1-3 seconds for the fragment to load/show up: https://gist.github.com/anonymous/b4fb728816962c7d208f8d85e7bdba26 – user7752683 Mar 22 '17 at 18:26
  • In your run method only have these two lines : Uri videoUri = Uri.parse(video.getUrl()); videoView.setVideoURI(videoUri); and move the other lines of code either below or above (try both) the Handler. – Chester Cobus Mar 22 '17 at 18:31
  • Tried both. Still takes 1-3 seconds to load the fragment. – user7752683 Mar 22 '17 at 18:36
  • What I would suggest is to load the video in a background task (AsyncTask) and have a progress dialog for the user see this link : http://www.oodlestechnologies.com/blogs/Working-with-MediaController,--VideoView-in-Android. If this is not what you want try looking into playing videos with a MediaPlayer class, you will have more control with the preparation of the video. – Chester Cobus Mar 22 '17 at 19:26
  • I've also tried using AsyncTask, but it still takes 1-3 seconds to load the fragment. – user7752683 Mar 22 '17 at 19:30