bugfix with playlist is None, but i dont know why we can stop if we dont have a playl...
[enigma2-plugins.git] / lastfm / src / StreamPlayer.py
1 from enigma import eServiceReference
2 from os import system
3 from math import ceil
4 import time 
5 class StreamPlayer:
6     STATE_PLAYINGSTARTED = 0
7     STATE_STOP = 1
8     STATE_PLAYLISTENDS = 2
9     is_playing = False
10     trackstarttime = 0
11     currentplaylistitemnumber = 0
12     playlist = None
13     
14     def __init__(self,session, args = 0):
15         print " init StreamPlayer"
16         self.session = session
17         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
18         self.session.nav.event.append(self.__event)
19         self.onStateChanged = []
20     
21     def setPlaylist(self,playlist):
22         if self.playlist is not None:
23             self.currentplaylistitemnumber = 0 
24         self.playlist = playlist
25         
26     def stateChanged(self,reason):
27         for i in self.onStateChanged:
28             i(reason)   
29  
30     def __event(self, ev):
31         print "EVENT ==>",ev
32         if ev == 6:
33             self.stop("got EVENT 6, GST stopped")
34         if ev == 4:
35             self.trackstarttime = time.time()
36
37     def getRemaining(self):
38         remaining = int((self.playlist.getTrack(self.currentplaylistitemnumber)["duration"]/1000) - (time.time() - self.trackstarttime))
39         minutes = int(remaining/60)
40         seconds = int(remaining-(minutes*60))
41         def shiftchars(integer,char):
42             if integer in range(0,10):
43                 return char+str(integer)
44             else:
45                 return str(integer)
46         return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
47     
48     def play(self,tracknumber=False):
49         self.session.nav.stopService()
50             
51         if tracknumber is False:
52             self.currentplaylistitemnumber = 0 
53         else:
54             self.currentplaylistitemnumber = tracknumber
55         
56         track = self.playlist.getTrack(self.currentplaylistitemnumber)
57         
58         if track['location'] != "no location":
59             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
60             self.session.nav.stopService()
61             self.targetfile = "/tmp/lastfm.mp3"
62             system("mknod %s p" %self.targetfile)
63             system("wget %s -O- > %s&" %(track['location'],self.targetfile))
64             self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
65         self.is_playing = True
66
67     def skip(self):
68         self.stop()
69                 
70     def stop(self,text="",force=False):
71         if self.playlist is None:
72             self.is_playing = False
73             self.stateChanged(self.STATE_STOP)
74         elif force is False and self.playlist.length > 0 and (self.playlist.length-1) > self.currentplaylistitemnumber:
75             self.play(tracknumber=self.currentplaylistitemnumber+1)
76             self.stateChanged(self.STATE_PLAYINGSTARTED)
77         elif self.is_playing is True and force is True:
78             self.session.nav.stopService()
79             system("killall -9 wget")
80             system("rm %s" %self.targetfile)
81             self.session.nav.playService(self.oldService)
82             self.is_playing = False
83             self.stateChanged(self.STATE_STOP)
84             
85         else:
86             self.stateChanged(self.STATE_PLAYLISTENDS)
87             
88     def exit(self):
89         self.stop()
90     
91     def getMetadata(self,key):
92         try:
93             track = self.playlist.getTrack(self.currentplaylistitemnumber)
94             return track[key]
95         except:
96             return "N/A"