Sounds dont work (tried mp3 and wav)

Hello,

I create a sound actuator but it does not play.

This is what I have. The collision works because the script does get triggered and the enemy dies.


Perhaps it is a Sound volume thing ?
In the logic block, the volume does not go more than 2.
In the scene Audio properties I have tried 50, 100, nothing is heard.

I also tried either mp3 or wav = no luck (I prefer mp3 because the wav file is too big –> 1.3 MB for a 3 sec explosion ! )
I create those files with Audacity in Ubuntu.

I Googled this problem and did not find anything. So perhaps its a hardware thing…?
On Monday I will try to play the file in another computer at work.

Any ideas ?

Never mind, got it.
The sound got repeatedly played because of the script.

I put the play sound in another script with a flag, so that it plays only once, like this =


import bge
import aud


cont = bge.logic.getCurrentController()
me = cont.owner


#play explosion sound only once. If the 'exploded' property exists, it means the sound has been already played, so dont do anything
if 'exploded' not in me:
    print("Playing explosion")
    sound = aud.Factory.file(bge.logic.expandPath("//snd//bang1.mp3"))
    aud.device().play(sound)
    me["exploded"] = 1

This works Ok, although not very efficient because it gets loaded every time it plays.
I guess thats not good game programming. I will try to buffer it somehow.

Thanks and sorry.

use CODE format when posting code, it makes the world a happier place (if not the world, then these forums atleast). :wink:

[CODE*] contents [/CODE*]

  • remove the “*”

now about your problem. you can store that sound into a game property or (my favorite) the logic module.

## Runs Once at start up ##

import bge
import aud

## Store Device ##
bge.logic.audioDevice = aud.Device()


## Load Sounds ##
bge.logic.soundsDict = { #open the dictionary

"PlayerDeath":  aud.Factory.file(bge.logic.expandPath("//snd//bang1.mp3")), #remember the comma
"Other":  aud.Factory.file(bge.logic.expandPath("//snd//other.mp3")) #no comma on last item

} #close the dictionary


when you die:

## Runs every frame ##

if owner["Explode"] == True:
    bge.logic.audioDevice.play(bge.logic.soundsDict["PlayerDeath"])
    owner["Explode"] = False


i have never done audio before, so if i royally screwed up, please, let me know. this is untested, use at your own risk.

No, you didnt screw up, Ive done a similar thing (sorry, for the late reply):

This works very well. It will load the file from disk only the first time (necessary for recurring sounds, like explosions, shootings, etc)


import aud


#play explosion sound
    #buffer the sound in ram first
    if 'explosion1' not in bge.logic.globalDict:
        bge.logic.globalDict["explosion1"] = aud.Factory.file(bge.logic.expandPath("//snd//explosion1.mp3")).buffer()
    aud.device().play(bge.logic.globalDict["explosion1"])
    me["soundPlayed"] = 1


If you want to just load and play a file (for example, the victory sound at the end of the level), then just do this =


import aud

 #play victory.mp3 (no need to buffer it, it is played only once)
sound = aud.Factory.file(bge.logic.expandPath("//snd//victory.mp3"))
aud.device().play(sound)

Note that for large files, you should probably pre-load them, before you need them, say, at the beginning of the game,
because loading times can be significant.