How do I stop and continue the audio?

Hello!
I’m doing a horror game. There are cassette tapes and I need to listen to them but the monsters keep me from finishing the audio.
How do I pause the audio while defending myself from the monster and resume audio when the monster leaves?

Hello @mega_fredbears

The only way to stop sounds mid-way in BGE is with python.
Take a look at this script :arrow_down:
https://docs.blender.org/api/2.79/aud.html

You are looking for these functions btw :arrow_down:

aud.pause()

aud.resume()

1 Like

Ok, what do I do now?

What you’re going to want to do is to set up an audio factory using the AUD system in Blender using a Python script. Take a look at the API on this page to see what I’m talking about: https://docs.blender.org/api/blender_python_api_2_77_0/aud.html It says it’s for 2.77 but it’s the same for 2.79. What it does is it creates a source for the sound files, or a “factory” as it refers to them. The factory has little options in it that you can change, called “handles.” These handles can change pitch, volume, play status, pause status, and a bunch of other things.

So first up what you’re going to want to do is create some properties. These will keep track of little changes you’ve made. They can be applied to other things like too, not just this example with the audio. So we’ll write int

import bge   #This imports the game engine itself
from bge import logic #This makes sure the game logic loads with the engine
import aud #This is the core function of this example, the AUD system. It's what controls the audio using Python.

scene = bge.logic.getCurrentScene() #This makes sure the object that's running the script knows what scene it's in. It can be used to keep track of objects when paired with other functions.
cont = bge.logic.getCurrentController() #This is the part that checks what's running the script and controlling it.
own = cont.owner #This is the object the script is running for as it's core.
device = aud.device() #This makes sure the game engine knows what audio device is currently being used by the system.

keyboard = bge.logic.keyboard #This lists the keyboard as a possible input device.
ACTIVE = bge.logic.KX_INPUT_ACTIVE #This checks to see if the input from the input device is active.
NONE = bge.logic.KX_INPUT_NONE #This checks to see that there is no input from the input device.
JUST_RELEASED = bge.logix.KX_INPUT_JUST_RELEASED #This checks to see that you just left off the input, in our case a keyboard key.

And that is the main setup. What we’ve done is loaded the core functions of our script. Those being the logic, devices, and the input device’s possible states. Next we’re going to create our properties. These will check to see if the audio has loaded and if the audio is allowed to play.

if "init" not in own:
    own["soundLoaded"] = False #This property is a Boolean. It's always either True or False.
    own["soundPlay"] = False #This one's also a Boolean. Make sure they're both False, or else it'll mess up the load order later on.
    own["tapeName"] = "" #This is a string. We keep it as just two quotation marks so it appears blank on initialization. We'll use this for a cool function later on, loading the audio by it's filename.
    own["tapeNumber"] = 1 #This is the currently selected tape. This is not the same as tapeName.
    own["init"] = True #This finally initializes our properties and loads them in. From here we can use them.

And with the properties done we’ll start with the audio itself. It’s a bit tricky (and sadly glitchy from my experience), so don’t worry if it frustrates you. The main thing we’ll be focusing on is loading in the audio, setting up it’s factory, and then adjusting it’s handles. So what we’ll do is change our properties depending on if the keyboard input is either active or inactive.

tapeName = own['tapeName'] #What this does is it allows the game engine to recognize the string property we created earlier.

if keyboard.events[bge.events.ONEKEY] == JUST_RELEASED: #This uses the keyboard system we loaded earlier. It changes which tape is loaded. You can create your own system based on collision instead of using the keyboard, though.
    own['tapeNumber'] += 1
if keyboard.events[bge.events.TWOKEY] == JUST_RELEASED:
    own['tapeNumber'] -= 1

if own['tapeNumber'] > 2: #This prevents the counter from going beyond the number we have listed. If we only have two tapes, letting the counter go to three wouldn't do anything. So we cap it at 2.
    own['tapeNumber'] = 2

if own['tapeNumber'] < 1: #Same deal here. Except this time we stop it from going below 1.
    own['tapeNumber'] = 1

if own['tapeNumber'] == 1:
    own['tapeName'] = "nameofyourfile.mp3" #Change this to the name of your audio file. 

if own['tapeNumber'] == 2:
    own['tapeName'] = "nameofyoursecondfile.mp3"

if own['soundLoaded'] == False:
    TapeSound = aud.Factory.file(bge.logic.expandPath("//" + tapeName)) #This loads the audio using the filenames of the .mp3 files we listed earlier.
    TS = device.play(TapeSound) #This is our first handle. It tells the AUD system to play the tape. But it does it right away, so we're going to have to fix that.
    TS.loop_count = 0 #This tells the game to play the audio only once. If set to -1 it loops forever until the factory is removed.
    TS.volume = 0.1 #This controls the volume. You can edit this as a universal property for all volume by doing something like "TS.volume = own['globalVolume']"
    TS.pause() #This prevents the audio from playing right away. Otherwise it will load in the audio every frame, creating a new instance until it crashes your computer. Not groovy, so we'll pause it instead.
    own['soundLoaded'] = True #This finally lets the audio get ready to play.

if own['soundLoaded'] == True:
    if keyboard.events[bge.events.PKEY] == ACTIVE:
        TS.resume() #And thus we finally get the audio to play. Hooray!
    if keyboard.events[bge.events.PKEY] == NONE:
        TS.pause() #The audio is now paused allowing you to flee in terror and hear your own screams instead of the poor sap on the tape.

And there you go. Hope this was helpful. Note that I didn’t include every system you’ll need to get this working with stuff like an inventory system or 3D sound. But those are problems for you to tackle and learn firsthand from.

To @mega_fredbears


Er. Don’t know if @VulpesHilarianu has covered everything but here is my 2 cents :arrow_down:

http://www.mediafire.com/file/gedcy44zfprqnwb/Play_%26_Stop_Sound.zip/file

(Sorry for the late reply. Internet went out yesterday :frowning:)