使用pygame(python)播放声音

使用pygame(python)播放声音,python,pygame,playback,Python,Pygame,Playback,到目前为止,我有一个程序,根据特定的按键播放单个声音。每次按键都会根据所按下的键成功播放正确的声音,但当另一个键的下一个声音开始播放时,声音会被切断。我想知道如何让我的程序完整地播放每个键的声音,即使当按下另一个键并开始播放新声音时(我希望声音同时播放)。我正在使用pygame和键盘库 以下是我用来播放按键声音的功能: # 'keys' refers to a dictionary that has the key press strings and sound file names store

到目前为止,我有一个程序,根据特定的按键播放单个声音。每次按键都会根据所按下的键成功播放正确的声音,但当另一个键的下一个声音开始播放时,声音会被切断。我想知道如何让我的程序完整地播放每个键的声音,即使当按下另一个键并开始播放新声音时(我希望声音同时播放)。我正在使用pygame和键盘库

以下是我用来播放按键声音的功能:

# 'keys' refers to a dictionary that has the key press strings and sound file names stored as key-value pairs.
key = keyboard.read_key() 
def sound(key):
    play = keys.get(key, 'sounds/c2.mp3')
    pygame.mixer.init()
    pygame.mixer.music.load(play)
    pygame.mixer.music.play()

如果您需要更多的上下文,请告诉我,我将更新我的问题。

在pygame中,您可以使用频道同时播放多个音轨。请注意,通道仅支持wav或ogg文件

下面是一些示例代码。按1-3键开始播放曲目。您也可以多次启动同一曲目

import pygame

lst = [
'Before The Brave - Free.wav',   # key 1
'Imagine Dragons - Demons.wav',  # key 2
'Shinedown-How Did You Love.wav' # key 3
]

pygame.init()
size = (250, 250)
screen = pygame.display.set_mode(size)

pygame.mixer.init()
print("channel cnt",pygame.mixer.get_num_channels()) # max track count (8 on my machine)

while True: 
    pygame.time.Clock().tick(10) 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            exit()
    idx = 0
    keys = pygame.key.get_pressed()
    if keys[pygame.K_1]: idx = 1
    if keys[pygame.K_2]: idx = 2
    if keys[pygame.K_3]: idx = 3
    if (idx):
       ch = pygame.mixer.find_channel() # find open channel, returns None if all channels used
       snd = pygame.mixer.Sound(lst[idx-1])  # create sound object, must be wav or ogg
       if (ch): ch.play(snd)  # play on channel if available

在pygame中,您可以使用频道同时播放多个音轨。请注意,通道仅支持wav或ogg文件

下面是一些示例代码。按1-3键开始播放曲目。您也可以多次启动同一曲目

import pygame

lst = [
'Before The Brave - Free.wav',   # key 1
'Imagine Dragons - Demons.wav',  # key 2
'Shinedown-How Did You Love.wav' # key 3
]

pygame.init()
size = (250, 250)
screen = pygame.display.set_mode(size)

pygame.mixer.init()
print("channel cnt",pygame.mixer.get_num_channels()) # max track count (8 on my machine)

while True: 
    pygame.time.Clock().tick(10) 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            exit()
    idx = 0
    keys = pygame.key.get_pressed()
    if keys[pygame.K_1]: idx = 1
    if keys[pygame.K_2]: idx = 2
    if keys[pygame.K_3]: idx = 3
    if (idx):
       ch = pygame.mixer.find_channel() # find open channel, returns None if all channels used
       snd = pygame.mixer.Sound(lst[idx-1])  # create sound object, must be wav or ogg
       if (ch): ch.play(snd)  # play on channel if available

我使用了你的一些代码,效果很好。唯一的问题是我需要八个以上的频道。如何增加可用频道的数量?如果我不能,是否有任何方法在程序返回“无”后保持程序运行,或者我必须找到一种方法清除通道,以便始终有一个可用通道?答案更新为检查无。如果频道已满,您需要另一个声音,则需要停止其中一个频道并替换声音文件。我使用了您的一些代码,效果良好。唯一的问题是我需要八个以上的频道。如何增加可用频道的数量?如果我不能,是否有任何方法在程序返回“无”后保持程序运行,或者我必须找到一种方法清除通道,以便始终有一个可用通道?答案更新为检查无。如果频道已满,您需要另一个声音,则需要停止其中一个频道并更换声音文件。