0

So im making a python game with python and I'm using mixer to play audio, but when I run the game this error shows

pygame 2.1.2 (SDL 2.0.16, Python 3.8.12)
Hello from the pygame community. https://www.pygame.org/contribute.html
music started playing....
Traceback (most recent call last):
  File "main.py", line 22, in <module>
    mixer.music.play('walk the dinosaur.mp3')
TypeError: an integer is required (got type str)"

this is my code

import pyflakes
import pygame
import os
from pygame import mixer

pygame.init()

loc = 1
input 
wtd = 'walk the dinosaur.mp3'
mixer.init()

#Load audio file
mixer.music.load('walk the dinosaur.mp3')

print("music started playing....")

#Set preferred volume
mixer.music.set_volume(0.2)

#Play the music
mixer.music.play('walk the dinosaur.mp3')
  • Docs are always the best place to start: https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play. `play` does not take the name of a file as input. – Brian Rodriguez Jul 10 '22 at 08:27

3 Answers3

1

According to pygame's documentation (https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play),
the play function doesn't have any required parameters. Unless you want to change any of the default ones (loops, start, or fade_ms) you don't need to pass anything to it.

Adid
  • 1,504
  • 3
  • 13
1

Play only starts the preloaded track, what you are searching for is

mixer.music.load('walk the dinosaur.mp3')
mixer.music.play()

Paul_0
  • 358
  • 4
  • 11
0

You don't need to give the name of the file again in the play function. Also play is async, so you will need to wait before exiting.

import pyflakes
import pygame
import os
from pygame import mixer

pygame.init()
mixer.init()

#Load audio file
mixer.music.load('walk the dinosaur.mp3')

print("music started playing....")

#Set preferred volume
mixer.music.set_volume(0.2)

#Play the music
mixer.music.play()
while pygame.mixer.music.get_busy(): 
    pygame.time.Clock().tick(10)
viggnah
  • 1,709
  • 1
  • 3
  • 12