I am trying to play a sound in my kivymd app. For this, I am loading a sound using kivy's SoundLoader upon initializing my app class. If I do this, my app crashes once I start it (It opens the app window completely transparent. I cannot close it and don't get any error messages.). When testing SoundLoader isolated in a python script, I can run it, although the sound does not play. Any advice on this?
I am on Ubuntu 24.04.2 LTS using Python 3.12.3 and kivy 2.3.1.
Crashing app example:
from kivy.lang import Builder
from kivymd.app import MDApp
import os
from kivy.core.audio import SoundLoader
KV = '''
MDScreen:
md_bg_color: self.theme_cls.backgroundColor
MDLabel:
text: "test"
halign: "center"
'''
class MyApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.sound = SoundLoader.load(os.path.abspath("alarm_sound.wav"))
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Olive"
return Builder.load_string(KV)
MyApp().run()
"Functioning" python script example:
from kivy.core.audio import SoundLoader
sound = SoundLoader.load('alarm_sound.wav')
if sound:
print("Sound found at %s" % sound.source)
print("Sound is %.3f seconds" % sound.length)
sound.play()
EDIT:
The problem seems to be related to kivymd. When using SoundLoader within a normal kivy app, it appears to work.
Normal kivy app example:
from kivy.lang import Builder
from kivy.app import App
import os
from kivy.core.audio import SoundLoader
KV = '''
Screen:
Label:
text: "test"
halign: "center"
'''
class MyApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.sound = SoundLoader.load(os.path.abspath("alarm_sound.wav"))
def build(self):
return Builder.load_string(KV)
MyApp().run()