import logging
import pyglet
from pyglet.resource import ResourceNotFoundException
from threading import Thread


class SoundManager(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True

    def run(self):
        logging.debug('Starting sound manager')
        pyglet.app.run()

    def play(self, path, loop=False, volume=None):
        logging.debug('Playing sound {}, loop={}'.format(path, loop))

        try:
            media = pyglet.resource.media(path)
            player = media.play()

            logging.info('Volume is at {}'.format(player.volume))

            if loop:
                player.eos_action = player.EOS_LOOP
    
            sound = Sound(path, player)

            if volume:
                sound.set_volume(volume)
            return sound
        except ResourceNotFoundException:
            logging.error('Could not find sound {}'.format(path))

class Sound(object):
    def __init__(self, path, player):
        self.path = path
        self.player = player

    def set_volume(self, volume):
        logging.debug('Setting volume of {} to {}'.format(self.path, volume))
        self.player.volume = volume
        return self

    def is_playing(self):
        return self.player._playing

    def play(self):
        self.player.play()
        return self

    def pause(self):
        self.player.pause()
        return self

    def stop(self):
        self.player.next()
        return self

@@ -22,65 +22,11 @@
  pyg
-let%0Afrom pyglet.resource import ResourceNotFoundException
+ame
 %0Afro
@@ -52,17 +52,16 @@
 Thread%0A%0A
-%0A
 class So
@@ -133,16 +133,100 @@
 __(self)
+        %0A        pygame.init()%0A        pygame.mixer.init()%0A        self._sounds = %7B%7D
 %0A       
@@ -316,33 +316,8 @@
 er')
-%0A        pyglet.app.run()
 %0A%0A  
@@ -448,69 +448,104 @@
     
-try:%0A            media = pyglet.resource.media
+if not path in self._sounds:%0A            self._sounds%5Bpath%5D = pygame.mixer.Sound
 (path)%0A
+%0A
         
     
@@ -544,32 +544,86 @@
     
-    player = media.play(
+loops = -1 if loop else 1%0A        player = self._sounds%5Bpath%5D.play(loops=loops
 )%0A%0A 
@@ -621,36 +621,32 @@
 loops)%0A%0A        
-  
-  
 logging.info('Vo
@@ -678,100 +678,24 @@
 yer.
+get_
 volume
+(
 ))
+)
 %0A%0A
-            if loop:%0A                player.eos_action = player.EOS_LOOP%0A    %0A    
     
@@ -735,20 +735,16 @@
         
-    
 if volum
@@ -754,28 +754,24 @@
             
- 
-   
 sound.set_vo
@@ -787,28 +787,24 @@
 me)%0A        
-    
 return sound
@@ -807,116 +807,8 @@
 ound
-%0A        except ResourceNotFoundException:%0A            logging.error('Could not find sound %7B%7D'.format(path))
 %0A%0Acl
@@ -1055,23 +1055,26 @@
 yer.
+set_
 volume
- = 
+(
 volume
+)
 %0A   

Use pygame for sound.