-1

i try to stop my sound in pressing back or home put it not work pleaze help me this is the code the sound play but i want the method to stop in pressing back or home.

 public class Youcha3 extends Activity {

    MediaPlayer mysound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youcha3);


        Button btsound=(Button)findViewById(R.id.dou3aa);
        btsound.setOnClickListener(new View.OnClickListener()   {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mysound=MediaPlayer.create(Youcha3.this, R.raw.dou3aa );
                 if (mysound.isPlaying()) {
                     mysound.pause();
                     mysound.release();
                 }
                 else{
                mysound.start();

                 }

            }

        });


    }
user2855148
  • 25
  • 1
  • 8
  • Do you want to stop your sound being played from when your `Activity` is paused? – Apoorv Nov 13 '13 at 11:56
  • possible duplicate of [Android Stop Background Music](http://stackoverflow.com/questions/9148615/android-stop-background-music) – Simon Nov 13 '13 at 11:57

2 Answers2

0

Try to add this code in your Activity.

@Override
protected void onStop() {
    if (mysound != null && mysound.isPlaying()) {
        mysound.stop();

    }
    super.onPause();
}

i think this may help you.

android_dev
  • 1,477
  • 12
  • 18
0

try this

// back button press
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if (mysound != null && mysound.isPlaying()) {
        mysound.stop();

    }
    super.onBackPressed();
}
// home button press
@Override
public void onAttachedToWindow() {
    // TODO Auto-generated method stub
    if (mysound != null && mysound.isPlaying()) {
        mysound.stop();

    }
    super.onAttachedToWindow();
}
Shijil
  • 2,226
  • 18
  • 33