Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...