title: Android: how to get a MediaPlayer object from the MainActivity in a Fragment?
body: <p>I'm developing an android app and i'm using <code>NavigationDrawer</code>, there is only one activity (<code>MainActivity</code>) and Fragments.
  I have a radio live stream button on the <code>ActionBar</code> so i made an object of <code>MediaPlayer</code> in the <code>MainActivity</code> for the live stream button and it's working fine, now i have some sound files in one of the fragments, i want to get the <code>mediaPlayer</code> object that i made in the MainActivity in order to use it. Note that the live stream button is always visible at the top of the application so if i'm in a fragment that has a sound file i want to be able to listen to the sound file and if i clicked on the live stream button the sound file will be paused and live stream will be on.</p>
  
  <p>Here's my code:
  This is MainActivity (only the relative part):</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
     private static final String RADIO_STREAM_URL= "http://198.178.123.23:8662/stream/1/;listen.mp3";
     MediaPlayer mediaPlayer;
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
  
         getMenuInflater().inflate(R.menu.main, menu);
         mediaPlayer = new MediaPlayer();
         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         try {
             mediaPlayer.setDataSource(RADIO_STREAM_URL);
         } catch (IOException e) {
             e.printStackTrace();
         }
  
         return true;
     }
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
  
      if (item != null )
      {
          if(item.getItemId() == android.R.id.home){
              if (mDrawerLayout.isDrawerOpen(Gravity.END)){
                  mDrawerLayout.closeDrawer(Gravity.END);
              } else {
                  mDrawerLayout.openDrawer(Gravity.END);
              }
          }else if(item.getItemId() == R.id.action_settings){
              try {
                  mediaPlayer.prepareAsync();
              } catch (IllegalStateException e) {
                  e.printStackTrace();
              }
  
  
              mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                  @Override
                  public void onPrepared(MediaPlayer mediaPlayer) {
                      if(mediaPlayer.isPlaying()){
                          mediaPlayer.stop();
                          Log.e("ANA Radio","was playing");
                      }else{
                          Log.e("ANA Radio","was not playing");
  
                          mediaPlayer.start();
                      }
                  }
              });
          }
  
      }
      return false;
  }
  </code></pre>
  
  <p>Now how to get the <code>mediaPlayer</code> object in a Fragment ?</p>