我遇到music.fadeout()
问题。据我了解,音乐将在淡出后停止。那为什么呢,下面的代码是否允许在淡出期间播放声音,但不能播放之后?在淡出之后,这些声音混合在一起; mixer.get_busy()
返回True。music.fadeout()似乎会在完成淡出后屏蔽声音
如果我用music.stop()
代替.fadeout()
音乐播放停止后播放声音。
我在这里错过了什么?
if not game_over:
if music_on:
pygame.mixer.music.fadeout(3000)
#pygame.mixer.music.stop()
music_on = False
在此期间我做这个来解决它:
if not game_over:
if music_on:
pygame.mixer.music.fadeout(3000)
fadeout_start = ticks
#pygame.mixer.music.stop()
if fadeout_start + 3000 < ticks:
pygame.mixer.music.stop()
music_on = False
编辑
我的具体问题特德,我不认为在你的答案覆盖(可能我只是密集)如下所示:
当我开始音乐(K_1),然后停止它(K_2),然后播放声音对象(K_5) ,mixer.get_busy为True,声音可听地播放。但是,如果我重复上述步骤但淡出音乐(K_3)而不是使用K_2停止,则mixer.get_busy为True,但不会发出可听声音。
Pydocs似乎表明音乐在淡出之后会以与简单的music.stop()相同的方式停止。
import pygame
from pygame.locals import *
import sys
pygame.init()
pygame.mixer.init()
pygame.display.set_mode((200,200))
pygame.display.set_caption("Sound tester")
clock = pygame.time.Clock()
pygame.mixer.music.load("theme.mid")
sound = pygame.mixer.Sound("missile.wav")
playing = pygame.mixer.get_busy()
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_1:
pygame.mixer.music.play(-1)
elif event.key == K_2:
pygame.mixer.music.stop()
elif event.key == K_3:
pygame.mixer.music.fadeout(2000)
elif event.key == K_4:
pygame.mixer.music.stop()
elif event.key == K_5:
sound.play(-1)
elif event.key == K_6:
pygame.mixer.stop()
music_playing = pygame.mixer.music.get_busy()
if music_playing:
print("music playing")
else:
print("no music")
sound_playing = pygame.mixer.get_busy()
if sound_playing:
print("Sound playing")
else:
print("no sound")
Fadeout会在淡出时间之后停止音乐,如'pygame.mixer.music.stop()'。我很抱歉,但我无法复制您的问题。当我淡出音乐时,我仍然可以播放并在音乐淡出时停止播放声音。 –
啊,drat。无论如何,非常感谢你的答复。 –
如果音乐和声音文件是你愿意分享的东西,也许我们可以测试它们是否存在问题? –