如何用recyclerview和viewpager2实现exoplayer?

Posted

技术标签:

【中文标题】如何用recyclerview和viewpager2实现exoplayer?【英文标题】:How to implement exoplayer with recyclerview and viewpager2? 【发布时间】:2020-11-23 13:17:13 【问题描述】:

我正在创建一个视频供稿,并使用 VideoView 在 vi​​ewpager2 中预览视频。但因为它很慢,我试图转向 exoplayer。一切正常,但问题是适配器中的每个视频都会立即加载,当我从 viewpager 滑动时,视频不会暂停。这是适配器的代码:

public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.VideoViewHolder> 

    private List<VideoItem> videoItems;

    public VideosAdapter(List<VideoItem> videoItems) 
        this.videoItems = videoItems;
    

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        return new VideoViewHolder(
               LayoutInflater.from(parent.getContext()).inflate(
                       R.layout.item_container_video,
                       parent,
                       false
               )
        );
    



    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) 
      holder.setVideoData(videoItems.get(position));
    

    

    @Override
    public int getItemCount() 
        return videoItems.size();
    


    static class VideoViewHolder extends RecyclerView.ViewHolder 

        PlayerView playerView;
        TextView video__description;
        ProgressBar videoProgressBar;
        SimpleExoPlayer simpleExoPlayer;
        LoopingMediaSource loopingMediaSource;
        DefaultHttpDataSourceFactory factory;
        ExtractorsFactory extractorsFactory;
        BandwidthMeter bandwidthMeter;
        TrackSelector trackSelector;

        public VideoViewHolder(@NonNull View itemView) 
            super(itemView);
            playerView = itemView.findViewById(R.id.player__view);
            video__description = itemView.findViewById(R.id.video__description);
            videoProgressBar = itemView.findViewById(R.id.videoLoader);
            factory = new DefaultHttpDataSourceFactory("exoplayer_video");
            extractorsFactory = new DefaultExtractorsFactory();
            bandwidthMeter = new DefaultBandwidthMeter();
            trackSelector = new DefaultTrackSelector(
                    new AdaptiveTrackSelection.Factory(bandwidthMeter)
            );
        

        void setVideoData(VideoItem videoItem) 
            Uri videoUrl =Uri.parse(videoItem.videoURL);
            simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(videoItem.context, trackSelector);
            MediaSource mediaSource = new ExtractorMediaSource(videoUrl,factory,extractorsFactory,null,null);
            loopingMediaSource = new LoopingMediaSource(mediaSource);
            playerView.setPlayer(simpleExoPlayer);
            playerView.setKeepScreenOn(true);
            simpleExoPlayer.prepare(loopingMediaSource);
            simpleExoPlayer.setPlayWhenReady(true);
            video__description.setText(videoItem.videoDescription);
            simpleExoPlayer.addListener(new Player.DefaultEventListener() 
                @Override
                public void onPlayerStateChanged(boolean playWhenReady, int playbackState) 
                    super.onPlayerStateChanged(playWhenReady, playbackState);
                    if(playbackState==Player.STATE_BUFFERING) 
                        videoProgressBar.setVisibility(View.VISIBLE);
                     else if(playbackState==Player.STATE_READY) 
                        videoProgressBar.setVisibility(View.GONE);
                    
                

                @Override
                public void onPlayerError(ExoPlaybackException error) 
                    super.onPlayerError(error);
                
            );
        
    



这是 videoItems 类的代码:

public class VideoItem 
    public String videoURL, videoDescription;
    public Context context;

    public VideoItem(String videoURL, String videoDescription, Context context) 
        this.videoURL = videoURL;
        this.videoDescription = videoDescription;
        this.context = context;
    

这里是设置适配器的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 

        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_first, container, false);


        final ViewPager2 videosViewPager = root.findViewById(R.id.videosViewPager);

            List<VideoItem> videoItems = new ArrayList<>();

VideoItem videoItemFirst = new VideoItem("https://www.infinityandroid.com/videos/video1.mp4","Hello World", requireActivity().getApplicationContext());
            videoItems.add(videoItemFirst);

            VideoItem videoItemSecond = new VideoItem("https://www.infinityandroid.com/videos/video2.mp4","Hello World", requireActivity().getApplicationContext());
            videoItems.add(videoItemSecond);

            VideoItem videoItemThird = new VideoItem("https://www.infinityandroid.com/videos/video3.mp4","Hello World", requireActivity().getApplicationContext());
            videoItems.add(videoItemThird);

videosViewPager.setAdapter(new VideosAdapter(videoItems));


return root;


这是布局文件:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.firstFragment"
    >

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/videosViewPager"
        android:orientation="horizontal"
        android:layout_
        android:layout_
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
         />

</androidx.constraintlayout.widget.ConstraintLayout>

加载片段时似乎一次加载所有视频,当我滑动到下一个视频时,上一个视频不会暂停。如何解决这个问题?有没有更好的方法?希望你能回答。

【问题讨论】:

我做了一个新项目,可以帮助所有正在尝试的人:github.com/paulo-coutinho/rvplayer 【参考方案1】:

此代码基于带有 RecyclerView 的 ExoPlayer :

https://github.com/NehaKushwah993/InstagramVideoFeedClone/blob/main/README.md

【讨论】:

以上是关于如何用recyclerview和viewpager2实现exoplayer?的主要内容,如果未能解决你的问题,请参考以下文章

如何用 ViewPager 中的另一个片段替换 Android 片段?

使用 recyclerview 和 Asynctask 设置 Viewpager

如何用所选目录的文件替换 recyclerview 中的现有内容?

带有折叠工具栏的 RecyclerView 和 ViewPager

如何用RecyclerView android选择一个单选按钮?

ViewPager 滑动不适用于 RecyclerView