def __init__(self): self.gtk_init() self.playlist = Playlist_wrapper(self) self.vlc = VLC_wrapper() self.item = None self.infos = None self.dl_pipe, self.pipe = Pipe() self.p = None self.quit = False
class Player(Player_interface): def __init__(self): self.gtk_init() self.playlist = Playlist_wrapper(self) self.vlc = VLC_wrapper() self.item = None self.infos = None self.dl_pipe, self.pipe = Pipe() self.p = None self.quit = False def _update(self): self.item = self.playlist.get_current_item() if self.item != None: # if playlist isnt empty if self.p == None: # if downloader wasnt launched filename = self.item["id"] url = self.item["url"] self.start_downloader(url, filename) else: infos = self._get_dl_infos() if infos != None: if infos["answer"] == dl_a["initializing"]: self.set_song(infos) if infos["answer"] == dl_a["downloading"] and self.infos["answer"] == dl_a["initializing"]: self.launch_song() self.infos = infos self.set_progressbar(self.dl_progressbar, self.infos["byte_counter"], self.infos["data_len"]) #print self.infos p, t = self._get_vlc_infos() self.set_progressbar(self.player_progressbar, p, t) def main_loop(self): if self.quit: return False gtk.main_iteration_do() self._update() return True def previous_song(self): self.playlist.go_previous() self.stop_downloader() self.p = None self.infos = None self.vlc.stop() self._update() def next_song(self): self.playlist.go_next() self.stop_downloader() self.p = None self.infos = None self.vlc.stop() self._update() def toggle(self): if self.item != None: if str(self.vlc.get_state()) == "State.NothingSpecial": self.launch_song() else: self.vlc.toggle() def set_song(self, infos): filename = infos["filename"] print "setting song : " + filename self.vlc.set_media(filename) def launch_song(self): print "launching song" self.vlc.play() def start_downloader(self, url, filename): print "download starting", filename self.p = Process(target = Downloader, args = (url, u"downloads/"+filename, self.dl_pipe, {})) self.p.start() self.pipe.send(dl_c["start"]) def stop_downloader(self): self.pipe.send(dl_c["stop"]) if self.p != None: self.pipe.poll() if self.p.is_alive(): self.p.terminate() self.p.join() self.dl_pipe, self.pipe = Pipe() def exit(self): self.stop_downloader() self.vlc.exit() self.quit = True def set_volume(self, val): self.vlc.set_volume(int(100*val)) def _get_dl_infos(self, sync = 1): """listen to the pipe connected with the download process. return None if the pipe is empty, an element of the pipe instead. @param sync specifies the maximum number of items to pop from the queue before returning (0 = infinite) @return an element sent by the process, or None if the pipe is empty """ infos = self.infos if sync == 0: sync = -1 while sync != 0: if self.pipe.poll(): infos = self.pipe.recv() sync -= 1 return infos def _get_vlc_infos(self): pos, tot = None, None if self.vlc.state != vlc_s["idle"]: p, t = self.vlc.get_pos() if t != 0 and t!= -1 : pos, tot = float(p), float(t) return pos, tot