package net.androidbootcamp.guessinggame;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class setting extends Activity {
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Button s =(Button) findViewById(R.id.btnons);
s.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(setting.this,R.raw.meow);
mp.start();
}
});
Button ss =(Button) findViewById(R.id.btnoffs);
ss.setOnClickListener(new OnClickListener(){
How can I code the Off Button.This is my code of ON button.I don’t know how to stop the sound when I click the other button(off).
I have two buttons, the ON and OFF button,a normal game settings of sound.
I don’t know what’s next. I want to stop the stop everytime I click the OFF button.
This should probably be sufficient to handle the
MediaPlayerpresent in your code:Changes towards your code:
mpis now a member of the classsetting, calledmediaPlayer. Previously, you’d have a hard time trying to work on thempin the scope of the “off button”‘sOnClickListener, but that’s solved now. Also wrapped the code instopMediaPlayer()with a null check to avoid aNullPointerException.