我有三个声音,我按下按钮时试图播放它们。现在我有两个按钮,Next和Back按钮。下一个按钮将向前移动声音阵列,并在点击时显然播放它们,类似地,后退按钮将播放声音并以相反方向移动阵列。
这是我尝试使用的逻辑:
private int[] sounds = { R.raw.chase, R.raw.get_down,
R.raw.under_arrest };
private int pool = 0;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn_main);
Button btnNext = (Button) findViewById(R.id.button2);
Button btnBack = (Button) findViewById(R.id.button1);
mp = MediaPlayer.create(this, sounds[pool]);
// INITIALIZING OVER
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (pool < sounds.length - 1) {
pool++;
} else {
pool = 0;
}
mp.???????????
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (pool > 0) {
pool--;
} else {
pool = sounds.length - 1;
}
mp.???????????
}
});
}
}
我希望我很好地解释了我想要的东西,至少我试过了。如果我在澄清我的问题时失败了,请告诉我,我会为此进行编辑。
答案 0 :(得分:1)
每次您希望它发挥不同的结果时,您需要重新初始化MediaPlayer
。这是一个例子:
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (pool < sounds.length - 1) {
pool++;
} else {
pool = 0;
}
refreshMediaPlayerWithSound(pool);
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (pool > 0) {
pool--;
} else {
pool = sounds.length - 1;
}
refreshMediaPlayerWithSound(pool);
});
private void refreshMediaPlayerWithSound(int pool) {
if (mp != null) {
mp.release(); // release any previously played / held resources
}
mp = MediaPlayer.create(YourActivity.this, sounds[pool]);
mp.start();
}