覆盖早在Android的按钮早在、按钮、Android

2023-09-12 08:12:10 作者:(づ ̄3 ̄)づ

我要扮演一个MP3文件,当点击设备上的后退按钮,然后会自动将歌曲应该停止。所以,我想给定的方法如下。但它不能正常工作。

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

    }

这首歌没有停止和previous页面着显示屏。请告诉我的解决方案。

the song is not stopping and the previous page cant display. Please tell me the solution.

最好的问候。

感谢你在前进。

推荐答案

我不知道这是不是你的问题,但是当你调用 onBack pressed(); 在你的onkeydown,你不回来,所以 parent.onkeydown 也叫和正常回到刚刚被执行。

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

插入一个return语句存在,所以你不会从父类做的正常功能。

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