简体   繁体   English

有人可以向我解释这种方法如何工作吗?

[英]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. 我有一个应用程序,当您按下屏幕上的按钮时,它会播放mp3文件。 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. 我要这样做,以便在播放mp3时按返回按钮或主页按钮,歌曲会停止播放。 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(); 我想做一个if语句,检查是否按下了后退/主页按钮,并且如果两种情况都成立,则调用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. 我的问题是我不知道如何在if语句中调用onKeyDown并检查是否按下了后退或主页按钮。

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: **以下是初始化和播放mp3文件的代码:

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. return语句告诉OS您是否已自行处理按键。 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. 就像这样,当您返回true时,操作系统就会知道您已经成功处理了该事件,并且不需要为该特定按键做任何事情。

However, a more standard way of detecting the back key press is by overriding onBackPressed(). 但是,检测后退按键的更标准方法是重写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 ? 您是在谈论UI按钮或键盘键吗? onKeyDown is to catch keyboard key presses. onKeyDown用于捕获键盘按键。

If you want to catch button click you need to register a clickListener to your buttons. 如果要捕获按钮单击,则需要向按钮注册clickListener。

If you want to catch keys you need to register an OnKeyListener in your view. 如果要捕获键,则需要在视图中注册OnKeyListener

If you want to catch the special back button then it is sufficient to override the onBackPressed() method of your activity. 如果要捕获特殊的后退按钮,则只需重写活动的onBackPressed()方法即可。 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. onKeyDown是一个公共方法,当在设备上按下按钮时,Android系统会自动调用该方法。 You do not need to call it directly, but you do need to @Override it. 您无需直接调用它,但需要@Override The KeyEvent that the system passes to it is described here . KeyEvent ,该系统通过将其描述这里 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. 编辑:我还没有足够的代表直接评论@Swayam的解决方案,但是我相信在onPause方法中实现您的解决方案还会在方向改变的情况下停止播放音乐,这可能是不可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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