我需要在Pygame中创建循环来帮助
我有以下代码
def play_music():
song = playlist.get(ACTIVE)
next_one = playlist.curselection()
next_one = next_one[0]+1
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
pygame.mixer.music.set_endevent()
for event in pygame.event.get():
playlist.selection_clear(0,END)
playlist.activate(next_one)
playlist.select_set(next_one,last=None)
song = playlist.get(ACTIVE)
pygame.mixer.music.queue(song)
当我运行代码时,它会播放一首歌曲,然后播放播放列表中的下一首歌曲。但我想将其实现为一个循环。它应该将下一首歌曲与播放列表中的歌曲数量一样多(即:我的播放列表中有 5 首歌曲,然后我想要,我只需按一次播放按钮,然后播放所有 5 首歌曲,一张一张。)
我的程序的图片: https://i.sstatic.net/T5Gch.png 我希望你能帮助我。提前感谢您的帮助。
I have the following Code
def play_music():
song = playlist.get(ACTIVE)
next_one = playlist.curselection()
next_one = next_one[0]+1
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)
pygame.mixer.music.set_endevent()
for event in pygame.event.get():
playlist.selection_clear(0,END)
playlist.activate(next_one)
playlist.select_set(next_one,last=None)
song = playlist.get(ACTIVE)
pygame.mixer.music.queue(song)
When I run the Code, then it plays a song and after that it plays the next song in the playlist. But I want to implement this into a loop. It should queue the next song, as much songs it has in the playlist (i.e: I have 5 songs in the playlist, then I want, that I only have to press the Play-Button once and after that it plays all 5 songs, one by one.)
A Picture of my Program:
https://i.sstatic.net/T5Gch.png
I hope you can help me. Thank you for your help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我回答的示例使用pygame.mixer.music.music.get_endevent()
它创建自己的事件
music_end
并将其个抓住末端。后来,它加载了一首歌,并将下一首歌添加到队列中。
当第一首歌曲结束后,它将生成/发送事件
music_end
。但是,这需要在pygame.event.get()中运行所有时间以捕获此事件并运行代码,该代码将在队列中添加下一个歌曲。
当它将完成下一首歌时,它将生成/再次发送事件
music_end
哪个循环会捕获并将歌曲再次添加到队列中。有了更多的歌曲,它需要将所有歌曲保留在列表中,并在列表中添加哪些歌曲。
编辑:
版本,可与歌曲列表和更改一起使用。
Here is example from my answer for Utilising the pygame.mixer.music.get_endevent()
It creates own event
MUSIC_END
and assing it to music endevent.Later it loads one song and adds next song to queue.
When first song will finished then it will generate/send event
MUSIC_END
. But this need to run all timefor event in pygame.event.get()
to catch this event and run code which will add next song to queue.When it will finish next song then it will generate/send again event
MUSIC_END
which loop will catch and add again song to queue.With more songs it need to keep all songs on list and remeber which song from list it has to add to queue.
EDIT:
Version which works with list of songs and changes also text on label.