网上找了一个简单的例程,代码如下
private MediaPlayer mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mp = MediaPlayer.create(this, R.raw.xjwq); mp.start(); mp.setLooping(true); }
这样是能够正常播放的。
然后本人就想做一个按钮来切换这个播放的声音。
本人这么做了,
private MediaPlayer mp1; private MediaPlayer mp2; private MediaPlayer mp3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mp1 = MediaPlayer.create(this, R.raw.xjwq); mp2 = MediaPlayer.create(this, R.raw.bg); mp3 = MediaPlayer.create(this, R.raw.xghzj); mp1.start(); mp1.setLooping(true); }
然后在按钮内执行
switch(mpIndex) { case 0: mp1.stop(); break; case 1: mp2.stop(); break; case 2: mp3.stop(); break; } mpIndex ++; if(mpIndex == 3) mpIndex = 0; if(mpIndex == 0) { mp1.start(); mp1.setLooping(true); } else if(mpIndex == 1) { mp2.start(); mp2.setLooping(true); } else { mp3.start(); mp3.setLooping(true); }
本人就想循环的播放资源文件里面的3个文件,这种方式报错
ERROR/MediaPlayer(9974): start called in state 64 ERROR/MediaPlayer(9974): error (-38, 0) ERROR/MediaPlayer(9974): Error (-38,0)
问一下怎么做?
解决方案
40
start前要记得prepare,例如这样
mpArr.get(mpIndex%3).stop(); mpArr.get(mpIndex%3).prepare(); mpIndex++;