下面是我的媒體播放器代碼。我停止後它不會重新開始。如何解決這個問題?即使我在調用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");
}
}
我想你將不得不分享'MediaPlayer'代碼。我們不知道'pause()','stop()','isPlaying()'在做什麼......如果'start(View view)'最初運行良好,你可能想檢查你的'stop()'或'isPlaying()'方法。 – wns349
那些放在下面。這是唯一使用的類。請檢查代碼 – user3578666
的底部部分,只需在停止它之後使用mediaplayer.release()! – Mahfa