title: How to implement completion listener in media player android image view
body: <p>I am working on project that plays background music when user slides through the images. And musci stops when users reaches the last image. It also starts playing when user backslides. However, my song is short and I want to implement completion listener so that when first music ends the second music starts from the raw folder. My main problem is that I don't know how to implement it in my mainactivity.java. I have also read android official guidelines but it didn't helped. Please provide some help along with explanation...</p>
  
  <p>My codes are the following</p>
  
  <p>Mainactivity.java</p>
  
  <pre><code>    import android.app.Activity;
      import android.content.Intent;
      import android.media.MediaPlayer;
      import android.os.Bundle;
      import android.support.v4.view.ViewPager;
      import android.support.v4.view.ViewPager.OnPageChangeListener;
      import android.view.Menu;
  
      import android.view.MenuItem;
      import android.widget.ShareActionProvider;
  
      public class MainActivity extends Activity {
  
          MediaPlayer oursong;
          ViewPager viewPager;
          ImageAdapter adapter;
  
          @Override
          public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
  
               oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
               oursong.seekTo(0);
               oursong.start();
  
               viewPager = (ViewPager) findViewById(R.id.view_pager);
               adapter = new ImageAdapter(this);
               viewPager.setAdapter(adapter);
  
  
               viewPager.setOnPageChangeListener(MyViewPagerListener);
          }
  
          private ShareActionProvider mShareActionProvider;
  
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
                  // Inflate menu resource file.
                  getMenuInflater().inflate(R.menu.activity_main, menu);
  
                  // Locate MenuItem with ShareActionProvider
                  MenuItem item = menu.findItem(R.id.menu_item_share);
  
                  // Fetch and store ShareActionProvider
                  mShareActionProvider = (ShareActionProvider) item.getActionProvider();
                  // Return true to display menu
                  return true;
          }
  
          // Call to update the share intent
          private void setShareIntent(Intent shareIntent) {
                  if (mShareActionProvider != null) {
                          mShareActionProvider.setShareIntent(shareIntent);
                  }
          }
  
          private int pos = 0;
          @Override
          protected void onPause() {
                  super.onPause();
  
                 if(oursong != null){
                     pos = oursong.getCurrentPosition();
                     oursong.release();
                     oursong = null;
                  }
          }
  
          @Override
          protected void onResume(){
                super.onResume();
               oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
               oursong.seekTo(pos); // You will probably want to save an int to restore here
               oursong.start();
          }
  
  
         private final OnPageChangeListener MyViewPagerListener = new OnPageChangeListener() {
  
                  @Override
                  public void onPageSelected(int pos) {
                    if (pos == adapter.getCount() - 1){
                       // adding null checks for safety
                       if(oursong != null){
                          oursong.pause();
                       }
  
                     } else if (!oursong.isPlaying()){ 
  
                      // adding null check for safety
                      if(oursong != null){
                          oursong.start();
                      }
                    }         
                  }
  
                  @Override
                  public void onPageScrolled(int arg0, float arg1, int arg2) {
                   // TODO Auto-generated method stub
  
                  }
  
                  @Override
                  public void onPageScrollStateChanged(int arg0) {
                  // TODO Auto-generated method stub
  
                  }
              };
  
       }
  </code></pre>