如何使用Pyglet播放音频(在生成器循环中)?

时间:2019-08-20 10:47:51

标签: python python-3.x audio pyglet

pyglet版本-1.4.2。
Python-3.6.6
Ubuntu-18.04

代码示例:

fileSelectedHandler = (event) => {
  console.log(event.target.files[0]);
  this.setState({
      selectedFile: event.target.files[0]
  }, () => this.fileUploadHandler());
};

代码是根据documentation编写的:

登录控制台:

import pyglet
import time

pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')
source = pyglet.media.StaticSource(pyglet.media.load('explosion.wav'))

def my_playlist():
    while True:
        print(time.time())
        print(1)
        yield source


player = pyglet.media.Player()
player.queue(my_playlist())
player.play()

pyglet.app.run()

预期结果:

音频应与发生器返回的声音一起循环播放。

当前结果:

音频只播放一次。

问题:

我在这里做错了什么以及如何获得预期的结果?

1 个答案:

答案 0 :(得分:2)

不知道是否要尝试完成更多工作,但是如果循环需要的只是循环声音,则实际上不应使用任何类型的loop。而是使用指定的EOS_LOOP标志/触发器。

import pyglet
import time

pyglet.options['audio'] = ('openal', 'pulse', 'directsound', 'silent')
source = pyglet.media.StaticSource(pyglet.media.load('explosion.wav'))

player = pyglet.media.Player()
player.queue(source)
player.EOS_LOOP = 'loop'
player.play()

pyglet.app.run()

由于不建议使用它,因此您应该转而使用设置了循环标志的SourceGroup