没有while真正循环的Pygame播放列表 [英] Pygame Playlist without while True loop

查看:170
本文介绍了没有while真正循环的Pygame播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码中的一小段代码:

Here is a short snippet from my code:

import pygame

class Player():

  playlist= ["track1.mp3","track2.mp3",...]

  def __init__(self):
    pygame.init()       
    pygame.mixer.music.load(self.playlist[0])
    pygame.mixer.music.play()

  def playnext(self):
    self.playlist = self.playlist[1:] + [self.playlist[0]]
    pygame.mixer.music.load(self.playlist[0])
    pygame.mixer.music.play()

我的问题是,我想在第一首曲目结束后播放下一首曲目,但没有一会儿真正的循环.此类不是我代码中的唯一课程,我希望其他人在音乐播放时工作.

My problem is that I want to play the next track after the first finished, but without a while true loop. This class is not the only one in my code and I want the others to work while the music plays.

就像一个触发器;当歌曲结束时,会调用playnext()函数.

Just like a trigger; when the song ends the playnext() function is called.

提前谢谢!

推荐答案

您可以使用

You can use pygame.mixer.music.set_endevent to make an event appear in the event queue when the music ends. Just check for that event and then change to the next song.

该事件将只是一个整数(就像所有其他事件一样),因此为了不遮蔽另一个事件,请使用pygame.USEREVENT.如果您还有其他用户定义的事件,只需使用pygame.USEREVENT + 1,然后使用pygame.USEREVENT + 2,依此类推.只要确保它是唯一的即可.

The event will just be an integer (just like all other events) so in order to not shadow another event, use pygame.USEREVENT. If you have other user-defined events, just use pygame.USEREVENT + 1, and then pygame.USEREVENT + 2 and so on. Just make sure it's unique.

此外,请尽量不要在播放器类中加入音乐;将其重构为另一个类.一堂课最好只做一件事.我在下面创建了一个简短的演示. if __name__ == '__main__':行下面的所有内容都可能是其他文件中的内容.

Also, try not to put music in your player class; refactor it to another class. One class should preferably only do one thing. I created a short demonstration below. Everything underneath the line if __name__ == '__main__': could be the stuff in your other file.

import pygame
pygame.init()


class MusicPlayer:

    MUSIC_ENDED = pygame.USEREVENT + 1

    def __init__(self):
        pygame.mixer.init()

        self.songs = ["track1.mp3", "track2.mp3"]
        self.current_song_index = 0
        pygame.mixer.music.set_endevent(MusicPlayer.MUSIC_ENDED)

    def play_next(self):
        pygame.mixer.music.load(self.songs[self.current_song_index])
        pygame.mixer.music.play()
        self.current_song_index = (self.current_song_index + 1) % len(self.songs)


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()

    music_player = MusicPlayer()

    running = True
    while running:

        clock.tick(60)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    music_player.play_next()
            elif event.type == music_player.MUSIC_ENDED:
                music_player.play_next()

        pygame.display.update()

这篇关于没有while真正循环的Pygame播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆