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 sto
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.
Use services.. Do something like that-
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop,buttonNext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonNext = (Button) findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonNext:
Log.d(TAG, "onClick: next Page");
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}
Look at this post for full code-Androidhub4you
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()
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
}
}