title: MediaPlayer won't stop playing even if programmed to
body: <p>I made a simple class that handles everything related to sound. Has an add, play, stop, release and releaseAll. How it works is that you have to add a song and then call play passing the name of the song you added. Anytime you need to stop, just call the stop function and pass the song's name as parameter and it should stop. My issue is that it isn't stopping even though it goes through <code>stop()</code>. </p>
  
  <p>Sound class:</p>
  
  <pre><code>public class Sound
  {
      private Map&lt;String, MediaPlayer&gt; songs = new HashMap&lt;String, MediaPlayer&gt;();
  
      private MediaPlayer currentlyPlayingSong;
  
      public Sound() {}
  
      public void Add(int songId, String songName, Context context)
      {
          MediaPlayer song = MediaPlayer.create(context, songId);
  
          songs.put(songName, song);
      }
  
      public void Play(String name, boolean shouldLoop)
      {
          MediaPlayer songToPlay = songs.get(name);
  
          if ( songToPlay != currentlyPlayingSong &amp;&amp; songToPlay != null) 
      {
          currentlyPlayingSong = songToPlay;
  
          currentlyPlayingSong.start();
  
          currentlyPlayingSong.setLooping(shouldLoop);
      }
      }
  
      public void Stop(String name)
      {
          MediaPlayer songToStop = songs.get(name);
          if (songToStop != null)
          {
              songToStop.setLooping(false);
  
              songToStop.stop();
          }
      }
  
      public void Release(String name)
      {
          songs.get(name).release();
      }
  
      public void ReleaseAll()
      {
         LinkedList&lt;MediaPlayer&gt; _songs;
  
          _songs = (LinkedList)songs.values();
  
          for (int i = 0; i &lt; _songs.size(); i++)
          {
              _songs.get(i).release();
          }
      }
  }
  </code></pre>
  
  <p>On the activity's <code>OnCreate</code> I call <code>Add</code> then <code>Play</code>. Everything is fine until I try to call <code>Stop</code> from a fragment. Runs without any errors or exceptions, it simply doesn't stop.</p>
  
  <p>Activity:</p>
  
  <pre><code>public class Main extends ActionBarActivity
  {
     private Sound sound = new Sound();
  
     private static boolean isSoundOn = true;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
  
          isSoundOn = true;
  
          sound.Add(R.raw.drajamainmenueddited, "mainMenuSong", this);
  
          //endregion
  
          //Hide upper action bar
          getSupportActionBar().hide();
  
              if (isSoundOn)
                  sound.Play("mainMenuSong", true);
      }
  
  
      public void SetIsSoundOn(Boolean isOn)
      {
          isSoundOn = isOn;
      }
  
      public boolean GetIsSoundOn()
      {
         return isSoundOn;
      }
  
      public Sound GetSoundObj()
      {
          return sound;
      }
  }
  </code></pre>
  
  <p>Fragment:</p>
  
  <pre><code>public class MainMenuFragment extends Fragment {
  
      private ImageButton soundImgBtn;
  
      private FragmentConfig fragmentConfig;
  
      public MainMenuFragment()
      {
          fragmentConfig = new FragmentConfig();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState)
      {
          //region Initiators
          View  view = inflater.inflate(R.layout.fragment_main_menu, container, false);
  
          soundImgBtn = (ImageButton)view.findViewById(R.id.soundImgBtn);
          //endregion
  
          //region Listeners
  
          soundImgBtn.setOnClickListener(
                  new View.OnClickListener()
                  {
                      @Override
                      public void onClick(View v)
                      {
                          SoundImgBtnClick(v);
                      }
                  }
          );
          //endregion
  
          //Changes audio img
          if (((Main)getActivity()).GetIsSoundOn())
              soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode_off);
  
          else
              soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode);
  
          // Inflate the layout for this fragment
          return view;
      }
  
      private void SoundImgBtnClick(View v)
      {
          //if sound is on and clicked, turn off
          if (((Main)getActivity()).GetIsSoundOn())
          {
              ((Main)getActivity()).SetIsSoundOn(false);
  
              ((Main)getActivity()).GetSoundObj().Stop("mainMenuSong");
  
              soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode);
          }
  
          else
          {
              ((Main)getActivity()).SetIsSoundOn(true);
  
              ((Main)getActivity()).GetSoundObj().Play("mainMenuSong", true);
  
              soundImgBtn.setImageResource(android.R.drawable.ic_lock_silent_mode_off);
          }
      }
  
  }
  </code></pre>
  
  <p>What I'm trying to do is emulate a mute button. Once clicked all sounds should be muted.</p>
  
  <p>This is pretty much all I've coded, so far.</p>
  
  <p>Cheers.</p>