Skip to content Skip to sidebar Skip to footer

Kivy Doesn't Play Sound File The First Time Play() Is Called

I run a simple Kivy app on Windows. A button executes following method from the Kivy docs (link) when pressed: def play_audio(self): sound = SoundLoader.load('output.wav')

Solution 1:

I think this thread will be useful. Try loading the sound once before the button is even pressed like so:

from kivy.core.audio import SoundLoader
from kivy.base import runTouchApp
from kivy.uix.button import Button
import time

sound = SoundLoader.load('output.wav')
sound.seek(0)

classMyLabel(Button):
    defon_release(self):
        start_time = time.time()
        self.play_sound()
        print("--- %s seconds ---" % (time.time() - start_time))

    defplay_sound(self):
        if sound:
            print("Sound found at %s" % sound.source)
            print("Sound is %.3f seconds" % sound.length)
            sound.play()

runTouchApp(MyLabel(text="Press me for a sound"))

The play_sound() function took about ten times less time to complete on my machine if you do sound.seek(0).

Post a Comment for "Kivy Doesn't Play Sound File The First Time Play() Is Called"