title: How to make the video in view pager stop playing when the next page is scrolled?
body: <p>First, I play a <code>video</code> in the first <code>view pager</code>, then when i scroll to the next page, the <code>video</code> continues to playing. The video stops when i come to the third page. Is it any coding to stop the <code>video</code> or the activity by the time I scroll to the next page? Eager for expert help!!! </p>
  
  <p><strong>Below is my MainActivity which has the viewPager and FragmentPageAdapter</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      ArrayList&lt;Item&gt; arrayList;
      List&lt;Fragment&gt; fragmentList;
      MyAdapter adapter;
      MyPagerAdapter adapterViewPager;
      int passPosition = -1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
  
          passPosition = getIntent().getIntExtra("key",-1);
          arrayList = new ArrayList&lt;&gt;();
          adapter = new MyAdapter(this,arrayList);
          fragmentList = addItem();
          adapterViewPager = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
          vpPager.setAdapter(adapterViewPager);
          vpPager.setPageTransformer(true, new CubeOutTransformer());
          if(passPosition != -1){
              vpPager.setCurrentItem(passPosition);
          }
      }
  
      private List&lt;Fragment&gt; addItem(){
          fragmentList = new ArrayList&lt;&gt;();
  
          arrayList.add(new Item("a",0,0));
          fragmentList.add(Fragment1.newInstance("a"));
          arrayList.add(new Item("b",0,0));
          fragmentList.add(Fragment1.newInstance("b"));
          arrayList.add(new Item("c",0,0));
          fragmentList.add(Fragment1.newInstance("c"));
          arrayList.add(new Item("d",0,0));
          fragmentList.add(Fragment1.newInstance("d"));
          Toast.makeText(MainActivity.this,"add successfully",Toast.LENGTH_SHORT).show();
          return fragmentList;
      }
  
      public class MyPagerAdapter extends FragmentStatePagerAdapter {
  
          List&lt;Fragment&gt; fragmentList;
  
          public MyPagerAdapter(FragmentManager fragmentManager,List&lt;Fragment&gt; fragmentList) {
              super(fragmentManager);
              this.fragmentList = fragmentList;
          }
  
          // Returns total number of pages
          @Override
          public int getCount() {
              return this.fragmentList.size();
          }
  
          // Returns the fragment to display for that page
          @Override
          public Fragment getItem(int position) {
             Fragment fm;
              fm = this.fragmentList.get(position);
  
              return fm;
          }
  
          @Override
          public int getItemPosition(Object object) {
              return POSITION_NONE;
          }
  
          // Returns the page title for the top indicator
          @Override
          public CharSequence getPageTitle(int position) {
              return "Pag " + position;
          }
      }
  }
  </code></pre>
  
  <p><strong>2.Below is my Fragment1 where has the videoView</strong></p>
  
  <pre><code>public class Fragment1 extends Fragment {
      public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
      private VideoView videoView;
  
      public static final Fragment1 newInstance(String message)
      {
          Fragment1 f = new Fragment1();
          Bundle bdl = new Bundle(1);
          bdl.putString(EXTRA_MESSAGE, message);
          f.setArguments(bdl);
          return f;
      }
  
      // Store instance variables based on arguments passed
      /*@Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          text = getArguments().getString("someInt");
          //title = getArguments().getString("someTitle");
      }*/
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment1, container, false);
          String message = getArguments().getString(EXTRA_MESSAGE);
          TextView textView = (TextView)view.findViewById(R.id.text);
          videoView = (VideoView) view.findViewById(R.id.video_view);
  
          textView.setText(message);
          videoView.setVideoURI(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.video));
          videoView.stopPlayback();
          getVideo();
          return view;
      }
      public void getVideo(){
          videoView.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  videoView.start();
                  return true;
              }
          });
          videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
                  videoView.start();
              }
          });
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/oJe4T.png" rel="nofollow noreferrer">Picture1 : Video played in first page</a></p>
  
  <p><a href="https://i.stack.imgur.com/RHr8R.png" rel="nofollow noreferrer">Picture2 : when i swipe to 2nd page, the video is still playing and visible! Why???</a></p>