Problem trying to play sounds with pygame module | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem trying to play sounds with pygame module

I'm trying to play multiple sounds at the same time using pygame. I'm using the function pygame.mixer.Channel(num).play(sound_path) but it gives me error! IN THIS CASE: import pygame pygame.mixer.init(channels=10) pygame.mixer.Channel(3).play("sound.mp3") It stops giving me "TypeError: argument 1 must be Sound, not str" So I've looked for a solution online and I found that I have to use the function pygame.mixer.Sound(sound_path) like this: import pygame pygame.mixer.init(channels=10) pygame.mixer.Channel(3).play(pygame.mixer.Sound("sound.mp3")) Running that I receive "pygame.error: Unable to open file 'sound.mp3'" I don't know what to do, so I'm asking here! Thanks in advice :)) EDIT: I've tried to convert .mp3 in .ogg: It doesn't give me errors but It doesn't play the sound too :((

23rd Sep 2019, 2:09 PM
Giordano Fratti
Giordano Fratti - avatar
2 Answers
+ 1
i found this in pygame documentation All sound playback is mixed in background threads. When you begin to play a Sound object, it will return immediately while the sound continues to play. so its probably the program already closed, before the music is playing. also from documentation mp3 format can have a problem ondifferent system.
23rd Sep 2019, 3:51 PM
Taste
Taste - avatar
+ 1
Thanks mannn, It works! I've inserted a delay to let sound play, and It goes right! Now I'll finish my function, and I'll go back in case of obstacles. :)))
23rd Sep 2019, 4:17 PM
Giordano Fratti
Giordano Fratti - avatar