Audio doesn't stop when closing my game on android

Hi. I’m working on an android game but i have a problem:
In the game there is a soundtrack. when you exit from the game using the exit botton that i created, i don’t have problem but if you close the app using home button the audio continues playing until it finish
I searched everywhere but i can’t find the solution

A few questions for clarification:

  1. Does the exit button call Processing’s exit() function?
  2. What does the home button do? What source code does it call?
  3. How are you playing the soundtrack, ie what code are you using. Does the code that you are using for this have a ‘stop’ function? Did you call the ‘stop’ function when the home button is hit?

The exit button that i made first of all, stops every soundstracks with soundtrack.stop() than it exit().
The button that i made works. the problem is the home button of the mobile. there’s no way to know if the user is pressing the home button because once pressed, the game doesn’t close. it remains opened in background

Immagine

to play the soundtrack i’m using soundtrack.play()

  1. You could try to trap the hit on the home button and then call soundtrack.stop from there. I’ve had difficulty doing this, and I get the impression that the operating system developers have intentionally made this difficult in order to discourage its use. Others here may have good code for it.

  2. You could try switching media players; another player might shut off when the home button is hit. For example, in my tests the Android MediaPlayer seems to stop when the home button is hit. An example of use of the MediaPlayer was recently published in this forum and may be found here:https://discourse.processing.org/t/accessing-sound-in-android/31793/5

You could insert your own music file in the data folder and then hit the home button to verify that it indeed does shut off.

1 Like

Hi @Marco0030, Are you using the Processing Sound library? Do you know if there is an Android Service set up to play audio in the background?
What else have you tried? Are there certain methods you have overridden that are not working? What permissions does your app have? Are you checking if your main Activity is in the background?
Any other information will help.

Thank you it works!!!

1 Like