1

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')
    if sound:
        print("Sound found at %s" % sound.source)
        print("Sound is %.3f seconds" % sound.length)
        sound.play()

The first time the button is pressed, it either plays about half a second of sound and then immediately stops or it's not playing anything at all. When I press the button again it plays the entire file as expected.

Why isn't it playing the file on the first button press and how do I get it to work properly?

Any help is greatly appreciated.

orangeInk
  • 1,360
  • 8
  • 16
  • Are you running the kivy eventloop? You are probably getting interrupted – user2722968 May 20 '17 at 18:55
  • I assume eventloop means MyApp().run() (sorry I'm new to this). If so than yes, it's running. I think if the problem was related to the main loop than it wouldn't make any sense for it to work the 2nd time I clicked the button (unless I'm missing something, which is entirely possible). – orangeInk May 20 '17 at 21:16

1 Answers1

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)

class MyLabel(Button):
    def on_release(self):
        start_time = time.time()
        self.play_sound()
        print("--- %s seconds ---" % (time.time() - start_time))

    def play_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).

Edvardas Dlugauskas
  • 1,469
  • 1
  • 12
  • 14
  • Sorry for the (very) late reply, I was away. I think I actually tried that (I found that thread during my research) and it didn't fix the issue. I'll try it again when I have the time. Also I just realized that I didn't install kivy.deps.gstreamer (https://kivy.org/docs/installation/installation-windows.html#kivy-dependencies) I'll check later if gstreamer fixes it. – orangeInk May 31 '17 at 13:20
  • Yeah, gstreamer might help. Let me know how it goes if you have the time, I'm very interested... – Edvardas Dlugauskas May 31 '17 at 13:59