stop and release exoplayer from viewpager 2

核能气质少年 提交于 2021-02-05 08:39:06

问题


I am using Exoplayer with ViewPager2.

Home Fragment.java

public class HomeFragment extends Fragment {


    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view= inflater.inflate(R.layout.fragment_home, container, false);

     

        ViewPager2 verticalViewPager = view.findViewById(R.id.vPager);
     
        verticalViewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);

        ArrayList<VideoDataModel> dataset = new ArrayList<VideoDataModel>();
        VideoDataModel videoData = new VideoDataModel();
        videoData.setId("1");
        videoData.setUrl("https://cdn.videvo.net/videvo_files/video/free/2019-02/small_watermarked/181004_10_LABORATORIES-SCIENCE_22_preview.webm");
        videoData.setUserid("112800058400468785815");
        videoData.setDescription("Video 1");
        dataset.add(videoData);

        VideoDataModel videoData1 = new VideoDataModel();
        videoData1.setId("2");
        videoData1.setUrl("https://cdn.videvo.net/videvo_files/video/premium/video0122/small_watermarked/100a%20Factory_preview.webm");
        videoData1.setUserid("112800058400468785815");
        videoData1.setDescription("Video 2");
        dataset.add(videoData1);




        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(view.getContext(),dataset);
        verticalViewPager.setAdapter(viewPagerAdapter);


        return  view;

    }





    @Override
    public void onResume() {
        super.onResume();


        BottomNavigationView bottomNavigationView = ((MainActivity) getActivity()).findViewById(R.id.bottom_navigation);
        bottomNavigationView.setBackground( new ColorDrawable(getResources().getColor(android.R.color.black)));
        bottomNavigationView.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
        bottomNavigationView.setItemIconTintList(ColorStateList.valueOf(Color.WHITE));
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".Home.HomeFragment">



    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vPager"/>


</FrameLayout>

ViewpagerAdapter.java

public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> {

    Context mContext;


    LinearLayout sourdImage_layout;
    ImageView soundImage;
    LinearLayout shared_layout;
    LinearLayout comment_layout;
    LinearLayout like_layout;
    FrameLayout profile_lt;
    LinearLayout gift_layout;
    LinearLayout collab_layout;
    Button check_bal_btn;
    LinearLayout upload;
    SharedPreferences pref;
    LinearLayout shop;





    ArrayList<VideoDataModel> videoDataset;

    public ViewPagerAdapter(Context mContext, ArrayList<VideoDataModel> videoDataset) {
        this.mContext = mContext;
        this.videoDataset = videoDataset;
    }

    @NonNull
    @Override
    public ViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup container, int viewType) {

        LayoutInflater mLayoutInflater = LayoutInflater.from(container.getContext());
        View itemView = mLayoutInflater.inflate(R.layout.customviewpager, container, false);
        ViewHolder viewHolder = new ViewHolder(itemView);



        soundImage = itemView.findViewById(R.id.sound_image);
        sourdImage_layout = itemView.findViewById(R.id.sound_image_layout);
        shared_layout = itemView.findViewById(R.id.shared_layout);
        comment_layout = itemView.findViewById(R.id.comment_layout);
        like_layout = itemView.findViewById(R.id.like_layout);
        profile_lt = itemView.findViewById(R.id.profile_lt);
        gift_layout = itemView.findViewById(R.id.gift_layout);
        collab_layout = itemView.findViewById(R.id.collab_layout);
        upload = itemView.findViewById(R.id.upload);
        shop = itemView.findViewById(R.id.shop);


        like_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),"Released",Toast.LENGTH_SHORT).show();

            }
        });


        pref = mContext.getSharedPreferences(Variables.pref_name, 0); // 0 - for private mode

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false)
                {
                    Intent intent1 = new Intent(mContext, VideoRecorderActivity.class);
                    mContext.startActivity(intent1);
                }
                else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
                }
            }
        });

        shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false)
                {
                    Intent intent1 = new Intent(mContext, ShopActivity.class);
                    mContext.startActivity(intent1);
                }
                else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
                }
            }
        });


        final BottomSheetDialog CommentBottomSheetDialog = new BottomSheetDialog(mContext);
        View sheetView =  mLayoutInflater.inflate(R.layout.comment_bottom_sheet, null);
        CommentBottomSheetDialog.setContentView(sheetView);

//
        final BottomSheetDialog GiftBottomSheetDialog = new BottomSheetDialog(mContext);
        View giftSheetView =  mLayoutInflater.inflate(R.layout.gift_bottom_sheet, null);
        check_bal_btn = giftSheetView.findViewById(R.id.check_bal_btn);
        GiftBottomSheetDialog.setContentView(giftSheetView);

        check_bal_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(mContext, WalletActivity.class);
                mContext.startActivity(intent1);
            }
        });

        final BottomSheetDialog collabBottomSheetDialog = new BottomSheetDialog(mContext);
        View collabSheetView =  mLayoutInflater.inflate(R.layout.collab_bottom_sheet, null);
        collabBottomSheetDialog.setContentView(collabSheetView);

        RotateAnimation rotate = new RotateAnimation(
                0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
        );
        rotate.setDuration(4800);
        rotate.setRepeatCount(Animation.INFINITE);
        sourdImage_layout.startAnimation(rotate);


        //share button
        shared_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT,"I suggest this app for you : https://play.google.com/store/apps/details?id=com.android.chrome");
                intent.setType("text/plain");
                mContext.startActivity(intent);
            }
        });


        //Comments
        comment_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false) {
                    CommentBottomSheetDialog.show();
                }
                else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
                }
            }
        });

        //Gift
        gift_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false) {
                    GiftBottomSheetDialog.show();
                }
                else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
                }
            }
        });


        //Collab
        collab_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false) {
                    collabBottomSheetDialog.show();
                }else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
                }

            }
        });

        //Open Others Profile
        profile_lt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if( pref.getBoolean(Variables.islogin,false)!=false) {
                    Intent intent1 = new Intent(mContext, OthersProfileActivity.class);
                    intent1.putExtra("userID", "111805417824620727725");
                    mContext.startActivity(intent1);
                }
                else
                {
                    Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_LONG).show();
                }
            }
        });



        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewPagerAdapter.ViewHolder holder, int position) {

        MediaSource videoSource = new ExtractorMediaSource.Factory(holder.datasourceFactory
        ).createMediaSource(Uri.parse(videoDataset.get(position).getUrl()));


        holder.player.prepare(videoSource);
        holder.player.setPlayWhenReady(true);
        holder.player.setRepeatMode(Player.REPEAT_MODE_ONE);

        holder.playerView.setUseController(false);
        holder.playerView.hideController();
        holder.playerView.setShowBuffering(true);

        holder.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);

        holder.username.setText(videoDataset.get(position).getUserid());
        holder.description.setText(videoDataset.get(position).getDescription());

    }



    @Override
    public int getItemCount() {
        return videoDataset.size();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        private PlayerView playerView;
        private SimpleExoPlayer player;
        DataSource.Factory datasourceFactory;

        TextView username;
        TextView description;



        public ViewHolder(View itemView) {
            super(itemView);

            playerView = itemView.findViewById(R.id.playerview);
            username = itemView.findViewById(R.id.username);
            description = itemView.findViewById(R.id.desc_txt);


            player = ExoPlayerFactory.newSimpleInstance(itemView.getContext(),new DefaultTrackSelector());

            playerView.setPlayer(player);



            datasourceFactory= new DefaultDataSourceFactory(itemView.getContext(),
                    Util.getUserAgent(itemView.getContext(), "videoApp"));


        }
    }
}

customviewPager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:background="@color/black"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/tools"
    app:ignore="NamespaceTypo">


    <androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:padding="0dp"
        card_view:cardBackgroundColor="@color/transparent">




        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mainlayout">




            <com.google.android.exoplayer2.ui.PlayerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/playerview"

                app:show_buffering="when_playing"

                app:controller_layout_id="@layout/item_player_contoller"

                android:layout_gravity="center"


                app:surface_type="surface_view"

                />


        </RelativeLayout>

    </androidx.cardview.widget.CardView>



</RelativeLayout>

I want to release and stop the player as I swipe the page. Only the page which is currently being view should have the exoplayer running. Running all the player lead to performance issues. I tried using onViewDetachedFromWindow method to release the player but if I swipe back , My player does not play...


回答1:


init your player again in viewpager fragment onresume because you have released surface of the player.




回答2:


ViewPager has callback functions that could be helpful in your case

  • onPageScrolled() — Triggered when a scroll event occurs for the current page
  • onPageSelected() — Triggered when a new page becomes selected
  • onPageScrollStateChanged() — Triggered when the scroll state changes

You could maybe when a new page is selected stop the old exoplayer and start the new one




回答3:


override following methods from your ViewpagerAdapter.java class

and class viewholder function as shown below

  @Override
public void onViewDetachedFromWindow(@NonNull VideoPlayerViewHolder holder) {
    super.onViewDetachedFromWindow(holder);
    holder.pausePlayer();
    holder.releasePlayer();
}

@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
    super.onViewAttachedToWindow(holder);
    holder.startPlayer();
}

ViewHolder functions

public void startPlayer() {
    player.setPlayWhenReady(true);
    player.getPlaybackState();
}

public void pausePlayer() {
    player.setPlayWhenReady(false);
    player.getPlaybackState();
}

 public void releasePlayer() {
    player.setPlayWhenReady(false);
    player.release();
    player.stop(true);
}

Let me know if any error..?



来源:https://stackoverflow.com/questions/62499013/stop-and-release-exoplayer-from-viewpager-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!