[英]Kivy doesn't play sound file the first time play() is called
I run a simple Kivy app on Windows. 我在Windows上运行一个简单的Kivy应用程序。 A button executes following method from the Kivy docs ( link ) when pressed:
当按下按钮时,将从Kivy文档( link )执行以下方法:
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. 任何帮助是极大的赞赏。
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)
. 如果您执行
sound.seek(0)
那么play_sound()
函数在我的机器上完成的时间将减少大约十倍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.