Skip to content Skip to sidebar Skip to footer

Background Music Not Playing In Pygame

When i run the code it play only the .wav file import pygame, sys from pygame.locals import * pygame.init() DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32) pygame.mixer.m

Solution 1:

Remove this line:

pygame.mixer.music.stop()

Basically, you're loading and playing the background noise, and then immediately stopping it. I also suggest you create a main loop, like so:

import pygame
from pygame.localsimport *

# 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 = Truewhile running:
        # Check if escape was pressed

Most of the time people just check if Escape was pressed in their main loop (if it was pressed, they set running = False and exit the program).

Post a Comment for "Background Music Not Playing In Pygame"