I have tried to repeat a song(mp3) using pygame module. The code is as follows from the site https://www.studytonight.com/tkinter/music-player-application-using-tkinter When calling this function, it comes out only one time, of course it’s natural.
def playsong(self): # Displaying Selected Song title self.track.set(self.playlist.get(ACTIVE)) # Displaying Status self.status.set("-Playing") # Loading Selected Song pygame.mixer.music.load(self.playlist.get(ACTIVE)) # Playing Selected Song pygame.mixer.music.play()
To repeat this sound file I added loop like this, but it plays just one time. How can I repeat many times over and over this? Should I call the function playsound(self) many times I want to repeat?
def playsong(self): for i in range(3): # Displaying Selected Song title self.track.set(self.playlist.get(ACTIVE)) # Displaying Status self.status.set("-Playing") # Loading Selected Song pygame.mixer.music.load(self.playlist.get(ACTIVE)) # Playing Selected Song pygame.mixer.music.play()
Advertisement
Answer
The first optional argument (loops) to pygame.mixer.music.play()
tells the function how often to repeate the music.
For instance pass 1 to play the music twice (repeat once):
pygame.mixer.music.play(1)
Pass -1 to play the music infinitely:
pygame.mixer.music.play(-1)
See pygame.mixer.music.play()
:
play(loops=0, start=0.0, fade_ms = 0) -> None
This will play the loaded music stream. If the music is already playing it will be restarted.
loops is an optional integer argument, which is 0 by default, it tells how many times to repeat the music. The music repeats indefinately if this argument is set to -1.