我必須播放一個mp3文件,當點擊設備上的後退按鈕時,自動停止播放歌曲。所以我嘗試了下面給出的方法。但它不起作用。覆蓋後退按鈕在android
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.audioplaying);
play=(ImageView)findViewById(R.id.play);
stop=(ImageView)findViewById(R.id.stop);
songid=(TextView)findViewById(R.id.songid);
status=(TextView)findViewById(R.id.status);
String s=Songs.song;
status.setText("Please Wait....!");
mp=new MediaPlayer();
try{
mp.setDataSource(s);
mp.prepare();
}
catch(Exception ex){
Log.e("Exception",ex.getMessage());
}
Log.e("Status","Song is going to Start");
mp.start();
start=true;
Log.e("Status","Song was Started");
status.setText("Playing...!");
songid.setText(s);
play.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
Log.d("CDA", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
audioStreamer.stop();
audioStreamer.getMediaPlayer().stop();
if(start)
{
mp.stop();
start=false;
}
else{
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
}
Intent setIntent = new Intent(AudioPlay1.this,Songs.class);
startActivity(setIntent);
finish();
return;
}
@Override
public void onClick(View v) {
if(v.equals(play)){
try{
mp.prepare();
}
catch(Exception ex){Log.e("Exception in onclick",ex.toString());}
mp.start();
start=true;
Log.e("Status","Song was Started again");
status.setText("Playing...!");
}
if(v.equals(stop)){
mp.stop();
start=false;
Log.e("Status","Song was stopped");
status.setText("Song was Stopped");
}
}
歌曲不停止,前一頁不能顯示。請告訴我解決方案。
最好的問候。
預先感謝您。
你想創建一個音樂播放器,還是什麼? – 2011-02-11 13:31:48
我嘗試在媒體播放器MediaPlayer mp – Ramakrishna 2011-02-11 13:33:54