gpt4 book ai didi

python - Pyglet无限循环播放音频

转载 作者:行者123 更新时间:2023-12-03 01:27:52 65 4
gpt4 key购买 nike

我正在制作一个小脚本,当您按键盘上的某个键时,该脚本会播放音频文件。现在,在切换了多个库之后,我进入了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()

最佳答案

使用OpenAL解决了

pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')

关于python - Pyglet无限循环播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59283286/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com