【问题标题】:Music Player will only play first song in playlist then stop音乐播放器只会播放播放列表中的第一首歌曲然后停止
【发布时间】:2016-04-10 20:05:35
【问题描述】:

尝试使用 pygame 制作播放列表,但它只会播放列表中的第一首歌曲,它会加载下一首歌曲但不播放音频

from pygame import mixer # Load the required library
from os import listdir
from mutagen.mp3 import MP3
import time
k = listdir('C:/LOCAL')
print(k)
mixer.init()
for x in k:
   y = "C:/LOCAL/" + x
   print y
   mixer.music.load(y)
   mixer.music.play()
   tracklength = MP3(y).info.length
   print tracklength
   time.sleep(tracklength)

【问题讨论】:

  • y 在第二首歌曲(失败的那首)中返回什么?

标签: python time operating-system mp3 mutagen


【解决方案1】:

我想你可以试试这个代码:

import time
import pygame
from os import listdir

pygame.mixer.init()
tracks = listdir('C:/LOCAL')

# Create the playlist.
playlist = list()
for track in tracks:
    playlist.append(track)

# Define the song end event.
SONG_END = pygame.endevent.USEREVENT + 1

pygame.mixer.music.load(playlist.pop())   # Load the first track to play.
pygame.mixer.music.queue(playlist.pop())  # Add the second track in queue.
pygame.mixer.music.set_endevent(SONG_END) # Set the event.
pygame.mixer.music.play()                 # Play.

while True:
    for event in pygame.event.get():
        if event.type == SONG_END: # A track has ended.
            if len(playlist) > 0:  # Queue the next track if there is one.
                pygame.mixer.music.queue(playlist.pop())

您还可以观看以下链接:

希望这有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多