from __future__ import print_function import os import sys from PySide import phonon class MusicBox(object): # default media media = {} # state _media_obj = None @classmethod def init(clazz): clazz._output = phonon.Phonon.AudioOutput(phonon.Phonon.MusicCategory) clazz._media_obj = phonon.Phonon.MediaObject() phonon.Phonon.createPath(clazz._media_obj, clazz._output) @classmethod def add_music(clazz, name, media_path): if name not in clazz.media.iterkeys(): if not os.path.isfile(media_path): return False clazz.media[name] = phonon.Phonon.MediaSource(media_path) return True @classmethod def play_music(clazz, name): try: clazz._media_obj.setCurrentSource(clazz.media[name]) clazz._media_obj.play() except KeyError: print("Warning: Song '%s' is missing, not found in media base" % (name,), file=sys.stderr) @classmethod def stop_music(clazz): # use pause so we don't stop having the device open clazz._media_obj.pause()