How do I change the volume of the sound or music in PyGame?

北慕城南 提交于 2021-01-28 11:22:49

问题


How to change volume in PyGame like changing the volume by going to the settings. I made the UI elements, just need to know how to change the volume. I know I am not clear, but you can understand me. Please help


回答1:


Changing the volume depends on whether you are playing a pygame.mixer.Sound object or playing the music via the pygame.mixer.music module.

The volume of a Sound can be changed by set_volume(). The volume argument is a value in range [0.0, 1.0]:

pygame.mixer.init()
my_sound = pygame.mixer.Sound('my_sound.wav')
my_sound.play()

my_sound.set_volume(0.5)

The volume of the music can be changed by pygame.mixer.music.set_volume():

pygame.mixer.init()
pygame.mixer.music.load('my_music.mp3')
pygame.mixer.music.play()

pygame.mixer.music.set_volume(0.5)


来源:https://stackoverflow.com/questions/65247656/how-do-i-change-the-volume-of-the-sound-or-music-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!