TypeError: an integer is required (got type str) mixer

344 Views Asked by At

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')
3

There are 3 best solutions below

0
Adid On

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.

0
Paul_0 On

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

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

0
viggnah On

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)