Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Pygame Playlist without while True loop

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.

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

Thanks in advance!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...