简体   繁体   中英

Can someone explain to me how this method works?

I have an app that when you press a button on the screen it plays an mp3 file. I want to make it so that if I press the back button or home button while the mp3 is playing, the song stops playing. I want to make a if statement that checks if the back/home button was pressed or not, and if either of the cases were true then call the myMediaPlayer.stop();

So far the only method that checks if the back button was pressed or not that I have found so far is:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

My problem is that I have no idea how in my if statement to call onKeyDown and check whether the back or home button were pressed.

Where do I find out what the keycode is and the event are that I need to pass to it?

**Here is the code that initializes and plays the mp3 file:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final MediaPlayer mp = new MediaPlayer();
Button b = (Button) findViewById(R.id.default_activity_button);

b.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        try {
            mp.reset();
            AssetFileDescriptor afd;
            afd = getAssets().openFd("sound.mp3");
            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mp.prepare();
            mp.start();

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();enter code here
        }
    }

});

Is there any way to do something like this:

if(mp.isPlaying() == true && /* pseudo code*/BackButtonPressed == true/* pseudo code*/)
        {

        }

This function is called automatically by the OS when any key down event occurs. So, you don't need to call it explicitly yourself. You just need to check for the key press that you need to determine. And the return statement tells the OS if you have handled the key press yourself or not. As in, when you return true the OS knows that you have successfully handled the event and it does need not do anything for that particular key press.

However, a more standard way of detecting the back key press is by overriding onBackPressed().

Something like this,

@Override
    public void onBackPressed() {
       //stop playing mp3
    }

A more specific solution in your case would be to stop playing the music as soon as your Activity goes into Pause when the back key or the home button is pressed.

@Override
protected void onPause(){
        //stop playing mp3
        super.onPause();
    }

Are you talking about UI buttons or keyboard keys ? onKeyDown is to catch keyboard key presses.

If you want to catch button click you need to register a clickListener to your buttons.

If you want to catch keys you need to register an OnKeyListener in your view.

If you want to catch the special back button then it is sufficient to override the onBackPressed() method of your activity. So something like:

public void onBackPressed() {
  if (mp.isPlaying()) {
    // do want you want
    mp.stopPlaying();
  } else {
    // default behavior ?
    super.finish(); // dismiss the current activity
  }
}

should work.

onKeyDown is a public method called automatically by the Android system when a button is pressed on the device. You do not need to call it directly, but you do need to @Override it. The KeyEvent that the system passes to it is described here . It contains a number of constants that refer to specific keys. I am not at home to test this, but I think you want something like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (((keyCode == KeyEvent.KEYCODE_BACK)||(keyCode == KeyEvent.KEYCODE_HOME))&&(mp.isPlaying())) {
        mp.stop();
    }
    return super.onKeyDown(keyCode, event);
}

Edit: I don't have sufficient rep yet to comment on @Swayam's solution directly, but I believe implementing your solution in the onPause method would also stop the music in the event of an orientation change which would probably be undesirable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM