Mark Rees
Mark Rees

Reputation: 287

Gluon Home Button Events

I'm using Gluon and have an audio player. When I call the audio and play it all works normally until I press the home button. What I want it to do is to stop the music not to continue playing as it does now. Currently I've tried using an event listener to capture the event and stop the music but it's not recognising the event, I'm thinking either I have assigned the wrong keycode or it simply doesn't work like that. I have a setOnHiding method in the view already and that only works on the back button. I've also tried setOnHidden and setOnCloseRequest as well with no luck. The event listener is below.

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

    }

Upvotes: 1

Views: 154

Answers (1)

jns
jns

Reputation: 6952

Add a Listener to 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)).

Upvotes: 1

Related Questions