title: Stopping Exoplayer onSwipe of ViewPager
body: <p>I am using a <code>ViewPager</code> with single fragment instance in which I am showing <code>Media</code> files like Images, Videos, Audio. </p>
  
  <p>I have implemented <code>ExoPlayer</code> for handling <code>Video</code> &amp; <code>Audio</code> files. And <code>Glide</code> for images. </p>
  
  <p>To avoid the memory leaks I am releasing the <code>ExoPlayer</code> object like this in <code>ItemViewerFragment.java</code> : </p>
  
  <pre><code> private void releasePlayer() {
          if (player != null) {
              player.release();
              player = null;
              trackSelector = null;
              simpleExoPlayerView.setPlayer(null);
          }
      }
  
      @Override
      public void onStart() {
          super.onStart();
          if (player == null &amp;&amp; currentGalleryModel != null) {
              initializePlayer();
          }
      }
  
      @Override
      public void onResume() {
          super.onResume();
          if (player == null &amp;&amp; currentGalleryModel != null) {
              initializePlayer();
          }
      }
  
      @Override
      public void onPause() {
          super.onPause();
          releasePlayer();
      }
  
      @Override
      public void onStop() {
          super.onStop();
          releasePlayer();
      }
  </code></pre>
  
  <p>And in <code>onViewCreated()</code> I am initializing view like this :</p>
  
  <pre><code> @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          if (currentGalleryModel.isVideo() || currentGalleryModel.isAudio()) {
  
              simpleExoPlayerView.setVisibility(View.VISIBLE);
              imageView.setVisibility(View.GONE);
  
              initializePlayer();
          } else if (currentGalleryModel.isImage() || currentGalleryModel.isGif()) {
              simpleExoPlayerView.setVisibility(View.GONE);
              imageView.setVisibility(View.VISIBLE);
  
  
              Glide.with(this)
                      .load(currentGalleryModel.getFilePath())
                      .placeholder(android.R.color.black)
                      .fitCenter()
                      .diskCacheStrategy(DiskCacheStrategy.NONE)
                      .into(imageView);
  
          }
      }
  </code></pre>
  
  <p>I am using <code>FragmentStatePagerAdapeter</code>. This is the <code>getItem</code> method :</p>
  
  <pre><code>@Override
  public Fragment getItem(int position) {
      return ItemViewerFragment.newInstance(mItems.get(position));
  }
  </code></pre>
  
  <p>I am not able to detect the <code>onPause</code> of the fragment on first swipe of <code>Viewpager</code>. On second swipe <code>video/audio</code> files are stopped playing.</p>
  
  <p>In activity I have tried adding <code>.addOnPageChangeListener</code> :</p>
  
  <pre><code>@Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
       try {
          ((ItemViewerFragment)mAdapter.getItem(mPreviousPos)).imHiddenNow();
          } catch (Exception e) {
              e.printStackTrace();
          }
          mPreviousPos = position;
      }
  </code></pre>
  
  <p>And in <code>ItemViewerFragment.java</code> :</p>
  
  <pre><code>public void imHiddenNow(){
        releasePlayer();
      }
  </code></pre>
  
  <p>Still <code>video/audio</code> keeps on playing.</p>
  
  <p>Here is a <a href="https://drive.google.com/file/d/0B_er4uRW9U5hX3pfTGJDanhEclU/view?usp=sharing" rel="nofollow noreferrer">Video Link</a> to the Screencast.</p>
  
  <p><a href="https://github.com/nieldeokar/ExoVideoPlayer" rel="nofollow noreferrer">Demo project GitHub link</a>.</p>