开发者

Stop media player

I am new in android and I have another (simple?) p开发者_开发技巧roblem. I don't know how to stop Media Player. This is my simple code:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        MediaPlayer mp;
        mp = MediaPlayer.create(this, R.raw.sauronsound);
        mp.setLooping(false);
        mp.start();
    @Override
    protected void onDestroy()
{

    // Stop play
        super.onDestroy();
        mp.stop();
    }
        }

After pressing back button app goes to my first activity but sound is on. When I leave an app it is on too. What should I do to turn off the sound?

As always excuse me for my poor English.

I solved the problem thanks to you Guys. Working code:

public class SauronEye extends Activity {
    private MediaPlayer mp;
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view);
        mp = MediaPlayer.create(this, R.raw.sound);
        mp.setLooping(false);
        mp.start();


     // Get instance of Vibrator from current Context
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(10000);
        }
@Override
    protected void onStop()
{
    // Stop play
    super.onStop();
    mp.stop();
}
    }

Is it correct (it works)? Thank you for helping me.


mp reference that you are using on onDestroy is different from the one you are using on onCreate. Move the MediaPlayer mp; line to outside the onCreate class.


Check this out http://developer.android.com/reference/android/media/MediaPlayer.html You can call stop or pause based on your requirement.When you select back button your onpause would be called, in that method you can call mp.stop(), onDestroy would be called only when activity is completely destroyed


onDestroy is only called when the activity is killed by the system. Rather than placing it in onDestroy, you should put it in onPause(), which is what's called whenever your activity is moved to the background but remains in memory. (Which is what happens with a back button being pressed or leaving the app)

@Override
protected void onPause() {
    super.onPause();
    mp.stop();
}


you can call the override implements source codes really easily and add them each into your code. All you need to do is right click the insertion point where you want them and click on Source->Override/Implement Methods. It will bring up a dialog box and you click on the methods you need, try using ondestroy, onpause, onstop. For your code and after it implements each of them just add the following to each.

    protected void onDestroy{
      super.onDestroy();
      mp.release();
   }

    protected void onStop{
      super.onStop();
      mp.stop();
   }

    protected void onPause{
      super.onPause();
      mp.pause();
   }

Also if you want a little more with you soundcodes you can try this link stealthcopters link or you can try this video series cornboyzAndroid

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜