Pygame MP3 Looping Question

(invid) #1

I am trying to write a python script that will keep an mp3 looping as long as the SoundOn property (a logic bricks boolean) is TRUE. I would like to do all of the initialization in this same script if possible, and when the SoundOn is false, simply not restart the sound when it reaches the end of the sound. I am not very familiar with python yet, so I am still learning some of its basic syntax etc. Here are some links to the pygame mixer methods etc.:

If anyone has successfully used pygame to loop sounds etc (this is for a game’s soundtrack), would you mind posting a snippet of how you were able to do this?


(Gabio) #2

You’ll have more chance of geting answer for Game engine relelated questions in the Game engine section. even if it’s python.

(invid) #3

Thanks, I was wondering which section to post this under.

(goldentaiji) #4

You need 2 scripts. Well that’s how I currently have it setup for the underwater racing game.

[bool] Playing (false)
[bool] Play (false)

Always(pulse mode on)->Python(SoundCheck)


import GameLogic as g
import pygame.mixer

c = g.getCurrentController()
o = c.getOwner()'sound')


import GameLogic as g
import pygame.mixer

c = g.getCurrentController()
o = c.getOwner()

o.Playing =

Lol I should really put this in a separate .blend when I have the time. Hehe make a complementary tutorial to Andy’s.

Hehe, right now the way it is. It’ll loop indefinitely. Off to church now, so maybe I’ll post a .blend file and tutorial up when I get back.

Jason Lin

(invid) #5

Thanks! Yeah, a tutorial would be GREAT!! I am being bad this weekend, skipping church to get the garage cleaned out while I am able to get some help heh.

(invid) #6

I tried the above - thanks goldentaiji!! works great! I think I will add a thrid script, for transitioning in/out etc. when I am swapping soundtrack scores.

(goldentaiji) #7

Really it works? Lol, I replicated it and did a tutorial blend, but couldn’t upload it cause antich3’s server is down :expressionless: . I had to change Playing (true) for it to work. Dunno what happened. Lol maybe cause in the tutorial I added pausing and unpausing…

I’ll upload a .blend and maybe some writing when servers back up.

:smiley: I’m glad it worked for you.

Jason Lin

(invid) #8

Well, I did so some tweaking to it. I have a boolean property so I know it is playing. The trouble was, it would play all the way through the song before arriving at that check again, I could have fired that script w/ a changed property… but I created a third script to set a fadeout. - I may just move that into the playing check, and on change if the ‘play’ boolean is false, then set a fadeout and launch the next soundtrack file etc.

Sorry, kind of thinking aloud through the keyboard.

(goldentaiji) #9

%| Heh, took me awhile but I do have a small .blend up. Nothing fancy just loops a sound over and over again. I’d probably put one up where you can play through multiple sounds, maybe also include a shuffle option. Lol this is going to turn into some sorta personal music player if I keep going.

Here’s the link for others that may be viewing this thread.

Invid I’m curious how are you fading out the music, creating the transistion from one to the next? Just a simple transistion piece of music?

Jason Lin

(invid) #10

So far I haven’t gotten to the transition part. What I am working on now is trying to get the pygame to play different soundtracks for different portions of the game. So far, I haven’t gotten it to loop - here is what I have done.

1. Created a global empty to manage looping. In its logic bricks I have an always check that calls this code:

import GameLogic as g
import pygame.mixer

c = g.getCurrentController()
o = c.getOwner()

o.IsPlaying =

It also has a Property Changed block that looks for changes to the IsPlaying property:

import GameLogic as g
import pygame.mixer

c = g.getCurrentController()
o = c.getOwner()

if o.IsPlaying != IsPositive:

2. Created Empties for each soundtrack sections, that the player will pass near. This empty has one Near sensor that will load the song for that section of the game:

import GameLogic as g
import pygame.mixer

c = g.getCurrentController()
o = c.getOwner()

if o.Init == 1:'EntryRoomST.mp3')
	o.Init = 0

The initial soundtrack is triggered once (which is good, at first it was triggering over and over), but it does not loop and, I’m not sure if the different sections will load their mp3s properly (I will work on that once it is looping properly). THEN, once I have the sections looping and loading properly, I will work on trying to smooth up their transitions. I’ll look at your sample tonight. Thanks for posting it!!!


(invid) #11

With some minor tweaking of the code above, I now have the soundtrack loading and looping, and changing songs when the player passes points that I have specified (knowing they will have to pass by the points where this occures etc). There are a couple issues that remain, that you may be able to help me with.

  1. Transitions
    So far my game just flips over to the new song, it would be cool to use the fadeout to create a smoother transition

  2. Using pygame for other sounds
    WHILE the soundtrack is still playing, I need to be able to fire off other mp3s etc. I assume this means using another channel in the mixer - does anyone have any code snippets that demonstrate doing such a thing?

It would be soo nice if Blender (2.36, so far the only version that has a game engine functional enough to work) was able to utilize ogg or mp3 this is quite a headache, but sounds and sound effects really enhance the game. :confused: