在python之前,声音已经完成了吗? [英] Having sounds complete before the next, in python?

查看:74
本文介绍了在python之前,声音已经完成了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python/pygame中,我希望重复某个wav文件(由pygame.mixer.Sound("foo.wav").play()循环读取,并让它们一个接一个地播放,最好是在最后一个播放完成后或默认延迟(1500毫秒有效)

到目前为止,我的解释是:

    for x in range(0, 5):
        pygame.mixer.Sound("foo.wav").play()

但是,播放时,它会一次全部播放.

使用pygame.time延迟会导致窗口挂起,就像tkinter .after(1500)一样,我似乎找不到一种直接的方法来使用库或python甚至是播放某个内容的示例我打算延迟一些音调.我可以将pygame替换为更标准"的Python音频插件,或者如果需要骇客单独使用pygame混音器来完成这项工作,则可以在按钮按下时使用线程.

如果需要,一些方便的参考资料: http://www.pygame.org/docs/ref/music.html https://docs.python.org/2/library/tkinter.html

解决方案

最简单的方法似乎是通过Tkinter的方法.

self.sound_queue = [pygame.mixer.Sound(s) for s in ('foo.wav', 'bar.ogg', 'baz.mp3')]

def play_queue(self, q, num=0)
    sound = q[num]
    duration = int(sound.get_length() * 1000)
    sound.play()
    if num < len(q)-1:
        self.root.after(duration, self.play_queue, q, num+1)

self.play_queue(self.sound_queue)

您还可以查看 pygame.mixer.Channel.queue() pygame.mixer.Channel.set_endevent ,因为这些可能是完成此类操作的预期方式.

In Python/pygame, I desire to repeat a certain wav file (read by pygame.mixer.Sound("foo.wav").play() in a loop, and have them play one after another, preferably after last has completed or by a default delay (1500ms works)

So far, paraphrasing, I have this:

    for x in range(0, 5):
        pygame.mixer.Sound("foo.wav").play()

When it plays, however, it plays all at once.

Using pygame.time for a delay hangs the window, as does tkinter .after(1500), and I cannot seem to find a straight-forward means to do this with either libraries or python or even an example of something playing a few tones with delay as I intend. I could substitute pygame with a more 'standard' audio drop-in for Python, or potentially use threading if it comes to it for the button presses, if it requires hackery to do such a thing with the pygame mixer alone.

Some handy references if needed: http://www.pygame.org/docs/ref/music.html https://docs.python.org/2/library/tkinter.html

解决方案

The most straightforward way to do this seems to be to get the length of each sound and play the next one after the given time has elapsed, via Tkinter's after method.

self.sound_queue = [pygame.mixer.Sound(s) for s in ('foo.wav', 'bar.ogg', 'baz.mp3')]

def play_queue(self, q, num=0)
    sound = q[num]
    duration = int(sound.get_length() * 1000)
    sound.play()
    if num < len(q)-1:
        self.root.after(duration, self.play_queue, q, num+1)

self.play_queue(self.sound_queue)

You could also look at pygame.mixer.Channel.queue() and pygame.mixer.Channel.set_endevent, as those are probably the intended way to do this sort of thing.

这篇关于在python之前,声音已经完成了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆