No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

music.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Licensed under GPLv3
  2. # Written by Sebastian Lohff (seba@someserver.de)
  3. # http://seba-geek.de/projects/seopardy/
  4. from __future__ import print_function
  5. import os
  6. import sys
  7. from PySide import phonon
  8. class MusicBox(object):
  9. # default media
  10. media = {}
  11. # state
  12. _media_obj = None
  13. @classmethod
  14. def init(clazz):
  15. clazz._output = phonon.Phonon.AudioOutput(phonon.Phonon.MusicCategory)
  16. clazz._media_obj = phonon.Phonon.MediaObject()
  17. phonon.Phonon.createPath(clazz._media_obj, clazz._output)
  18. @classmethod
  19. def add_music(clazz, name, media_path):
  20. if name not in clazz.media.iterkeys():
  21. if not os.path.isfile(media_path):
  22. return False
  23. clazz.media[name] = phonon.Phonon.MediaSource(media_path)
  24. return True
  25. @classmethod
  26. def play_music(clazz, name):
  27. try:
  28. clazz._media_obj.setCurrentSource(clazz.media[name])
  29. clazz._media_obj.play()
  30. except KeyError:
  31. print("Warning: Song '%s' is missing, not found in media base" % (name,), file=sys.stderr)
  32. @classmethod
  33. def stop_music(clazz):
  34. # use pause so we don't stop having the device open
  35. clazz._media_obj.pause()