arraylistandroid-viewpagerexoplayerandroid-pageradapter

PagerAdapter always getting called two times in ViewPager


I am trying to make a slider between TouchImageView and PlayerView (Exoplayer) but I am unable to catch up with certain issues that are persisting even after several changes. All the suggestions and answers are welcome. Pardon my questioning skills and please let me know if more inputs are needed for your analysis. Kindly also let me know if there is any other alternative to successfully meet my expectations of properly implementing views smoothly in ViewPager.

Problem description:-

Issues related to click on view :-

  1. When the image is clicked, the audio of next video (if any) starts playing in background.
  2. The same issue is with PlayerView. When the video thumbnail is clicked, the audio of clicked video as well as next video plays together.

Issues related to slider :-

  1. When an we slide and reach to an image preceding to a video, the audio starts playing in background. However, after sliding once toward video and sliding again in forward or backward direction from video for once, the audio stops. But this issue persists after viewing more than one images in forward or backward direction of video.

Attempts made by me to solve this issue :-

  1. I tried to use playerView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {...}) method in PagerAdapter to handle player states while sliding between views. Unfortunately, I was unable to grasp to use different player states.
  2. I also tried to use viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {...} method in StatusViewer class.

StatusViewer Java class (Setting PagerAdapter class object inViewPager) :-

modelFeedArrayList = (ArrayList<File>) getIntent().getSerializableExtra("modelFeedArrayList");
position = intent.getIntExtra("position", 0);
ImageSlideAdapter imageSlideAdapter = new ImageSlideAdapter(this,modelFeedArrayList,position);
viewPager.setAdapter(imageSlideAdapter);
viewPager.setCurrentItem(position);
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            File currentFile = modelFeedArrayList.get(position);
            String filePath = currentFile.toString();
            if (filePath.endsWith(".jpg") || currentPage == position){
                currentPage = position;
                ImageSlideAdapter.player.pause();
            }
            else {
                currentPage = position;
                ImageSlideAdapter.player.play();
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

ImageSliderAdapter (PagerAdapter) (code mentioned below is inside instantiateItem):-

File currentFile = modelFeedArrayList.get(position);
String filePath = currentFile.toString();

if (currentFile.getAbsolutePath().endsWith(".mp4")) {
    statusImageView.setVisibility(View.GONE);
    playerView.setVisibility(View.VISIBLE);

    player = new ExoPlayer.Builder(context).build();

    MediaItem mediaItem = MediaItem.fromUri(filePath);
    player.addMediaItem(mediaItem);

    playerView.setPlayer(player);
    player.prepare();
    playerView.setBackgroundColor(context.getResources().getColor(android.R.color.black));

        playerView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {

                Log.d("Filepath", filePath);
                Log.d("Position", "" + position);
            }

            @Override
            public void onViewDetachedFromWindow(View v) {

                if (filePath.endsWith(".jpg") || currentPage == position || modelFeedArrayList.get(currentPage).getAbsolutePath().endsWith(".jpg")){
                    currentPage = position;
                    player.pause();
                    Objects.requireNonNull(playerView.getPlayer()).pause();
                }
                else {
                    player.release();
                    Objects.requireNonNull(playerView.getPlayer()).release();
                }
            }
        });


    } else {
        playerView.setVisibility(View.GONE);
        statusImageView.setVisibility(View.VISIBLE);
        Glide.with(context).load(modelFeedArrayList.get(position)).into(statusImageView);

        statusImageView.setBackgroundColor(context.getResources().getColor(android.R.color.black));
    }

    Objects.requireNonNull(container).addView(itemView);

    return itemView;
}

@Override
public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {

    container.removeView((ConstraintLayout) object);
}

Solution

  • Thank you StackOverflow community for viewing this question. I resolved the above issue by below mentioned modifications :-

    1. Changes in ImageSliderAdapter (PagerAdapter) :-

    -> Below mentioned code was added in onViewAttachedToWindow(View v) :-

    if (filePath.endsWith(".jpg") || currentPage == position || modelFeedArrayList.get(currentPage).getAbsolutePath().endsWith(".jpg")){
         currentPage = position;
         player.pause();
         Objects.requireNonNull(playerView.getPlayer()).pause();
    }
    else {
         player.pause();
         Objects.requireNonNull(playerView.getPlayer()).pause();
         if (filePath.endsWith(".mp4")){
              player.pause();
              Objects.requireNonNull(playerView.getPlayer()).pause();
         }
         else {
              player.play();
              Objects.requireNonNull(playerView.getPlayer()).play();
         }
    }
    

    -> Below mentioned code was added in onViewDetachedFromWindow(View v) :-

    if (filePath.endsWith(".mp4")){
         player.release();
         Objects.requireNonNull(playerView.getPlayer()).release();
    }
    

    -> player.play() was added after player.prepare().

    1. Changes in StatusViewer Java class :-

    -> The below changes cured the issue of player malfunctioning and player's play state and release state. I used the smoothScroll: false in setCurrentItem.

        viewPager.setCurrentItem(position,false);