Tuesday 6 August 2013

How to play mp3 continuosly when application starts and stop when user close app in background

How to play mp3 continuosly when application starts and stop when user
close app in background

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
}
}
});

No comments:

Post a Comment