first working version, but not used now
[enigma2-plugins.git] / lastfm / src / StreamPlayer.py
1 from enigma import eServiceReference
2 import os
3 class StreamPlayer:
4     is_playing = False
5     
6     def __init__(self,session, args = 0):
7         print " init StreamPlayer"
8         self.session = session
9         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
10         self.session.nav.event.append(self.__event)
11         self.onStateChanged = []
12
13     def stateChanged(self):
14         for i in self.onStateChanged:
15             i()   
16  
17     def __event(self, ev):
18         print "EVENT ==>",ev
19         if ev ==6:
20             self.stop("got EVENT 6, GST stopped")
21
22     def play(self,stream):
23         print " start streaming %s" %stream
24         if self.is_playing is True:
25             self.stop()
26             self.play(stream)
27         else:
28             if stream.startswith("/") is not True:
29                 print "playing remote stream",stream
30                 self.session.nav.stopService()
31 #                sref = eServiceReference(4097,0,stream )
32 #                sref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%stream.replace(":",":"))
33 #                self.session.nav.playService(sref)
34                 self.targetfile = "/tmp/lastfm.mp3"
35                 os.system("mknod %s p" %self.targetfile)
36                 os.system("wget %s -O- > %s&" %(stream,self.targetfile))
37                 self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
38             else:
39                 print "playing local stream",stream
40                 esref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%stream)
41                 self.session.nav.playService(esref)
42             self.is_playing = True
43         self.stateChanged()
44             
45     def stop(self,text=""):
46         if self.is_playing is True:
47             print " stop streaming",text
48             self.session.nav.stopService()
49             os.system("killall -9 wget")
50             os.system("rm %s" %self.targetfile)
51             self.session.nav.playService(self.oldService)
52         self.is_playing = False
53         self.stateChanged()
54     def exit(self):
55         self.stop()
56