class Player(): def __init__(self): self.playlist = [] self.currentTrack = [] self.playing = False self.p = Popen(['mplayer', '-cache', '16000', '-quiet', '-idle'], stdin=PIPE) self.paused = False self.player_state = "Stopped" def stop(self): self.p.terminate() self.playing = False self.p = None def play(self): # Used for starting streams try: self.p.stop() except: print "##### No active process #####" self.playing = True self.p = Popen(['mplayer', '-quiet', self.playlist[0].url], stdin=PIPE) self.player_state = "Playing" urlopen('http://localhost:5000/player_state') urlopen('http://localhost:5000/new_track') print "Current: %s" % (self.playlist[0].title) def play_next(self): if (len(self.playlist) < 1): return # because we can't do shit if (len(self.playlist) > 1): self.stop() self.playlist.pop(0) self.play() def add_stream_from_youtube(self, youtubeurl): # returns stream object url = youtubeurl try: video = pafy.new(url) audiostreams = video.audiostreams best = video.getbestaudio() s = Stream(best.title, best.url) self.playlist.append(s) except: print "had a problem adding" def send_command_to_player(self, command): sys.stdin.flush() self.p.stdin.write(command) def decrease_volume(self): if self.p is None: return sys.stdin.flush() for i in range(0, 10): self.p.stdin.write("/") def increase_volume(self): if self.p is None: return sys.stdin.flush() for i in range(0, 15): self.p.stdin.write("*") def play_or_pause(self): if (self.playing == False): self.play() return sys.stdin.flush() if self.player_state == "Playing": self.player_state = "Paused" else: self.player_state = "Playing" self.p.stdin.write("p") urlopen('http://localhost:5000/player_state') def seek_back(self): sys.stdin.flush() self.p.stdin.write("\x1B[D") def seek_forward(self): sys.stdin.flush() self.p.stdin.write("\x1B[C") def get_playlist(self): res = json.dumps(self.playlist, default=lambda o: o.__dict__) # so like, black magic and shit j = json.loads(res) return j def get_playback_status_true_false(self): if self.player_state == "Playing": return True else: return False