Search code examples
androidpythonkivyandroid-mediaplayerbackground-service

Kivy - Android background service doesn't play audio


I'm trying to play an audio file from the background service but it doesn't play it. The log says it does for a while and stop but the audio can't be heard.

The service is called when the app is paused:

from jnius import autoclass
service = autoclass('{}.Service{}'.format('kivyapp.music', 'S1'))
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(mActivity, argument)

/service/main.py

from jnius import autoclass
MediaPlayer = autoclass('android.media.MediaPlayer')
AudioManager = autoclass('android.media.AudioManager')

if __name__ == '__main__':
    nowPlaying = MediaPlayer()
    nowPlaying.setDataSource('music00.mp3')
    nowPlaying.prepare()
    nowPlaying.setLooping(False)
    nowPlaying.setVolume(0.5, 0.5)
    nowPlaying.start()

I would like to know if I'm doing something wrong. Thank you. Log.txt


Solution

  • I think the main problem was that, MediaPlayer didn't like me requesting for getCurrentPosition() every second. Also adding :sticky to my buildozer services = service:service.py seems to solve the problem.

    Following these steps also helped.