简体   繁体   English

我可以在Pygame中播放声音并同时做其他事情吗?

[英]Can I play sound in Pygame and do other stuff simultaneously?

I am making a little fantasy console in python and pygame and I have stumbled upon a problem. 我在python和pygame中制作了一个小小的幻想控制台,但我偶然发现了一个问题。 I was making a music editor that can make chiptune tunes, but when I play them using the pygame.mixer.Sound method, it pauses everything and plays the sound. 我当时正在制作一个音乐编辑器,可以进行芯片音乐调,但是当我使用pygame.mixer.Sound方法播放音乐时,它会暂停所有内容并播放声音。 For example, if I play a sound every time the hero picks up something, everything would freeze, it would play the sound, and then everything will resume. 例如,如果我每次英雄捡起东西时都播放声音,则所有内容都会冻结,它将播放声音,然后所有内容都会恢复。 This is not a big deal at first, but it makes doing something like background music impossible. 刚开始时这没什么大不了的,但是这使得做诸如背景音乐之类的事情变得不可能。 Is it possible to fix this? 有可能解决这个问题吗?

I have tried using the playsound library, but that freezes everything as well. 我试过使用playsound库,但这也会冻结所有内容。

def music(notes):

    note_list = ["c","c2","d","d2","e","e2","f","f2","g","g2","a","a2"]
    for i in note_list:
        exec("{} = pygame.mixer.Sound('res/sounds/{}.wav')".format(i,i))
    for i in notes:
        for j in i:
            exec("pygame.mixer.Sound.play({})".format(j))

        time.sleep(.2)

I expect the program to be able to play sound using the pygame.mixer.Sound method and do other things at the same time, but what actually happens is that everything freezes, plays the sound, and then everything resumes. 我希望程序能够使用pygame.mixer.Sound方法播放声音并同时执行其他操作,但是实际发生的情况是所有内容冻结,播放声音,然后所有内容恢复。

The sound will play in a different thread and will not halt the execution. 声音将在其他线程中播放,并且不会停止执行。 However, your loop contains time.sleep(.2) , which will halt the execution for 0.2 seconds. 但是,您的循环包含time.sleep(.2)它将暂停执行0.2秒。 And you're doing it for every note, which means that if you have a tune with 10 notes, it'll halt your program for 2 seconds. 而且您要为每个音符做这件事,这意味着如果您有10个音符的乐曲,它将使您的程序暂停2秒钟。

There are many different solutions to your specific problem which all depends on how you've structured the code where you call this function. 对于您的特定问题,有很多不同的解决方案,这都取决于您在调用此函数的代码的结构方式。 One solution might be to schedule events, and then play them when they appear in the event queue. 一种解决方案可能是安排事件,然后在事件出现在事件队列中时对其进行播放。 Another is to call the individual sounds when they are supposed to be played. 另一种是在应该播放单个声音时调用它们。 Both require a game loop, which I doubt you have since you're creating a console game. 两者都需要游戏循环,因为您正在创建主机游戏,所以我怀疑您有这个循环。

I would go the easy route and combine the sounds to the joint sound you actually want to play, instead of playing many different sounds in succession. 我会走一条简单的路线,将声音组合到您实际想要播放的联合声音中,而不是连续播放许多不同的声音。 You can use some program such as Audacity to edit sound files. 您可以使用Audacity之类的程序来编辑声音文件。 Then, you wouldn't even need a function. 然后,您甚至不需要功能。 You could just call pygame.mixer.Sound.play(zelda_pickup_tune) and everything would work without halting (assuming zelda_pickup_tune is a variable that holds a valid Sound object). 您可以只调用pygame.mixer.Sound.play(zelda_pickup_tune) ,一切都将不会停止(假设zelda_pickup_tune是一个保存有效Sound对象的变量)。


Also, don't use exec . 另外,不要使用exec It's slow and could potentially be a security risk if you were ever to let other people use your code (not in you're exact use currently, but it can easily happen if you do some minor changes later on). 如果您曾经让其他人使用您的代码,它的运行速度很慢,并且可能会带来安全风险(目前尚无确切用途,但是如果您稍后进行一些小的更改,则很容易发生)。 It's also a bit "magic" and it's not possible to rewrite this code in other languages. 这也有点“神奇”,并且不可能用其他语言重写此代码。

I would suggest a more standard approach to problems like this and use a dictionary. 我会建议采用更标准的方法来解决此类问题,并使用字典。

def music(notes):

    note_list = ["c","c2","d","d2","e","e2","f","f2","g","g2","a","a2"]
    sounds = {}  # Empty dictionary
    for i in note_list:
        sounds[i] = pygame.mixer.Sound('res/sounds/{}.wav'.format(i))  # Add note i as key and the sound as value.
    for i in notes:
        for j in i:
            sounds[j].play()  # Look up the sound with key i and play the sound.

        time.sleep(.2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM