Hi everyone, I'm looking for a way to play the next music from a list. I use pygame.mixer to play a music from lists_of_songs, I can't find a way to play the next music in the list. Do you know a method or something to do that?
Here is my code if you want to have a look:
Here is my code if you want to have a look:
#!/usr/bin/env python3 # RFID import RPi.GPIO as GPIO from mfrc522 import SimpleMFRC522 # Sound import os import pygame import threading import time tags = { '1006795053427': 'wild', #blue tag '526264140976': 'sport', #blue tag '584185304561': 'wild', # given to Valerie / Antoine '584185305597': 'sport', # given to Valerie / Antoine '584195095434': 'politic', # given to Valerie / Antoine '584195357686': 'wild', '584195619826': 'sport', '584195881982': 'politic', } class Media (): def __init__(self): self.themes = [] self.playing = False self.ready_to_play = False self.DELAY_BEFORE_TO_PLAY = 0.5 self.t = None self.SOUND_PATH = "/tmp/IRMA/" pygame.init() pygame.mixer.init() self.ready_to_play = True def play_theme(self, theme): print("play_theme - theme:", theme) theme_path = self.SOUND_PATH + theme + "/" lists_of_songs = os.listdir(theme_path) for song in lists_of_songs: if self.playing == False: break if song.endswith(".mp3"): file_path = theme_path + song pygame.mixer.music.load(str(file_path)) pygame.mixer.music.play() pygame.mixer.music.set_volume(1.0) print("play_theme - Playing::::: " + song) while pygame.mixer.music.get_busy() == True: if self.playing == False: break time.sleep(0.05) print("play_theme - stop") pygame.mixer.music.stop() return 0 def is_playing(self): return self.playing def is_ready(self): return self.ready_to_play def add(self, theme): if len(theme) > 20: return 1 # only one theme ''' if self.themes.count(theme) == 1: return 2 ''' self.clear() self.themes.append(theme) self.display() return 0 def clear(self): self.themes.clear() def play(self): print("play - start") self.ready_to_play = False self.playing = True # No merge, for the moment # Only one tag #theme = self.merge_themes() theme = self.themes[0] self.play_theme(theme) self.playing = False self.ready_to_play = True print("play - end") def stop(self): print("stop") self.playing = False if self.t is not None: self.t.cancel() self.t = None #pygame.mixer.music.stop() print("stop ended") def play_delayed(self): print("play_delayed - start") while self.is_ready() is False: time.sleep(0.01) if self.t is not None: self.t.cancel() self.t = None self.t = threading.Timer(self.DELAY_BEFORE_TO_PLAY, self.play) self.t.start() print("play_delayed - end") def display(self): print("display - List:") for theme in self.themes: print("-", theme) ''' def merge_themes(self): # TODO: 0111 if self.themes.count('wild') == 1: if self.themes.count('sport') == 1: return 'politic' elif self.themes.count('politic') == 1: return 'sport' else: return 'wild' elif self.themes.count('sport') == 1: if self.themes.count('politic') == 1: return 'wild' else: return 'sport' elif self.themes.count('politic') == 1: return 'politic' ''' def read_tags(tags): try: reader = SimpleMFRC522() except: print("read_tags - init error") return 1 media = Media() old_id = '' count_none = 0 read_interval = 0.5 # 500ms while True: time.sleep(read_interval) #print("read_tags - Waiting tag...") try: id, text = reader.read_no_block() #reader.read() except: print("read_tags - error") id = None text = "" if id is None: if count_none < 3: count_none += 1 elif count_none == 3 and media.is_playing(): count_none = 0 old_id = '' print("read_tags - media is playing ... stop") media.stop() continue count_none = 0 # convert id to string id = str(id) if id not in tags: continue if old_id != tags[id]: old_id = tags[id] print("read_tags - ID:", id) print("read_tags - Text:", tags[id]) media.stop() if media.add(tags[id]) == 0: print("read_tags - media added a new theme") media.play_delayed() GPIO.cleanup() if __name__ == "__main__": read_tags(tags)