下面是我的媒体播放器代码。我停止后它不会重新开始。如何解决这个问题?即使我在调用 start 方法之前调用了 prepare 方法。还有,为什么我退出程序后音乐还没有停止播放。请指教。
public class MainActivity extends Activity {
Button play,pause,stop;
CheckBox c1,c2,c3;
MediaPlayer song1,song2,song3;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button)findViewById(R.id.button1);
pause=(Button)findViewById(R.id.button2);
stop=(Button)findViewById(R.id.button3);
c1=(CheckBox)findViewById(R.id.checkBox1);
c2=(CheckBox)findViewById(R.id.checkBox2);
c3=(CheckBox)findViewById(R.id.checkBox3);
song1=MediaPlayer.create(this, R.raw.finalcountdown);
song2=MediaPlayer.create(this, R.raw.invincible);
song3=MediaPlayer.create(this, R.raw.somewhereibelong);
builder=new AlertDialog.Builder(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void start(View view) throws IllegalStateException, IOException
{
if(c1.isChecked())
{
if(!song1.isPlaying())
{
song1.prepare();
song1.start();
}
else{
song1.start();
}
}
if(c2.isChecked())
{
if(!song2.isPlaying())
{
song2.prepare();
song2.start();
}
else{
song2.start();
}
}
if(c3.isChecked())
{
if(!song3.isPlaying())
{
song3.prepare();
song3.start();
}
else{
song3.start();
}
}
else{
builder.setMessage("Please mark a checkbox");
}
}
public void stop(View view)
{
if(c1.isChecked())
song1.stop();
else if (c2.isChecked())
song2.stop();
else if (c3.isChecked())
song3.stop();
else
builder.setMessage("Error message ");
}
public void pause(View view)
{
if(c1.isChecked())
{
song1.pause();
}
else if(c2.isChecked())
{
song2.pause();
}
else if(c3.isChecked())
{
song3.pause();
}
else
builder.setMessage("error message");
}
}
最佳答案
您需要将 song1.release() 和其他歌曲的 release 方法放在 stop 方法中。有关详细信息,请参阅媒体播放器的生命周期。 http://developer.android.com/reference/android/media/MediaPlayer.html
关于java - 停止后媒体播放器不启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23325769/