Playing multiple sound files at same time

272 views
Skip to first unread message

Max Bedacht

unread,
Oct 13, 2014, 6:54:43 PM10/13/14
to kivy-...@googlegroups.com
When using pygame directly you have access to multiple sound channels. Using these you can play, for example,  background music and at the same time have the game sprites making their own sounds.

Is there any way to do this with Kivy? It seems that when you play a sound when another is already playing you get nothing. 

Bruce Cropley

unread,
Oct 13, 2014, 11:31:26 PM10/13/14
to kivy-...@googlegroups.com
I haven't experienced this. Could you create a minimal example?

knappador

unread,
Oct 14, 2014, 9:57:36 AM10/14/14
to kivy-...@googlegroups.com
I can 2nd that I don't do anything special with 1.8.0+ to play multiple sounds concurrently.  I DO have to make sure to use SoundLoader multiple times to play the same sound file concurrently.  A snippet to do so as needed follows:

DEBUG_SOUND = False

class Sounds(object):

    def __init__(self):
        self.sounds = {}
        super(Sounds, self).__init__()
    
    def __getattr__(self, attr, volume=None):
        if not attr.startswith('play_'):
            return object.__getattribute__(self, attr)
        f = attr.split('play_')[1]
        sounds = getattr(self, 'sounds')
        loaded = sounds.get(f, [])
        ready = None
        for l in loaded:
            if l.state == 'stop':
                ready = l
                break
        if ready == None:
            ready = SoundLoader.load('audio/' + f + '.mp3')
            sounds[f] = loaded
            loaded.append(ready)
        if platform == 'android' or DEBUG_SOUND:
            return ready.play
        else:
            return lambda *args: None

sounds = Sounds()
sounds.play_meow() # loads a copy
sounds.play_meow() # loads a second copy
Reply all
Reply to author
Forward
0 new messages