title: play video inside view pager
body: <p>I have to play 3 videos inside view pager and I am using PagerAdapter to go to different pages. 
  I am facing some problems : On the first time, the first video does not render, it shows a black screen.
  My second video runs only the first time when visited. Other videos run again when I visit that page. Following are the classes I am using</p>
  
  <pre><code>enter code here
  public class VideoPagerAdapter extends PagerAdapter {
  
      Context context;
      static int[] arrayvid;
      private VideoView videoView;
      LayoutInflater inflater;
      View layout;
      TextView message;
  
  
      public VideoPagerAdapter(Context context, int[] arrayvid) {
          this.context = context;
          this.arrayvid = arrayvid;
          inflater = (LayoutInflater) context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          ((ViewPager) container).removeView((View) object);
      }
  
      @Override
      public int getCount() {
          return arrayvid.length;
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          layout = inflater.inflate(R.layout.video_layout, container, false);
          videoView = (VideoView)layout.findViewById(R.id.video_view);
          message = (TextView)layout.findViewById(R.id.message);
  
          MediaController mediaController = new MediaController(context, false);
          mediaController.setAnchorView(videoView);
          ((ViewPager) container).addView(layout);
          return layout;
  
      }
  
      private Uri getPath(int id) {
          Log.i("Video", "num" + id);
  
          return Uri.parse("android.resource://com.example.videoviewpager/raw/video_" + id);
      }
  
      public void play(int position) {
          videoView.setVideoURI(getPath(position+1));
          videoView.requestFocus();
  
          videoView.start();
  
      }
  
      @Override
      public boolean isViewFromObject(View view, Object object) {
          return view==(View)object;
      }
  
  
  public class MainActivity extends AppCompatActivity {
  
      ViewPager viewPager;
      PagerAdapter adapter;
      private int[] ids = {R.raw.video_1, R.raw.video_2,R.raw.video_3};
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(
                  WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          setContentView(R.layout.activity_main);
  
          viewPager = (ViewPager) findViewById(R.id.video_pager);
  
          // Pass results to ViewPagerAdapter Class
          adapter = new VideoPagerAdapter(MainActivity.this,ids);
          viewPager.setAdapter(adapter);
  
          viewPager.setCurrentItem(0);
          //((VideoPagerAdapter) adapter).play(0);
  
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
              }
  
              @Override
              public void onPageSelected(int position) {
                  if(position==0){
                      ((VideoPagerAdapter) adapter).play(position);
  
  
                  }
                  if(position==1){
  
                      ((VideoPagerAdapter) adapter).play(position);
                  }
                  if(position==2){
                      ((VideoPagerAdapter) adapter).play(position);
                  }
              }
  //
              @Override
              public void onPageScrollStateChanged(int state) {
  
              }
          });
      }
  
      private Uri getPath(int id) {
          Log.i("Video", "num" + id);
  
          return Uri.parse("android.resource://com.example.videoviewpager/raw/video_" + id);
      }
  }
  </code></pre>