我正在开发类似MP3的应用程序。我现在面临的问题是,在播放声音之前,后退按钮无效。如果我没有播放任何声音,它说,“不幸的是,MP3停止了”帮助我。
(抱歉英语不好)
这是我的代码
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.headerfooter);
ScrollView scrollable_contents = (ScrollView) findViewById(R.id.scrollablecontents);
getLayoutInflater().inflate(R.layout.filxml, scrollable_contents);
btnReturn1 = (Button)findViewById(R.id.back);
btnReturn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
title = (TextView)findViewById(R.id.title);
title.setText("Surah Al FIL (105)");
play = (Button)findViewById(R.id.play);
stop = (Button)findViewById(R.id.stop);
pause = (Button)findViewById(R.id.pause);
play1=(ImageButton)findViewById(R.id.fil1);
play2=(ImageButton)findViewById(R.id.fil2);
play3=(ImageButton)findViewById(R.id.fil3);
play4=(ImageButton)findViewById(R.id.fil4);
play5=(ImageButton)findViewById(R.id.fil5);
play.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
play_all();
}
});
stop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
stopall();
}
});
pause.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
pause();
}
});
play1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
go();
}
});
play2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
go1();
}
});
play3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
go2();
}
});
play4.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
go3();
}
});
play5.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
go4();
}
});
}
public void play_all()
{
stopPlaying();
if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil_full);
mp.setLooping(true);
mp.start();
}
public void stopall()
{
if(mp.isPlaying())
mp.stop();
}
public void pause()
{
if(mp.isPlaying())
mp.pause();
else
mp.reset();
}
public void go()
{
stopPlaying();
if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil1);
mp.setLooping(true);
mp.start();
}
public void go1()
{
if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil2);
mp.start();
}
public void go2()
{
if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil3);
mp.start();
}
public void go3()
{if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil4);
mp.start();
}
public void go4()
{
if(mp != null && mp.isPlaying())
{
mp.stop();
mp.release();
mp = null;
}
mp = MediaPlayer.create(this,R.raw.fil5);
mp.start();
}
//@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.stop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.stop();
}
private void stopPlaying()
{
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}