Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

overriding - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...