【问题标题】:Playing a sound once pygamepygame 播放一次声音
【发布时间】:2014-12-12 19:45:18
【问题描述】:

我正在尝试播放背景音乐,但音乐一直在播放。我尝试使用music.busy,但我无法弄清楚。我希望它检查声音/音乐是否正在播放,然后我希望它打印“音乐正在播放”。如果没有播放声音/音乐,我希望它开始播放一首新歌并循环播放。

def musica():
    if pygame.mixer.music.get_busy() == True:
        print("music is playing")
    if pygame.mixer.music.get_busy() == False:    
        music.play(loops=-1)

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    播放循环声音的一种简单方法是 winsound。请参阅 python 文档 (https://docs.python.org/3.4/library/winsound.html) 并将 PlaySound 中的参数 'flags' 设置为 winsound.SND_LOOP。

    【讨论】:

    • 我的任务只能使用 pygames
    • @Chan 你应该把它放在你的问题中! ;P
    • 把它放在标题“Playing a sound once pygame”中
    【解决方案2】:

    我最终这样做了,这更像是一种解决方法,但它确实有效:

    m = 0
    def musica():
        global m
        if m == 0:
            music.play(loops=-1)
            m = 1
    

    【讨论】:

      【解决方案3】:

      如果您希望在循环中的特定事件期间播放声音(例如打开菜单),则可以使用计时器仅播放一次声音。

      示例:

      sound = pygame.mixer.Sound('sound.wav')
          playSound = True
          playSound_counter = 0
          
          while:
                  playSound_counter += 1
                  if playSound_counter >= 11:
                      playSound = True
                      playSound_counter = 0
      
            if i.rect.collidepoint(mouse_position):
                      if playSound == True :
                          sound.play()
                          playSound = False
                      elif playSound_counter == 10:
                           playSound_counter = 0
      

      这本质上是两个计时器 - 一个每 11 秒将声音设置回 True,另一个在鼠标悬停在矩形上时取消它。

      “播放一次”是指当某个事件被触发时,能够以可控的方式再次播放

      【讨论】:

        猜你喜欢
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多