Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

python - How can I play song repeatedly using pygame.mixer.music.play()?

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()
question from:https://stackoverflow.com/questions/65858091/how-can-i-play-song-repeatedly-using-pygame-mixer-music-play

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...