Exoplayer is not stop in RecyclerView when player detached from window?

0

 Problem Describe: When You use Exoplayer as a item in RecyclerView with Firebase ui adapter then you can suffer from this type of problem. Your exoplayer not stop when item detached from widow or activity's onStop() onPause() even in onDestroy() method . This problem occurs when you want to stop exoplayer in Activity/Fragments by those methods (onStop() onPause()onDestroy()) .

Example:

@Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
        if (HomeVideoHolder.simpleExoPlayer != null) {
            HomeVideoHolder.simpleExoPlayer.stop();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        adapter.stopListening();
        if (HomeVideoHolder.simpleExoPlayer != null) {
            HomeVideoHolder.simpleExoPlayer.stop();

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        adapter.stopListening();
        if (HomeVideoHolder.simpleExoPlayer != null) {
            HomeVideoHolder.simpleExoPlayer.stop();

        }

Reason : In adapter's onBindHolder()  always pass only one view for one item. so if you went to stop player using this process , it always stop last player of recyclerView.

Solution : You can use your Adapter onViewDetachedFromWindow(HomeVideoHolder holder)or onViewRecycled(HomeVideoHolder holder)or both to stop or Pause the simpleExoplayer. But onDestroy method you have to use recyclerView.setAdapter(null) before super call.




Post a Comment

0Comments
Post a Comment (0)