title: How to play mp3 continuosly when application starts and stop when user close app in background
body: <p>I am writing an app in which i am allowing user to view images and select one of them to set an WALLPAPER, and in this i also want to play an mp3 when user starts an App and stop that mp3 when user close application</p>
  
  <p>I have stored an MP3 Music file in <em>res/raw</em> folder namely : <strong>mymusic.mp3</strong></p>
  
  <p>I know how to play and stop MP3 music by using click on button, but don't know how to play mp3 in background continuosly, play when user start an app and stop when user close an app.</p>
  
  <p>Please someone help me, its much needed any suggestion, sample code would be helpful...</p>
  
  <pre><code>MediaPlayer mPlayer;
  MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
  
  Button buttonPlay;
  Button buttonStop;
  
  buttonPlay = (Button) findViewById(R.id.play);
      buttonPlay.setOnClickListener(new OnClickListener() {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
              mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
              mPlayer.start();//Start playing the music
          }
      });
  
  buttonStop = (Button) findViewById(R.id.stop);
      buttonStop.setOnClickListener(new OnClickListener() {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
              if(mPlayer!=null &amp;&amp; mPlayer.isPlaying()){//If music is playing already
                  mPlayer.stop();//Stop playing the music
              }
          }
      });
  </code></pre>