我尝试播放两个声音,但这是第二个声音结尾的错误。我尝试调试,但没有发现任何东西向我显示我的问题。
这是我的代码:
public class MainActivity extends Activity {
private MediaPlayer mp;
private int[] sound = new int[2];
private int currentSound = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sound[0] = R.raw.sound_one;
sound[1] = R.raw.sound_two;
mp = MediaPlayer.create(this, sound[currentSound]);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
if (currentSound < sound.length) {
currentSound++;
arg0 = MediaPlayer.create(getApplicationContext(),sound[currentSound]);
arg0.setOnCompletionListener(this);
arg0.start();
}
}
}
});
mp.start();
}
当第一声音完成,currentSound < sound.length
意味着0 < 2
同样第二声音结束后currentSound < sound.length
暗示1 < 2
和进一步将其递增currentSound++
所以现在它的值是2
。因此arg0 = MediaPlayer.create(getApplicationContext(),sound[currentSound]);
会抛出错误。sound[currentSound]
是sound[2]
。
移动currentSound++
前if (currentSound < sound.length)
@Override
public void onCompletion(MediaPlayer arg0) {
currentSound++;
if (currentSound < sound.length) {
arg0 = MediaPlayer.create(getApplicationContext(),sound[currentSound]);
arg0.setOnCompletionListener(this);
arg0.start();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句