title: How to stop playing video on view pager swipe in android?
body: <p>I am using View Pager showing image and video, I am able to show image and video properly but I have problem, when I swipe for video, then video is playing, but I swipe next or previous then video is still playing on just next or previous screen but when I move two slide next or previous then video is being stop, but why not on next or previous slide.</p>
  
  <p>I search it more but I did not get any solution, any help will be appreciable. Thanks in advance.</p>
  
  <p>Here is my adapter code:</p>
  
  <pre><code>public class SelectedMediaViewPagerAdapter extends PagerAdapter {
  private final Context context;
  private final LayoutInflater layoutInflater;
  public ArrayList&lt;MediaItem&gt; mediaItems;
  PlayerView playerView;
  SimpleExoPlayer player;
  
  public String path = "";
  
  public SelectedMediaViewPagerAdapter(Context context, ArrayList&lt;MediaItem&gt; mediaItems) {
  
      this.mediaItems = mediaItems;
      this.context = context;
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      DefaultTrackSelector trackSelector = new DefaultTrackSelector();
      player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
  
  }
  
  @Override
  public int getCount() {
      return mediaItems.size();
  }
  
  @Override
  public boolean isViewFromObject(View view, Object object) {
      return (view == object);
  }
  
  @Override
  public Object instantiateItem(final ViewGroup container, int position) {
      RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(R.layout.item_selected_media_view_pager, container, false);
      RelativeLayout vidView= layout.findViewById(R.id.vidView);
      ImageView imgView = layout.findViewById(R.id.imgView);
      if (mediaItems.get(position).isVideo()) {
          imgView.setVisibility(View.GONE);
          vidView.setVisibility(View.VISIBLE);
          String video_url;
          playerView = layout.findViewById(R.id.player_view);
          final ProgressBar progressBar = layout.findViewById(R.id.progresbar_video_play);
          progressBar.setVisibility(View.VISIBLE);
  
          playerView.setPlayer(player);
  
          DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                  Util.getUserAgent(context, context.getResources().getString(R.string.app_name)));
          // This is the MediaSource representing the media to be played.
          MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                  .createMediaSource(Uri.parse(mediaItems.get(position).getPath()));
          // Prepare the player with the source.
          player.prepare(videoSource);
          player.setPlayWhenReady(false);
          player.addListener(new Player.DefaultEventListener() {
              @Override
              public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                  if (playWhenReady) {
                      progressBar.setVisibility(View.GONE);
                  }
                  super.onPlayerStateChanged(playWhenReady, playbackState);
              }
          });
      }else {
          // path = drawable.get(drawable.size() - position - 1);
          imgView.setVisibility(View.VISIBLE);
          vidView.setVisibility(View.GONE);
          Glide.with(context).load("file://" + mediaItems.get(position).getPath()).into(imgView);
      }
      container.addView(layout);
      return layout;
  }
  
  
  
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
      container.removeView((RelativeLayout) object);
  }
  </code></pre>
  
  <p>}</p>