Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Created October 18, 2013 20:32
Show Gist options
  • Save tshirtman/7047781 to your computer and use it in GitHub Desktop.
Save tshirtman/7047781 to your computer and use it in GitHub Desktop.
class MediaPlayer(object):
'''
MediaPlayer object is used to avoid keeping multiple sound files in memory
'''
def __init__(self):
self._media_player = None
def play_sound(self, soundfile, volume):
if not soundfile.startswith('/'):
soundfile = join(getcwd(), 'sounds', soundfile)
if platform == 'android':
if self._media_player:
if self._media_player.isPlaying():
self._media_player.stop()
self._media_player.release()
self._media_player = media_player = MP()
media_player.setDataSource(soundfile)
media_player.prepare()
# XXX
media_player.setVolume(float(volume), float(volume))
media_player.start()
return media_player
else:
print "XXX play_sound not implemented on %s" % platform
play_sound = MediaPlayer().play_sound
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment