2016-09-16 87 views
0

我遇到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") 
+0

Fadeout会在淡出时间之后停止音乐,如'pygame.mixer.music.stop()'。我很抱歉,但我无法复制您的问题。当我淡出音乐时,我仍然可以播放并在音乐淡出时停止播放声音。 –

+0

啊,drat。无论如何,非常感谢你的答复。 –

+0

如果音乐和声音文件是你愿意分享的东西,也许我们可以测试它们是否存在问题? –

回答

1

功能pygame.mixer.music.fadeout(3000)将使音量褪色3秒钟,然后停止音乐。音乐停止后,您需要重新开始播放。你的代码在淡出之后不应该播放任何音乐,而必须是因为你的程序中存在其他逻辑错误(也许你正在连续设置music_on = True?)。

这里是一些代码演示它是如何工作的:

import pygame 
pygame.init() 
pygame.display.set_mode((250, 200)) 
clock = pygame.time.Clock() 

pygame.mixer.music.load("music.wav") 
playing = pygame.mixer.music.get_busy() 

while 1: 
    clock.tick(30) 

    for event in pygame.event.get(): 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_f: 
       pygame.mixer.music.fadeout(3000) 
       print("Fading out") 
      elif event.key == pygame.K_p: 
       pygame.mixer.music.play(-1) 
       print("Play") 
      elif event.key == pygame.K_s: 
       pygame.mixer.music.stop() 
       print("Stop") 
     elif event.type == pygame.QUIT: 
      quit() 

    if playing != pygame.mixer.music.get_busy(): 
     playing = pygame.mixer.music.get_busy() 
     if playing: 
      print("Music is playing") 
     else: 
      print("Music is not playing") 

然而,如果“声音”你说的是pygame.mixer.Sound对象,它非常有意义。 pygame.mixer.music模块处理音乐播放,而pygame.mixer处理声音。这些是分开的(尽管紧密联系)。停止pygame.mixer停止声音而停止pygame.mixer.music停止音乐。

试验下面的代码。按1将开始音乐和声音,并立即停止pygame.mixer,按2将开始音乐和声音,并立即停止pygame.mixer.music。你应该在这里停止不同的事情。

import pygame 
pygame.init() 
pygame.display.set_mode((250, 200)) 
clock = pygame.time.Clock() 

pygame.mixer.music.load("music.wav") 
sound = pygame.mixer.Sound("sound.wav") 

playing = pygame.mixer.music.get_busy() 

while 1: 
    clock.tick(30) 

    for event in pygame.event.get(): 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_1: 
       sound.play(-1) 
       pygame.mixer.music.play(-1) 
       pygame.mixer.stop() 
      elif event.key == pygame.K_2: 
       sound.play(-1) 
       pygame.mixer.music.play(-1) 
       pygame.mixer.music.stop() 
     elif event.type == pygame.QUIT: 
      quit()