0
import pygame, sys
from pygame.locals import *

pygame.init()
pygame.mixer.init()
DISPLAYSURF = pygame.display.set_mode((400,300))
pygame.display.set_caption('sound')
try:
    soundObj = pygame.mixer.Sound("badaboum.wav")
    soundObj.play()
    import time
    time.sleep(1)
    soundObj.stop()
except pygame.error:
    print('unable to open the file')


pygame.mixer.music.load(r"Zhouna - Don't Hurt (Zhouna Bootleg).mp3")
pygame.mixer.music.play(-1)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    DISPLAYSURF.fill((255, 255, 255))

    pygame.display.update()

This program will print 'unable to open file' and then play the music which load by pygame.mixer.music.load. if I don't use try-except, it will give me the error pygame.error: Unable to open file 'badaboum.wav', I wonder why I can use pygame.mixer.music.load to play the music in my game, but as soon as I use pygame.mixer.Sound it will give me the error. this problem troubled me for a few days. Thank you in advance.

0 Answers0