0

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

I have stored an MP3 Music file in res/raw folder namely : mymusic.mp3

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.

Please someone help me, its much needed any suggestion, sample code would be helpful...

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 && mPlayer.isPlaying()){//If music is playing already
                mPlayer.stop();//Stop playing the music
            }
        }
    });
Chulbul Pandey
  • 506
  • 1
  • 8
  • 20

3 Answers3

3

This part has to be in EVERY activity's onPause:

Stop music automatically when user exit from app

public void onPause(){
super.onPause();
    Context context = getApplicationContext();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            if (!taskInfo.isEmpty()) {
              ComponentName topActivity = taskInfo.get(0).topActivity; 
              if (!topActivity.getPackageName().equals(context.getPackageName())) {
               StopPlayer();
                Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. MUSIC STOP", Toast.LENGTH_SHORT).show();
              }
            }
  }

This part has to be in EVERY activity's onResume:

Play music automatically when user resume the app

Public void onResume()
    {
       super.onResume();
     StartPlayer();
    }

You can put your Player functionalty in global class. where every class can call it's player. so your plyer will be remain same in whole application. & you can start or stop it. on Pause method it will detect wether user left this App or not. if user left from the app so u can stop it.

GlobalPlayer.class

public MediaPlayer mPlayer;

public void StartPlayer(){
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);


            // TODO Auto-generated method stub
            mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
            mPlayer.start();//Start playing the music

}
public void StopPlayer(){
            if(mPlayer!=null && mPlayer.isPlaying()){//If music is playing already
                mPlayer.stop();//Stop playing the music
          }
}
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
  • i am little bit confuse in above code where we are playing mp3, will you please show me complete code how my activity should look like, by taking example of single activity – Chulbul Pandey Aug 07 '13 at 04:55
  • i want to play and stop mp3 automatically – Chulbul Pandey Aug 07 '13 at 04:59
  • Check my Edited Answer. these are global methods. so you can start music in your first Main Activity where user start app. you should call startPlayer() in every activity's onResume(). so music will play automatically when user start your app.& above code should be put in every onPause(). so music will be stop automatically – Bhoomika Brahmbhatt Aug 07 '13 at 05:03
  • mPlayer.setLooping(). check http://developer.android.com/reference/android/media/MediaPlayer.html – Bhoomika Brahmbhatt Aug 07 '13 at 05:46
3

simply put your player.start() method in onResume() method and call player.stop () in onPause() method.Take a look at this Difference between onStart() and onResume()

Community
  • 1
  • 1
mchouhan_google
  • 1,775
  • 1
  • 20
  • 28
2

create service and put Mp3 start code in OnstartCommand(its a service class method you can override it) and whenever your you want to start mp3 just create Intent object and call StartService method and call StopSerice where you want to stop mp3 in application.

Mohit
  • 93
  • 7