class MPlayer(object): def __init__(self, *args, **kwargs): self.argparser = None self.exit = False self.paused = False self.player = Player() def set_argparser(self, argparser): self.argparser = argparser def start(self, *args, **kwargs): pass @property def is_playing(self): if self.player.filename and not self.paused: status = True else: status = False return status def play(self, path, track, block=True): self.player.loadfile(path) self.player.pause() time.sleep(3) if block: while self.player.filename: if self.exit: break try: time.sleep(0.1) except KeyboardInterrupt: pass def pause(self): if self.is_playing: self.paused = True self.player.pause() def stop(self): self.player.stop() def resume(self): if not self.is_playing: self.pause = False self.player.pause() def shutdown(self): if self.player.is_alive(): self.player.quit()
class PiPlayBoxMode(BaseMode): def setup(self): self.modecolour = self.display.RED self.display.changeColour(self.modecolour) self.enabled = self.__ping() self.mplayer = None if self.enabled: self.actions = { 0: { "text": "Capital FM", "sound": "http://ice-sov.musicradio.com:80/CapitalMP3" }, 1: { "text": None, "sound": None }, 2: { "text": None, "sound": None }, 3: { "text": None, "sound": None }, 4: { "text": None, "sound": None }, 5: { "text": None, "sound": None }, 6: { "text": None, "sound": None }, 7: { "text": None, "sound": None } } self.addInterrupts() self.modename = "Internet Radio" self.subtext = "ONLINE" self.mplayer = Player() else: self.modename = "No Internet" self.subtext = "Connection" self.display.Update("%s\n%s" % (self.modename, self.subtext)) def __ping(self): self.display.Update("Checking if\nonline...") try: a = check_call( ["/bin/ping", "www.bbc.co.uk", "-c", "1", "-t", "200"]) return True except CalledProcessError: return False def addInterrupts(self): e = GPIO.add_event_detect for i in range(8): e(self.buttons[i], GPIO.RISING, lambda channel, x=i: self.buttonAction(x), bouncetime=600) def buttonAction(self, button): action = self.actions[button] if action["text"] is not None: self.display.Update(action["text"]) self.mplayer.stop() self.speaker.Unmute() self.mplayer.loadfile(action["sound"]) def quit(self): if self.mplayer is not None and self.mplayer.is_alive(): self.mplayer.stop() self.mplayer.quit() self.speaker.Mute() self.mplayer = None
def main(): try: print("Selecting stream...") sys.stdout.flush() if os.path.isfile(alt_music_list_path): alt_music_list_file = open(alt_music_list_path, 'r') alt_music_list = [f.strip() for f in alt_music_list_file.readlines()] print("Alternative list available") sys.stdout.flush() else: alt_music_list = [] if store in alt_music_list: CODE = 'f6403f66f656451f8fcbf4b4fe881d9b' else: CODE = '79c6519c9d1b46fc8d6d341d92f4eb4d' CURL = commands.getoutput("curl -s 'https://api.rebrandly.com/v1/links/%s' -H 'apikey: 77a44fcac24946679e800fadc4f84958'" % CODE) JSON = json.loads(CURL) STREAM = JSON["destination"] print("Checking stream...") sys.stdout.flush() if int(urllib.urlopen(STREAM).getcode()) != 200: raise Exception("Stream not available. Restarting") print("Starting player...") sys.stdout.flush() player = Player(args=('-ao alsa:device=hw=1.0')) player.loadfile(STREAM) print("Playing %s" % STREAM) sys.stdout.flush() print("Checking for announcements...") sys.stdout.flush() announcements = [] if os.path.exists(announcements_lang_path): announcements = [os.path.join(announcements_lang_path, f) for f in os.listdir(announcements_lang_path) if not f.startswith('.') and os.path.isfile(os.path.join(announcements_lang_path, f))] # announcements_date_file = "%s.mp3" % datetime.today().strftime('%Y-%m-%d') # if country == 'sweden' and os.path.exists(announcements_date_path) and os.path.isfile(os.path.join(announcements_date_path, announcements_date_file)): # announcements.append(os.path.join(announcements_date_path, announcements_date_file)) if announcements: print("Announcements found for %s" % country) sys.stdout.flush() print("Waiting for cache. Sleeping for 2 mins") sys.stdout.flush() sleep(120) while True: for announce in announcements: for n in range(GAP): if not player.is_alive(): raise Exception("Player not playing. Restarting") if player.filename is None: raise Exception("No internet connected. Restarting") sleep(1) player.pause() player_announce = Player(announce) sleep(1) duration = int(math.ceil(player_announce.length)) if (duration > 30): duration -= 27 else: duration -= 1 sleep(duration) player_announce.quit() player.pause() else: print("No announcements found for %s" % country) sys.stdout.flush() print("Waiting for cache. Sleeping for 2 mins") sys.stdout.flush() sleep(120) while True: if not player.is_alive(): raise Exception("Player not playing. Restarting") if player.filename is None: raise Exception("No internet connected. Restarting") sleep(1) except Exception as e: try: player.quit() except: pass print(e) sys.stdout.flush() main()