有两种声音sound1和sound2,我想通过按下按钮btnSTART开始声音,而正在播放声音时另一个按钮被按下(btn2),声音变为sound2。我怎样才能做到这一点。
我正在尝试以下方法:
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btnSTART = (Button) findViewById(R.id.button3);
Button btnSTOP = (Button) findViewById(R.id.button4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btnSTOP.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mp.pause();
}
});
btnSTART.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mp.start();
mp.setLooping(true);
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
mp = MediaPlayer.create(this, R.raw.light_switch_off);
break;
case R.id.button2:
mp = MediaPlayer.create(this, R.raw.light_switch_on);
break;
}
}
}
BTN
答案 0 :(得分:0)
试试这个:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button3:
// Play voicefile
if(mp != null) {
mp.stop();
mp.release();
} else
mp = MediaPlayer.create(this, R.raw.light_switch_off);
mp.start();
break;
case R.id.button4:
// Stop MediaPlayer
if(mp != null) {
mp.stop();
mp.release();
} else
mp = MediaPlayer.create(this, R.raw.light_switch_on);
mp.start();
break;
}
}