Gluon 主页按钮事件

Gluon Home Button Events

我正在使用 Gluon 并有一个音频播放器。当我调用音频并播放时,一切正常,直到我按下主页按钮。我想让它做的是停止音乐,不再像现在这样继续播放。目前,我已经尝试使用事件侦听器来捕获事件并停止播放音乐,但它没有识别出该事件,我在想我分配了错误的键码,或者它根本无法像那样工作。我已经在视图中有一个 setOnHiding 方法,它只适用于后退按钮。我也尝试过 setOnHidden 和 setOnCloseRequest,但没有成功。事件侦听器如下。

    if (event.getCode().equals(KeyCode.HOME) && KeyEvent.KEY_PRESSED == event.getEventType()) {
    if (service1 != null) {
        service1.backPressed();

    }

LifecycleEvent.Pause 添加侦听器:

 Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> stopPlayback());

The PAUSE event is fired when an application loses focus (e.g. on Android / iOS when the focus is switched out of view (but still running in the background)).