fix crash when pressing play in Movieplayer
[enigma2.git] / lib / python / Screens / MediaPlayer.py
1 from enigma import eTimer, iPlayableService, eServiceCenter, iServiceInformation, eSize
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import NumberActionMap
5 from Components.Label import Label
6 from Components.Input import Input
7 from Components.GUIComponent import *
8 from Components.Pixmap import Pixmap
9 from Components.Label import Label
10 from Components.FileList import FileEntryComponent, FileList
11 from Components.MediaPlayer import PlayList, PlaylistEntryComponent
12 from Plugins.Plugin import PluginDescriptor
13 from Tools.Directories import resolveFilename, SCOPE_MEDIA
14 from Components.ServicePosition import ServicePositionGauge
15 from Screens.ChoiceBox import ChoiceBox
16 from Components.ServiceEventTracker import ServiceEventTracker
17 from Screens.InfoBarGenerics import InfoBarSeek
18
19 import os
20
21 class MediaPlayer(Screen, InfoBarSeek):
22         def __init__(self, session, args = None):
23                 Screen.__init__(self, session)
24                 self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
25                 self.session.nav.stopService()
26                 
27                 self.filelist = FileList(resolveFilename(SCOPE_MEDIA), matchingPattern = "^.*\.(mp3|ogg|ts|wav|wave)", useServiceRef = True)
28                 self["filelist"] = self.filelist
29
30                 self.playlist = PlayList()
31                 self["playlist"] = self.playlist
32                 
33                 self["PositionGauge"] = ServicePositionGauge(self.session.nav)
34                 
35                 self["currenttext"] = Label("")
36                 
37                 self["artisttext"] = Label(_("Artist:"))
38                 self["artist"] = Label("")
39                 self["titletext"] = Label(_("Title:"))
40                 self["title"] = Label("")
41                 self["albumtext"] = Label(_("Album:"))
42                 self["album"] = Label("")
43                 self["yeartext"] = Label(_("Year:"))
44                 self["year"] = Label("")
45                 self["genretext"] = Label(_("Genre:"))
46                 self["genre"] = Label("")
47                 
48                 #self["text"] = Input("1234", maxSize=True, type=Input.NUMBER)
49                 
50                 self["actions"] = NumberActionMap(["OkCancelActions", "DirectionActions", "NumberActions", "MediaPlayerSeekActions"],
51                 {
52                         "ok": self.ok,
53                         "cancel": self.exit,
54                         
55                         "right": self.rightDown,
56                         "rightRepeated": self.doNothing,
57                         "rightUp": self.rightUp,
58                         "left": self.leftDown,
59                         "leftRepeated": self.doNothing,
60                         "leftUp": self.leftUp,
61                         
62                         "up": self.up,
63                         "upRepeated": self.up,
64                         "down": self.down,
65                         "downRepeated": self.down,
66                         
67                         "play": self.playEntry,
68                         "pause": self.pauseEntry,
69                         "stop": self.stopEntry,
70                         
71                         "previous": self.previousEntry,
72                         "next": self.nextEntry,
73                         
74                         "menu": self.showMenu,
75                         
76             "1": self.keyNumberGlobal,
77             "2": self.keyNumberGlobal,
78             "3": self.keyNumberGlobal,
79             "4": self.keyNumberGlobal,
80             "5": self.keyNumberGlobal,
81             "6": self.keyNumberGlobal,
82             "7": self.keyNumberGlobal,
83             "8": self.keyNumberGlobal,
84             "9": self.keyNumberGlobal,
85             "0": self.keyNumberGlobal
86         }, -2)
87
88                 InfoBarSeek.__init__(self)
89
90                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
91                         {
92 #                               iPlayableService.evSeekableStatusChanged: self.__seekableStatusChanged,
93 #                               iPlayableService.evStart: self.__serviceStarted,
94                                 
95                                 iPlayableService.evEOF: self.__evEOF,
96 #                               iPlayableService.evSOF: self.__evSOF,
97                         })
98
99                 self.onClose.append(self.delMPTimer)
100                 self.onClose.append(self.__onClose)
101                 
102                 self.righttimer = False
103                 self.rightKeyTimer = eTimer()
104                 self.rightKeyTimer.timeout.get().append(self.rightTimerFire)
105
106                 self.lefttimer = False
107                 self.leftKeyTimer = eTimer()
108                 self.leftKeyTimer.timeout.get().append(self.leftTimerFire)
109                 
110                 self.infoTimer = eTimer()
111                 self.infoTimer.timeout.get().append(self.infoTimerFire)
112                 self.infoTimer.start(500)
113                 
114                 self.currList = "filelist"
115                 
116         def doNothing(self):
117                 pass
118         
119         def exit(self):
120                 self.close()
121         
122         def checkSkipShowHideLock(self):
123                 self.updatedSeekState()
124         
125         def __evEOF(self):
126                 self.nextEntry()
127                 
128         def __onClose(self):
129                 self.session.nav.playService(self.oldService)
130         
131         def delMPTimer(self):
132                 del self.rightKeyTimer
133                 del self.leftKeyTimer
134                 del self.infoTimer
135                 
136         def infoTimerFire(self):
137                 currPlay = self.session.nav.getCurrentService()
138                 if currPlay is not None:
139                         self.updateMusicInformation( artist = currPlay.info().getInfoString(iServiceInformation.sArtist),
140                                                                                  title = currPlay.info().getInfoString(iServiceInformation.sTitle),
141                                                                                  album = currPlay.info().getInfoString(iServiceInformation.sAlbum),
142                                                                                  genre = currPlay.info().getInfoString(iServiceInformation.sGenre),
143                                                                                  clear = True)
144                 else:
145                         self.updateMusicInformation()
146         
147         def updateMusicInformation(self, artist = "", title = "", album = "", year = "", genre = "", clear = False):
148                 self.updateSingleMusicInformation("artist", artist, clear)
149                 self.updateSingleMusicInformation("title", title, clear)
150                 self.updateSingleMusicInformation("album", album, clear)
151                 self.updateSingleMusicInformation("year", year, clear)
152                 self.updateSingleMusicInformation("genre", genre, clear)
153
154         def updateSingleMusicInformation(self, name, info, clear):
155                 if info != "" or clear:
156                         if self[name].getText() != info:
157                                 self[name].setText(info)
158
159         def fwdTimerFire(self):
160                 self.fwdKeyTimer.stop()
161                 self.fwdtimer = False
162                 self.nextEntry()
163         
164         def rwdTimerFire(self):
165                 self.rwdKeyTimer.stop()
166                 self.rwdtimer = False
167                 self.previousEntry()
168         
169         def leftDown(self):
170                 self.lefttimer = True
171                 self.leftKeyTimer.start(1000)
172
173         def rightDown(self):
174                 self.righttimer = True
175                 self.rightKeyTimer.start(1000)
176                 
177         def leftUp(self):
178                 if self.lefttimer:
179                         self.leftKeyTimer.stop()
180                         self.lefttimer = False
181                         self[self.currList].pageUp()
182     
183         def rightUp(self):
184                 if self.righttimer:
185                         self.rightKeyTimer.stop()
186                         self.righttimer = False
187                         self[self.currList].pageDown()
188                         
189         def leftTimerFire(self):
190                 self.leftKeyTimer.stop()
191                 self.lefttimer = False
192                 self.switchToFileList()
193         
194         def rightTimerFire(self):
195                 self.rightKeyTimer.stop()
196                 self.righttimer = False
197                 self.switchToPlayList()
198                                 
199         def switchToFileList(self):
200                 self.currList = "filelist"
201                 self.filelist.selectionEnabled(1)
202                 self.playlist.selectionEnabled(0)
203                 self.updateCurrentInfo()
204                 
205         def switchToPlayList(self):
206                 if len(self.playlist) != 0:
207                         self.currList = "playlist"
208                         self.filelist.selectionEnabled(0)
209                         self.playlist.selectionEnabled(1)
210                         self.updateCurrentInfo()
211                 
212         def up(self):
213                 self[self.currList].up()
214                 self.updateCurrentInfo()
215
216         def down(self):
217                 self[self.currList].down()
218                 self.updateCurrentInfo()
219
220         def updateCurrentInfo(self):
221                 text = ""
222                 if self.currList == "filelist":
223                         if not self.filelist.canDescent():
224                                 text = self.filelist.getServiceRef().getPath()
225                 if self.currList == "playlist":
226                         text = self.playlist.getSelection().getPath()
227                 
228                 self["currenttext"].setText(os.path.basename(text))
229     
230         def ok(self):
231                 if self.currList == "filelist":
232                         if self.filelist.canDescent():
233                                 self.filelist.descent()
234                                 self.updateCurrentInfo()
235                         else:
236                                 self.copyFile()
237                 if self.currList == "playlist":
238                         selection = self["playlist"].getSelection()
239                         self.changeEntry(self.playlist.getSelectionIndex())
240
241         def keyNumberGlobal(self, number):
242                 pass
243         
244         def showMenu(self):
245                 menu = []
246                 if self.currList == "filelist":
247                         menu.append((_("switch to playlist"), "playlist"))
248                         if self.filelist.canDescent():
249                                 menu.append((_("add directory to playlist"), "copydir"))
250                         else:
251                                 menu.append((_("add file to playlist"), "copy"))
252                 else:
253                         menu.append((_("switch to filelist"), "filelist"))
254                         menu.append((_("delete"), "delete"))
255                         menu.append((_("clear playlist"), "clear"))
256                 self.session.openWithCallback(self.menuCallback, ChoiceBox, title="", list=menu)
257                 
258         def menuCallback(self, choice):
259                 if choice is None:
260                         return
261                 
262                 if choice[1] == "copydir":
263                         self.copyDirectory(self.filelist.getSelection()[0])
264                 elif choice[1] == "copy":
265                         self.copyFile()
266                 elif choice[1] == "playlist":
267                         self.switchToPlayList()
268                 elif choice[1] == "filelist":
269                         self.switchToFileList()
270                 elif choice[1] == "delete":
271                         self.deleteEntry()
272                 elif choice[1] == "clear":
273                         self.stopEntry()
274                         self.playlist.clear()
275                         self.switchToFileList()
276                         
277         def copyDirectory(self, directory):
278                 filelist = FileList(directory, useServiceRef = True, isTop = True)
279                 
280                 for x in filelist.getFileList():
281                         if x[0][1] == True: #isDir
282                                 self.copyDirectory(x[0][0])
283                         else:
284                                 self.playlist.addFile(x[0][0])
285                 self.playlist.updateList()
286         
287         def copyFile(self):
288                 self.playlist.addFile(self.filelist.getServiceRef())
289                 self.playlist.updateList()
290                 if len(self.playlist) == 1:
291                         self.changeEntry(0)
292
293         def nextEntry(self):
294                 next = self.playlist.getCurrentIndex() + 1
295                 if next < len(self.playlist):
296                         self.changeEntry(next)
297         
298         def previousEntry(self):
299                 next = self.playlist.getCurrentIndex() - 1
300                 if next >= 0:
301                         self.changeEntry(next)
302                         
303         def deleteEntry(self):
304                 self.playlist.deleteFile(self.playlist.getSelectionIndex())
305                 self.playlist.updateList()
306                 if len(self.playlist) == 0:
307                         self.switchToFileList()
308                 
309         def changeEntry(self, index):
310                 self.playlist.setCurrentPlaying(index)
311                 self.playEntry()
312         
313         def playEntry(self):
314                 if len(self.playlist.getServiceRefList()):
315                         currref = self.playlist.getServiceRefList()[self.playlist.getCurrentIndex()]
316                         if self.session.nav.getCurrentlyPlayingServiceReference() is None or currref != self.session.nav.getCurrentlyPlayingServiceReference():
317                                 self.session.nav.playService(self.playlist.getServiceRefList()[self.playlist.getCurrentIndex()])
318                                 info = eServiceCenter.getInstance().info(currref)
319                                 description = info.getInfoString(currref, iServiceInformation.sDescription)
320                                 self["title"].setText(description)
321                         self.unPauseService()
322                                 
323         def updatedSeekState(self):
324                 if self.seekstate == self.SEEK_STATE_PAUSE:
325                         self.playlist.pauseFile()
326                 elif self.seekstate == self.SEEK_STATE_PLAY:
327                         self.playlist.playFile()
328                 elif self.seekstate in ( self.SEEK_STATE_FF_2X,
329                                                                  self.SEEK_STATE_FF_4X,
330                                                                  self.SEEK_STATE_FF_8X,
331                                                                  self.SEEK_STATE_FF_32X,
332                                                                  self.SEEK_STATE_FF_64X,
333                                                                  self.SEEK_STATE_FF_128X):
334                         self.playlist.forwardFile()
335                 elif self.seekstate in ( self.SEEK_STATE_BACK_16X,
336                                                                  self.SEEK_STATE_BACK_32X,
337                                                                  self.SEEK_STATE_BACK_64X,
338                                                                  self.SEEK_STATE_BACK_128X,):
339                         self.playlist.rewindFile()
340         
341         def pauseEntry(self):
342                 self.pauseService()
343                 
344         def stopEntry(self):
345                 self.playlist.stopFile()
346                 self.session.nav.playService(None)
347
348