简体   繁体   English

如何使用 pygame.mixer.music.play() 重复播放歌曲?

[英]How can I play song repeatedly using pygame.mixer.music.play()?

I have tried to repeat a song(mp3) using pygame module.我尝试使用 pygame 模块重复一首歌曲(mp3)。 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.代码如下来自站点https://www.studytonight.com/tkinter/music-player-application-using-tkinter当调用这个function时,它只出来一次,当然是自然的。

    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?我应该多次调用 function playsound(self) 我想重复吗?

   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()

The first optional argument ( loops ) to pygame.mixer.music.play() tells the function how often to repeate the music. pygame.mixer.music.play()的第一个可选参数( loops )告诉 function 多久重复一次音乐。

For instance pass 1 to play the music twice (repeat once):例如通过 1 播放音乐两次(重复一次):

pygame.mixer.music.play(1)

Pass -1 to play the music infinitely:传递 -1 无限播放音乐:

pygame.mixer.music.play(-1)

See pygame.mixer.music.play() :请参阅pygame.mixer.music.play()

play(loops=0, start=0.0, fade_ms = 0) -> None

This will play the loaded music stream.这将播放加载的音乐 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. loops是一个可选的 integer 参数,默认为 0,它告诉重复音乐的次数。 The music repeats indefinately if this argument is set to -1.如果此参数设置为 -1,则音乐将无限重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM