[Python] Python-Kivy: SoundLoader.load crashes app

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página