如何使用 pyglet API for sound 播放声音文件的子集,例如从 1 秒到 6 秒声音剪辑的 3.5 秒?
我可以加载声音文件并播放它,并且可以寻找所需间隔的开始,但我想知道如何在指示的点停止播放?
pyglet 似乎不支持设置停止时间。您的选择是:
这种方法似乎有效:与其手动轮询当前时间以停止播放,不如使用 pyglet 时钟调度程序在给定时间间隔后运行一次停止回调。这对于我的用例来说已经足够精确了 ;-)
player = None
def stop_callback(dt):
if player != None:
player.stop()
def play_sound_interval(mp3File, start=None, end=None):
sound = pyglet.resource.media(mp3File)
global player
player = pyglet.media.ManagedSoundPlayer()
player.queue(sound)
if start != None:
player.seek(start)
if end != None and start != None:
pyglet.clock.schedule_once(stop_callback, end-start)
elif end != None and start == None:
pyglet.clock.schedule_once(stop_callback, end)
player.play()