How to set sound volume level using python?

hello kind people
i’m searching for a python command can access a sound played in the Sound sensor and edite the volume level , also if copying from a property is supported then you gave me a shortcut :smile:

thank you

Hi, Im working on a way to control the soundlogic actuator via python.
This code might help:

import bge
'''
DOC

you have one "music manager" in your level(if you use constant HUD put it there)
it can be any object
set up 3 properties:

SoundIndex  integer
last_index  integer
Volume      float

set an always sensor named "mySensor" and put it in a python controller with this
skript

combine the python controller with sound actuators named "music1" and "music2"

change property "SoundIndex"
'''
def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner
    
#### properties
    
    SI = own["SoundIndex"]
    LI = own["last_index"]
    VO = own["Volume"]

    
#### sensors

    sens = cont.sensors['mySensor']
    
#### actuators
    
    m1 = cont.actuators['music1']

    m2 = cont.actuators['music2']
    
#### DJ

    if SI == 1:
        
        if SI != LI:# make sure this happens only once
            own["Volume"] = 0# reset property for fadein/may be useless later
            cont.activate('music1')# activate actuator with soundfile
            m1.volume = VO# mute sound for fadein
            cont.deactivate('music2')
            own["last_index"] = 1
        elif VO <= 1:# rise volume to 1
            own["Volume"] += 0.01# set property(just a variable)
            m1.volume = VO# assign current volume


    elif SI == 2:
        if SI != LI:# make sure this happens only once
            own["Volume"] = 0# reset property for fadein/may be useless later
            cont.activate('music2')# activate actuator with soundfile
            m2.volume = VO# mute sound for fadein
            cont.deactivate('music1')
            own["last_index"] = 2
        elif VO <= 1:# rise volume to 1
            own["Volume"] += 0.01# set property(just a variable)
            m2.volume = VO# assign current volume

        
    elif SI == 0:
        if SI != LI:
            own["Volume"] = 0# reset property for fadein
            cont.deactivate('music1')
            cont.deactivate('music2')
            own["last_index"] = 0




main()

1 Like