我想创建一个图像按钮的方法,当它点击背景音乐停止和图像按钮改变另一个图像,当再次按下它将返回,因为它是在第一次和重播音乐,我试图布尔时这是真的,音乐开始,当它是错误的音乐,但它不工作! 另外我怎么可以让另一个活动播放或停止音乐取决于主要活动?如何在ImageButton上点击另一个活动中的声音?
public class MainActivity extends AppCompatActivity {
MediaPlayer mp ;
ImageButton SoundButton;
ImageButton NoSoundButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SoundButton = new ImageButton(this);
NoSoundButton = new ImageButton(this);
/*---------Image Buttons--------*/
SoundButton=(ImageButton) findViewById(R.id.sound);
SoundButton.setVisibility(View.GONE);
NoSoundButton=(ImageButton) findViewById(R.id.nosound);
NoSoundButton.setVisibility(View.VISIBLE);
/*---------Media Player--------*/
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.aud);
mp.setLooping(true);
mp.start();
}
public void nosound(View view) {
SoundButton.setVisibility(View.VISIBLE);
NoSoundButton.setVisibility(View.INVISIBLE);
mp.stop();
mp.prepareAsync();
}
public void sound(View view) {
SoundButton.setVisibility(View.INVISIBLE);
NoSoundButton.setVisibility(View.VISIBLE);
mp.start();
}
}
莫非你提供给我们你到目前为止使用的代码吗? –
我们必须知道:您使用什么来播放音乐?你如何改变你的照片?你是否设置了一个onclicklistener等。 –
编辑你的问题,并插入代码 –