2013-12-02 00:23:05 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-12-02 22:54:51 +01:00
|
|
|
import os
|
2013-12-02 00:23:05 +01:00
|
|
|
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):
|
2013-12-02 02:46:33 +01:00
|
|
|
if name not in clazz.media.iterkeys():
|
2013-12-02 22:54:51 +01:00
|
|
|
if not os.path.isfile(media_path):
|
|
|
|
return False
|
2013-12-02 02:46:33 +01:00
|
|
|
clazz.media[name] = phonon.Phonon.MediaSource(media_path)
|
2013-12-02 22:54:51 +01:00
|
|
|
return True
|
2013-12-02 00:23:05 +01:00
|
|
|
|
|
|
|
@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()
|
|
|
|
|