Stop sound using Audaspace module

Hello, I’m writing a python script to play a sound every time i press a button

from bge import logic
import aud

cont = logic.getCurrentController()
scene = logic.getCurrentScene()

keys = cont.sensors["keys"]

SPACE = keys.getKeyStatus(32)

device = aud.device()

SFX = logic.expandPath('//Sounds/')


sfx_sound = SFX+"sound"+".wav"

if SPACE == 1:
  beep = aud.Factory.file(sfx_sound)
  device.play(beep)

It works fine, but if I keep mashing the button it creates new instances of the same sound, that end up overlapping with each other.

I want the sound to stop and start over again when i press the button. Is there any way to do this?

Thanks in advance.

use button press -> add sound emmitting object,

parent the item to the object that makes the sound

each time you add one, have it cycle through object.children, and kill the old instance with endObject()

?

for child own.children:
    if 'soundTag' in child:
        child.endObject()

added = bge.logic.getCurrentScene().addObject('soundEmmiter',own,0)
added.setParent(own,0,1)

Yes, I pressed the power button instead of F12, too.

I ripped that key out of my keyboard

I’m pretty sure there must a better way of doing it using the audaspace module, but i don’t seem to find it because there aren’t many tutorials out there, and the documentation available for it is terrible.

Yes, I pressed the power button instead of F12, too.

I ripped that key out of my keyboard

LOL

just giving you the solution I ended up using,

it works, if you can find a better one, use it and post it :smiley:

http://wiki.blender.org/index.php/User:NeXyon/GSoC2010/Audaspace

I think you can save the sound as a property and then stop it later,

pseudo code


if key.positive: # key press
    play
elif not key.positive: # key release
    stop

When you release the key, it should stop playing

import aud
device = aud.device()
s = aud.Factory('music.ogg')
c = device.play(s)
if 'playing' not in own:
    own['playing']=[c]
else:
     own['playing'].append(c)

Nailed it!!! :smiley:

beep = aud.Factory.file(sfx_sound)


if SPACE == 1:
  if "sound" not in own:
    own["sound"] = device.play(beep)
  own["sound"].position = 0

Just call stop() on it.


beep = aud.Factory.file(sfx_sound)
playing_thing = device.play(beep)
playing_thing.stop()