在pygame.Sound模块中全局设置音量 [英] Setting volume globally in pygame.Sound module

查看:641
本文介绍了在pygame.Sound模块中全局设置音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为使用pygame播放的所有声音设置音量. def on_press(): file_name = self.filename

I have been trying to set volume to all the sound playing using pygame. def on_press(self): file_name = self.filename

    pygame.mixer.pre_init(44100, 16, 2, 4096) 
    pygame.init()
    pygame.mixer.init(44100)
    channel=pygame.mixer.Sound(file_name)
    channel.play(0)

每次我按下前端的按钮时,都会播放声音.我可以播放尽可能多的声音. 现在我的问题是如何控制所有音乐的音量?在这里,我应该为每个文件创建对象并设置其音量值.代码在下面

every time I press a button in the front end, A sound plays. I can play as many sounds I can. Now my question is how do I control volume of all the music? Here I should create object for each file and set its volume value. Code is below

           channel=pygame.mixer.Sound(file_name)
           channel.play(0)

如何全局设置音量?给定的音量会影响正在播放的所有文件吗? 感谢您的帮助!

How do I set the volume globally? All files that are playing should be affected with given volume? Thanks for any help!

推荐答案

我相信您必须分别使用Sound.set_volume(value)设置每个.您需要存储创建的每个Sound实例,然后在想要更改音量时在它们之间循环.如果要根据已经设置的音量设置每个,则可以执行以下操作:

I believe you have to set each one individually with Sound.set_volume(value). You will have to store each Sound instance you create and then loop through them when you want the volume to change. If you want to set each one depending on the volume it was already, you can do something like:

def set_all_volume(sounds,mult):
    for sound in sounds:
        vol = sound.get_volume()
        sound.set_volume(min(vol*mult,1.0))

这篇关于在pygame.Sound模块中全局设置音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆