使用pygame.mixer创建播放列表

时间:2015-02-27 11:44:50

标签: python pygame

我一直试图制作一个简单的播放列表,一个接一个地播放3首歌曲,下面的代码显示了这一点。而不是播放第一首歌music1.mp3,它播放最后一首歌music3.mp3并停止播放。我曾尝试过寻找类似的问题,但却想找到一个解决方案。

如果有人能帮忙给我一些建议或指导,我们将不胜感激。

麦克

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

2 个答案:

答案 0 :(得分:0)

您是否尝试过pygame.mixer.music.queue(' next_song.mp3')。我听说过这个问题,并且在当前的歌曲结束后播放。我是python的新手。昨天开始:)

检查以下链接,您可能会有一个想法{{3}}

答案 1 :(得分:0)

list.pop()返回列表中的最后一项。 如果你改变了:

playlist.append ( "music3.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music1.mp3" )

public partial class Frm1 : Form
{
    public Frm1()
    {
        InitializeComponent();
        AddCheckBox();
        AddCheckBox();
        AddCheckBox();

    }

    public void AddCheckBox()
    {
        CheckBox cb = new CheckBox();
        cb.Text = "AddedFromCode";
        cb.Padding = new Padding(0,0,0,0);
        cb.Margin = new Padding(0,0,0,0);
        flowLayoutPanel1.Controls.Add(cb);
    }
}

然后它会正常工作。

相关问题