问题
I have been trying to make a simple playlist that plays 3 songs one after the other the code below shows an attempt of that. Rather than playing the first song music1.mp3 it plays the last song music3.mp3 and stops playing. I have tried looking at similar questions but strugling to find a soloution.
Please if anyone can help give me some advice or some guidance it will be much appreciated.
Mike
import pygame
import time
pygame.mixer.init()
pygame.display.init()
screen = pygame.display.set_mode ( ( 420 , 240 ) )
playlist = list()
playlist.append ( "music1.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music3.mp3" )
pygame.mixer.music.load ( playlist.pop() ) # Get the first track from the playlist
pygame.mixer.music.queue ( playlist.pop() ) # Queue the 2nd song
pygame.mixer.music.set_endevent ( pygame.USEREVENT ) # Setup the end track event
pygame.mixer.music.play() # Play the music
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.USEREVENT: # A track has ended
if len ( playlist ) > 0: # If there are more tracks in the queue...
pygame.mixer.music.queue ( playlist.pop() ) # Queue the next one in the list
回答1:
Have you tried pygame.mixer.music.queue('next_song.mp3'). I have heard that this ques the song and is played after the current one is finished. I am a newbie to python. Started yesterday :)
Check the following link, you might get an idea http://www.nerdparadise.com/tech/python/pygame/basics/part3/
回答2:
list.pop() returns the last item in the list. If you changed:
playlist.append ( "music1.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music3.mp3" )
to
playlist.append ( "music3.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music1.mp3" )
then it would work correctly.
来源:https://stackoverflow.com/questions/28764355/create-a-playlist-using-pygame-mixer