pluginhider: add translation support and german translation by SiennaRoot (thanks!)
[enigma2-plugins.git] / lastfm / src / StreamPlayer.py
1 from enigma import eServiceReference
2 from enigma import iPlayableService
3 from Components.ServiceEventTracker import ServiceEventTracker
4 from twisted.internet import reactor
5 from time import time
6
7 # for localized messages
8 from . import _
9
10
11 class StreamPlayer:
12     STATE_PLAYINGSTARTED = 0
13     STATE_STOP = 1
14     STATE_PLAYLISTENDS = 2
15     is_playing = False
16     trackstarttime = 0
17     currentplaylistitemnumber = 0
18     playlist = None
19     onClose = []
20     def __init__(self,session, args = 0):
21         self.session = session
22         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
23         self.onStateChanged = []
24         self.__event_tracker = ServiceEventTracker(screen=self,eventmap=
25             {
26                 iPlayableService.evStart: self.__onStart,
27                 iPlayableService.evEOF: self.__onStop,
28             })
29     def __onStart(self):
30         self.trackstarttime = time()
31     
32     def __onStop(self):
33         self.stop()
34         
35     def setSession(self,session):
36         self.session = session
37         
38     def setPlaylist(self,playlist):
39         if self.playlist is not None:
40             self.currentplaylistitemnumber = 0 
41         self.playlist = playlist
42         
43     def stateChanged(self,reason):
44         for i in self.onStateChanged:
45             i(reason)   
46
47     def getRemaining(self):
48         track = self.playlist.getTrack(self.currentplaylistitemnumber)
49         if track is False:
50             return "N/A"
51         else:
52             remaining = int((track["duration"]/1000) - (time() - self.trackstarttime))
53             minutes = int(remaining/60)
54             seconds = int(remaining-(minutes*60))
55             def shiftchars(integer,char):
56                 if integer in range(0,10):
57                     return char+str(integer)
58                 else:
59                     return str(integer)
60             return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
61     
62     def play(self,tracknumber=False):
63         if tracknumber is False:
64             self.currentplaylistitemnumber = 0 
65         else:
66             self.currentplaylistitemnumber = tracknumber
67         
68         track = self.playlist.getTrack(self.currentplaylistitemnumber)
69         if track is False:
70             print "no track to play"
71         elif track['location'] != "no location":
72             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
73             reactor.callLater(1, self._delayedPlay, eServiceReference(4097,0,track['location']))
74             self.is_playing = True
75
76     def _delayedPlay(self,sref):
77         if self.is_playing: # making sure, that no one presses stop while we had wait 
78             self.session.nav.playService(sref)
79     
80     def skip(self):
81         self.stop()
82                 
83     def stop(self,text="",force=False):
84         if self.playlist is None:
85             self.is_playing = False
86             self.stateChanged(self.STATE_STOP)
87         elif force is False and self.playlist.length > 0 and (self.playlist.length-1) > self.currentplaylistitemnumber:
88             self.play(tracknumber=self.currentplaylistitemnumber+1)
89             self.stateChanged(self.STATE_PLAYINGSTARTED)
90         elif self.is_playing is True and force is True:
91             pass
92             self.session.nav.playService(self.oldService)
93             self.is_playing = False
94             self.stateChanged(self.STATE_STOP)            
95         else:
96             self.stateChanged(self.STATE_PLAYLISTENDS)
97             
98     def exit(self):
99         for x in self.onClose:
100             x()
101         self.stop()
102     
103     def getMetadata(self,key):
104         try:
105             track = self.playlist.getTrack(self.currentplaylistitemnumber)
106             return track[key]
107         except:
108             return "N/A"