I'm trying to stop my music when the user leaves the app or clicks the home button. My application plays music even if it is in the background. How can I stop the music if the user clicks the home button? If the user returns, the music is played again.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//toggle off and on :
MusicButton = (ToggleButton) findViewById(R.id.toggleButton);
MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (MusicButton.isChecked() && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
});
In your Activity:
Check for two states:
on pause state: When you press home button
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
on resume state: When you come back to your screen
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
Since you are in Activity you can override onResume and onStop methods
and then play/pause your app.
#Override
protected void onResume() {
super.onResume();
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
#Override
protected void onStop() {
super.onStop();
mediaPlayer.pause();
}
Related
How to stop play sound by press button back or button home
Please Help me :
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button_play);
final MediaPlayer mP = MediaPlayer.create(MainActivity.this,R.raw.music);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mP.isPlaying()) {
mP.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
}
);
}}
Pause media player in onPause(); Method
#Override
protected void onPause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
super.onPause();
}
Hope It will work for you.
For more information Refer MediaPlayer LifeCycle
to stop music player
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
to stop play sound by press button back or button home
in Activity you need to stop music on onDestroy and onBackPressed
#Override
protected void onDestroy() {
super.onDestroy();
stopPlaying();
}
#Override
public void onBackPressed() {
super.onBackPressed();
stopPlaying();
}
I'm trying to play music in all activities in my app but when I switch between mainactivity and another activity music stopped please someone help me without using Service
MainAcivity :
ToggleButton MusicButton;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//code
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
if(MusicButton.isChecked()){
mediaPlayer.pause();
}
else
mediaPlayer.start();
}
}
I don't know what I can do in the other activity.
You can pass the current seek position using extras, but the Music may stop while switching between activities.
I suggest that you use service then in each activity onPause event you check if you are switching to another activity in your application or not, if not send some broadcast to the service to stop playing music.
I'm trying to play music when the user clicks in the MUSIC ON toggle button, and music will pause when he clicks in MUSIC OFF. I also need to play music when opening the app. This is my code but doesn't work:
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vapor);
mediaPlayer.start();
.....
}
MusicButton = (ToggleButton)findViewById(R.id.toggleButton);
MusicButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(MusicButton.isChecked()){
mediaPlayer.start();
}
else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
#Override
public void onPause() {
mediaPlayer.stop();
mediaPlayer.release();
super.onPause();
}
#Override
public void onResume() {
mediaPlayer = MediaPlayer.create(this, R.raw.vapor);
mediaPlayer.setLooping(false);
mediaPlayer.start();
super.onResume();
}
Error log:
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1384)
at com.myapp$MainActivity$2.onClick(MainActivity.java:80)
first,you should not use setOnClickListener() on ToggleButton,ToggleButton has function setOnCheckedChangeWidgetListener to listene the status of compent
second,about you error,maybe your ToggleButton is unchecked,when you clicked it, ToggleButton become checked and invoke you code mediaPlayer.start();,but you start mediaPlayer at onCreate() already,you can check it.
MediaPlayer player = MediaPlayer.create(context, R.raw.background);
player.start();
The Code above is part of the current code.
When I press the home key, MediaPlayer does not stop playing.
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (KeyCode == KeyEvent.KEYCODE_BACK) {
System.exit(0);
return true;
}
}
return super.onKeyDown(KeyCode, event);
}
I've implemented the code shown above, for when you press the Back key.
Do you think that the above code is correct? Appreciate any help.
Just implement below methods for this,
#Override
protected void onPause() {
super.onPause();
if(player != null)
player.stop();
}
#Override
protected void onStop() {
super.onStop();
if(player != null)
player.stop();
}
#Override
protected void onDestroy() {
super.onDestroy();
if(player != null)
player.stop();
}
Please try with this solution:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
For more information, please read this post: Android MediaPlayer Stop and Play
If your media player is running on the activity you created, you can call player.stop() in onStop() method.
Edit:
Your implementation is OK, but it only overrides the back key only. You have to override the home key also to stop the player on pressing the home key. (but, use onStop() instead as it calls when the user interface is hidden.)
Do not use System.exit(), use finish() instead.
If you using finish() method (which destroys the activity), make sure that you have released the MediaPlayer resources by calling player.release() and nullifying the player. (i.e. player = null)
I have media player in main activity. And sound start with this activity. I want when new activity start audio stop and when I press back from next activity and back to main activity audio starts. Please help me.
Code-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.test);
mp.start();
mp.setLooping(true);
addListenerOnButton();
}
public void addListenerOnButton()
{
boy1 = (Button) findViewById(R.id.boy1);
boy1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
startActivity(new Intent(Main.this, Boy1.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
mp.stop();
}
#Override
public void onBackPressed() {
super.onBackPressed();
mp.stop();
finish();
}
#Override
public void onResume()
{
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
mp.stop();
}
}
try this way:onPause()
#Override
protected void onPause() {
super.onPause();
try{
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}catch(Exception we){
we.printStackTrace();
}
}
And onResume()
#Override
protected void onResume() {
super.onResume();
try{
mediaPlayer.start();
}catch(Exception we){
we.printStackTrace();
}
}
You could use the pause() method available in MediaPlayer class. This would be useful:
http://developer.android.com/reference/android/media/MediaPlayer.html
You can do this using mediaPlayer.pause();