从URL加载VideoView滞后片段

时间:2017-03-22 17:20:49

标签: android android-fragments android-viewpager android-videoview

编辑:我想我可能找到了解决方案。我没有使用VideoView,而是转而使用TextureView的实现:

https://github.com/sprylab/texturevideoview

但有人可以解释为什么这似乎解决了我的问题吗?

我有一个简单的片段,可以将视频(从外部网址)加载到VideoView中。

问题在于,当调用片段时,在视频完全加载之前它不会显示。片段最终加载通常需要1-3秒。我相信它可能与阻止主UI线程的VideoView加载有关?我不是100%肯定。

这是我的片段:

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);
        }
    }
}

我应该更改什么来阻止片段加载这么长时间?

我尝试在另一个帖子中关注this solution,但片段加载/显示仍需要1-3秒:

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

2 个答案:

答案 0 :(得分:0)

而不是将mediacontroller设置为null,将媒体控制器添加到视图中。查看https://examples.javacodegeeks.com/android/android-videoview-example/

上的示例

答案 1 :(得分:0)

不确定这是否有帮助,但不应该为videoView设置mediaController:

    videoView.setMediaController(mediaController);