title: One public media player class to be used across all activities
body: <p>Updated.</p>
  
  <p>After the recommendations, I decided to run media player in a new thread. Because I need Media Player only when activities are on the screen. Here is the new code:</p>
  
  <p>First, my public SingleMP (Media Player) class used in multiple classes:</p>
  
  <pre><code>import android.content.Context;
  import android.media.MediaPlayer;
  import android.net.Uri;
  
  public class SingleMP implements Runnable
  {
      public static MediaPlayer mp;
      private static Context context;
      private static Uri uri;
  
      public SingleMP(Context context, Uri uri){
      this.context= context;
      this.uri= uri;
      }
  
      @Override
      public void run(){
          try {
              if (mp != null) {
                  mp.stop();
                  mp.reset();
                  mp.release();
                  mp = null;
              }
  
              mp = MediaPlayer.create(context, uri);
              mp.start();
          } catch (Exception e) {
              if (mp != null) {
                  mp.stop();
                  mp.reset();
                  mp.release();
                  mp = null;
              }
              e.printStackTrace();
              mp = MediaPlayer.create(context, uri);
              mp.start();
          }
      }
  
      // Called in OnDestroy of used class.
      public static void mpstop()
      {
          if (mp != null) {
              mp.stop();
              mp.reset();
              mp.release();
              mp = null;
          }
      }
  }
  </code></pre>
  
  <p>And an example of using it in another Java class:</p>
  
  <pre><code>public class MainMenu
  {
      private Uri uri;
      private Runnable MPthread;
  
      public void onCreate(Bundle savedInstanceState)
      {
          RadioButton rbtnA = (RadioButton) findViewById(R.id.radio0);
          rbtnA.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  // Assign a sound from raw folder.
                  uri =Uri.parse("android.resource://"+getPackageName()+"/raw/nice");
                  MPthread = new SingleMP(MainMenu.this, uri));
                  new Thread(MPthread).start();               
                  }
               }
          });
  
          RadioButton rbtnB = (RadioButton) findViewById(R.id.radio1);
          rbtnB.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  // Assign a sound from raw folder.
                  uri =Uri.parse("android.resource://"+getPackageName()+"/raw/morning");
                  MPthread = new SingleMP(MainMenu.this, uri));
                  new Thread(MPthread).start();
                  }
               }
           });
  
      }
  
  
      @Override
      protected void onDestroy() {
              super.onDestroy();
  
      if(MPthread!=null) {
          SingleMP.mpstop();
      }
  
      }
  }
  </code></pre>
  
  <p>What do you think? It seems that my UI works a little bit smoother.</p>