Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override back button in android

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.

like image 627
Ramakrishna Avatar asked Feb 11 '11 13:02

Ramakrishna


1 Answers

I don't know if this is your problem, but when you call onBackPressed(); in your onkeydown, you are not returning, so the parent.onkeydown is also called, and the 'normal' back is just being 'executed'.

Insert a return statement there so you will not do the normal function from the parent class.

@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 true;
    }
    return super.onKeyDown(keyCode, event);
}
like image 175
Nanne Avatar answered Nov 15 '22 07:11

Nanne