Muting/manipulating sound can be a rather difficult task. In this tutorial, you will learn how to set up this simple script to manipulate sound. I will be using this script to get a sound to mute, without stopping and having to restart, but you will be able to do much with this little script if you know how to use it. 😉
Step 1: Opening Blender
As always, you will need to open up Blender. Get your scene set up and select the object that you want to play the music. Mine is a cube, called soundDebug.
Step 2: Setting up the Logic Brickwork
With the object selected, add a bool property called mute. Now, set up your logic bricks however you’d like, but be sure to add a sound actuator called “sound”. Go to the text panel, and add a new text document, called “muteSound”. Add a python sensor, running muteSound, and connect it to the sound actuator. You should now have something like this:
Step 3: Writing the code
Now comes the fun part 😛 put this code into your muteSound file:
import GameLogic cont = GameLogic.getCurrentController() own = cont.getOwner() vol = cont.getActuator("Your Sound Actuator") TRUE = 1 FALSE = 0 if not own.mute: vol.setGain(1) GameLogic.addActiveActuator(vol, TRUE) else: vol.setGain(0) GameLogic.addActiveActuator(vol, TRUE)
Step 4: Opening the Sound
Go to the Audio Panel and open the sound (be sure to upload a wav file), and open the same sound in the “sound” sound. actuator. Now, you should have something slightly similar to this:
Step 5: Testing and Conclusion
Now, just place your cursor in the 3D viewport, and press “P”. As long as mute is False, the sound will be audible. You can view its value in the game, by selecting the “D” button on the property and by enabling “Show Debug Properties” in the Game menu.
Download: CLICK HERE (No audio included. You will have to do Step 3 to get it)