我正在制作一个小脚本,当您按下键盘上的某个键时,该脚本会播放音频文件。现在,在切换多个库之后,我进入了pyglet + pynput组合。脚本工作正常,但是音频以无限循环播放,这是不希望的,并且音频会堆叠。我找不到关于此的任何想法吗?
* ffmpeg已安装
这是代码;
import string, random, pyglet
from pynput.keyboard import Key, Listener
pyglet.options['search_local_libs'] = True
spec_keys = ['Key.caps_lock', 'Key.shift', 'Key.delete', 'Key.backspace', 'Key.enter', 'Key.up', 'Key.left', 'Key.down', 'Key.right']
caps = ['Key.caps_lock', 'Key.shift']
delete = ['Key.delete', 'Key.backspace']
move = ['Key.up', 'Key.left', 'Key.down', 'Key.right']
ks1 = pyglet.media.load('key-press-1.mp3', streaming=False)
ks2 = pyglet.media.load('key-press-2.mp3', streaming=False)
ks3 = pyglet.media.load('key-press-3.mp3', streaming=False)
ks4 = pyglet.media.load('key-press-4.mp3', streaming=False)
ks_list = [ks1, ks2, ks3, ks4]
k_caps = pyglet.media.load('key-caps.mp3', streaming=False)
k_conf = pyglet.media.load('key-confirm.mp3', streaming=False)
k_del = pyglet.media.load('key-delete.mp3', streaming=False)
k_move = pyglet.media.load('key-movement.mp3', streaming=False)
def on_press(key):
x = str(key)
x = x.strip("'")
if x not in spec_keys:
ks_sound = random.choice(ks_list)
ks_sound.play()
elif x in caps:
k_caps.play()
elif x in delete:
k_del.play()
elif x in move:
k_move.play()
elif x == 'Key.enter':
k_conf.play()
with Listener(on_press = on_press) as listener:
listener.join()
pyglet.app.run()
答案 0 :(得分:0)
使用OpenAL解决了
pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')