Volume control in pygame

Question:

I was wondering if there was a way to control the volume in pygame? Currently I am just importing my mp3 pygame.mixer.Sound("My sound.MP3") and then running my sound with pygame.mixer.Sound.play() which works fine but I want to control the volume in my script rather than having to go through my volume settings.

Asked By: Nicholas Geiser

||

Answers:

The pygame.mixer.Sound object has a set_volume(value) method:

This will set the playback volume (loudness) for this Sound. This will immediately affect the Sound if it is playing. It will also affect any future playback of this Sound.

Parameters: value (float) —
volume in the range of 0.0 to 1.0 (inclusive)

my_sound = pygame.mixer.Sound("My sound.MP3")
my_sound.play()
my_sound.set_volume(0.5)
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.