comparison browser_side/radiocol.py @ 287:7a1dc69112b8

browser_side (plugin radiocol): send the current queue to new players
author souliane <souliane@mailoo.org>
date Thu, 28 Nov 2013 21:43:12 +0100
parents e76ec07be8e5
children 4f221f34bdc7
comparison
equal deleted inserted replaced
286:e76ec07be8e5 287:7a1dc69112b8
141 Timer(5000, self.updateStatus) 141 Timer(5000, self.updateStatus)
142 142
143 def onSubmitComplete(self, event): 143 def onSubmitComplete(self, event):
144 result = event.getResults() 144 result = event.getResults()
145 if result == "OK": 145 if result == "OK":
146 self.setTemporaryStatus('[Your song has been added to queue]', "ok") 146 # the song can still be rejected (not readable, full queue...)
147 self.setTemporaryStatus('[Your song has been submitted to the radio]', "ok")
147 elif result == "KO": 148 elif result == "KO":
148 self.setTemporaryStatus('[Something went wrong during your song upload]', "ko") 149 self.setTemporaryStatus('[Something went wrong during your song upload]', "ko")
149 else: 150 else:
150 Window.alert('Submit error: %s' % result) 151 Window.alert('Submit error: %s' % result)
151 self.status.setText('') 152 self.status.setText('')
155 156
156 def __init__(self, player_id, metadata_panel): 157 def __init__(self, player_id, metadata_panel):
157 HTML.__init__(self) 158 HTML.__init__(self)
158 self._id = player_id 159 self._id = player_id
159 self.metadata = metadata_panel 160 self.metadata = metadata_panel
161 self.timestamp = ""
160 self.title = "" 162 self.title = ""
161 self.artist = "" 163 self.artist = ""
162 self.album = "" 164 self.album = ""
163 self.filename = None 165 self.filename = None
164 self.played = False # True when song is playing/played, become False on preload 166 self.played = False # True when song is playing/played, become False on preload
165 167
166 def preload(self, filename, title, artist, album): 168 def preload(self, timestamp, filename, title, artist, album):
167 """preload the song but doesn't play it""" 169 """preload the song but doesn't play it"""
170 self.timestamp = timestamp
168 self.filename = filename 171 self.filename = filename
169 self.title = title 172 self.title = title
170 self.artist = artist 173 self.artist = artist
171 self.album = album 174 self.album = album
172 self.played = False 175 self.played = False
173 self.setHTML('<audio id="%s" style="display: none" preload="auto" src="radiocol/%s" />' % (self._id, html_sanitize(filename))) 176 self.setHTML('<audio id="%s" style="display: none" preload="auto" src="radiocol/%s" />' % (self._id, html_sanitize(filename)))
174 print "preloading %s in %s" % (title, self._id) 177 print "preloading %s in %s" % (title, self._id)
175 178
176 def play(self): 179 def play(self, play=True):
177 """actually play the song""" 180 """actually play the song"""
178 self.played = True 181 if play:
179 self.metadata.setTitle(self.title) 182 self.played = True
180 self.metadata.setArtist(self.artist) 183 self.metadata.setTitle(self.title)
181 self.metadata.setAlbum(self.album) 184 self.metadata.setArtist(self.artist)
182 185 self.metadata.setAlbum(self.album)
183 JS(""" 186
184 var player = top.document.getElementById(this._id); 187 if play: # JS only support constant strings
185 player.play(); 188 JS("""var player = top.document.getElementById(this._id); if (player) player.play();""")
186 """) 189 else:
190 JS("""var player = top.document.getElementById(this._id); if (player) player.pause();""")
187 191
188 192
189 class RadioColPanel(HorizontalPanel, ClickHandler): 193 class RadioColPanel(HorizontalPanel, ClickHandler):
190 194
191 def __init__(self, parent, referee, player_nick, players, queue_data): 195 def __init__(self, parent, referee, player_nick, players, queue_data):
204 self.add(CaptionPanel("Songs queue", self.playlist_panel)) 208 self.add(CaptionPanel("Songs queue", self.playlist_panel))
205 self.control_panel = ControlPanel(self) 209 self.control_panel = ControlPanel(self)
206 self.add(CaptionPanel("Controls", self.control_panel)) 210 self.add(CaptionPanel("Controls", self.control_panel))
207 211
208 self.next_songs = [] 212 self.next_songs = []
209 self.players = [Player("player_%d" % i, self.metadata_panel) for i in range(4)] 213 self.players = [Player("player_%d" % i, self.metadata_panel) for i in xrange(queue_data[1] + 1)]
210 self.current_player = None 214 self.current_player = None
211 for player in self.players: 215 for player in self.players:
212 self.add(player) 216 self.add(player)
213 self.addClickListener(self) 217 self.addClickListener(self)
214 218
224 """Add a song to the left panel's next songs queue""" 228 """Add a song to the left panel's next songs queue"""
225 next_song = Label(title) 229 next_song = Label(title)
226 next_song.setStyleName("radiocol_next_song") 230 next_song.setStyleName("radiocol_next_song")
227 self.next_songs.append(next_song) 231 self.next_songs.append(next_song)
228 self.playlist_panel.append(next_song) 232 self.playlist_panel.append(next_song)
233 self.control_panel.updateStatus()
229 234
230 def popNextSong(self): 235 def popNextSong(self):
231 """Remove the first song of next songs list 236 """Remove the first song of next songs list
232 should be called when the song is played""" 237 should be called when the song is played"""
233 #FIXME: should check that the song we remove is the one we play 238 #FIXME: should check that the song we remove is the one we play
234 next_song = self.next_songs.pop(0) 239 next_song = self.next_songs.pop(0)
235 self.playlist_panel.remove(next_song) 240 self.playlist_panel.remove(next_song)
241 self.control_panel.updateStatus()
236 242
237 def getQueueSize(self): 243 def getQueueSize(self):
238 return len(self.playlist_panel.getChildren()) 244 return len(self.playlist_panel.getChildren())
239 245
240 def radiocolPreload(self, filename, title, artist, album): 246 def radiocolCheckPreload(self, timestamp):
247 for player in self.players:
248 if player.timestamp == timestamp:
249 return False
250 return True
251
252 def radiocolPreload(self, timestamp, filename, title, artist, album):
253 if not self.radiocolCheckPreload(timestamp):
254 return # song already preloaded
241 preloaded = False 255 preloaded = False
242 for player in self.players: 256 for player in self.players:
243 if not player.filename or \ 257 if not player.filename or \
244 (player.played and player != self.current_player): 258 (player.played and player != self.current_player):
245 #if player has no file loaded, or it has already played its song 259 #if player has no file loaded, or it has already played its song
246 #we use it to preload the next one 260 #we use it to preload the next one
247 player.preload(filename, title, artist, album) 261 player.preload(timestamp, filename, title, artist, album)
248 preloaded = True 262 preloaded = True
249 break 263 break
250 if not preloaded: 264 if not preloaded:
251 print("WARNING: Can't preload song, we are getting too many songs to preload, we shouldn't have more than 2 at once") 265 print("WARNING: Can't preload song, we are getting too many songs to preload, we shouldn't have more than 2 at once")
252 else: 266 else:
253 self.pushNextSong(title) 267 self.pushNextSong(title)
254 268
255 def radiocolPlay(self, filename): 269 def radiocolPlay(self, filename):
256 for player in self.players: 270 found = False
257 if player.filename == filename: 271 for player in self.players:
272 if not found and player.filename == filename:
258 player.play() 273 player.play()
259 self.popNextSong() 274 self.popNextSong()
260 self.current_player = player 275 self.current_player = player
261 self.control_panel.updateStatus() 276 found = True
262 return 277 else:
263 print("WARNING: Song not found in queue, can't play it. This should not happen") 278 player.play(False) # in case the previous player was not sync
279 if not found:
280 print("WARNING: Song not found in queue, can't play it. This should not happen")
264 281
265 def radiocolNoUpload(self): 282 def radiocolNoUpload(self):
266 self.control_panel.blockUpload() 283 self.control_panel.blockUpload()
267 284
268 def radiocolUploadOk(self): 285 def radiocolUploadOk(self):