如何使用pyglet在mayavi动画中播放声音?

how to use pyglet to play sound in a mayavi animation?

我想在 mayavi 动画循环中使用 pyglet 播放声音,但我发现 pyglet 与必须在 mayavi 动画中使用的 'yield' 配合使用效果不佳。情况是,当声音播放和动画完成一次时,它无法开始新的循环,这是我的一些代码,有什么想法吗?

pyglet可以在for循环中播放声音,但不能使用yield

@mlab.animate(delay=delays)
def animate():
    f = mlab.gcf()
    while True:
        for i in range(frames_num): 
            # update sound
            sound = 'shiping/shiping_%d.wav'%i
            sound_adjust = pyglet.resource.media(sound, streaming=False)
            sound_adjust.play()

            # update scene
            print('Update scene >>', time.time())
            function_to_update_scene()
            # with out 'yield' it works well
            yield
animate()

其他模块建议也可以采纳。问题是我需要在20毫秒内快速更新声音。

我终于通过使用 winsound 模块解决了这个问题。使用

winsound.PlaySound(sound, winsound.SND_FILENAME | winsound.SND_ASYNC)

替换

sound_adjust = pyglet.resource.media(sound, streaming=False) 
sound_adjust.play()

异步播放定义的声音。当然你必须在一开始就import winsound