I have to play a mp3 file and when click on back button on the device then automatically the song should stop. So I tried below given method. But it is not working.
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");
}
}
the song is not stopping and the previous page cant display. Please tell me the solution.
Best Regards.
Thank you in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…