背景音乐pygame的不打(background music not playing in pyga

2019-10-20 14:14发布

当我运行的代码,它只能播放.wav文件

import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)

pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()

soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
pygame.mixer.music.stop()

有人知道我应该做什么来太扮演的background.ogg?

Answer 1:

删除这一行:

pygame.mixer.music.stop()

基本上,你在加载和播放背景噪声,然后立即停止它。 我也建议你创建一个main loop ,就像这样:

import pygame
from pygame.locals import *

# This makes sure that you're not importing the module.
if __name__ == "__main__":
    # Create surface
    size = width, height = (500, 400)
    # You don't need the extra arguments
    window = pygame.display.set_mode(size)
    # Do sound stuff
    pygame.mixer.music.load('background.ogg')
    pygame.mixer.music.play()

    soundObj = pygame.mixer.Sound('bird.wav')
    soundObj.play()
    # This is your main loop.
    running = True
    while running:
        # Check if escape was pressed

大多数时候人们只是检查是否Escape在自己的主循环被压(如果它被按下,他们成立running = False和退出程序)。



文章来源: background music not playing in pygame
标签: python pygame