2016-04-10 20 views
-1

Попытки сделать плейлист мейкер с помощью Pygame, но он будет играть только самую первую песню в списке, и это загрузить следующую песню, но звук не воспроизводитсяMusic Player будет играть только первую песню в плейлисте затем остановить

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) 
+0

Что возвращает 'y' вторая песня (та, которая не срабатывает)? – Scratso

ответ

0

Я думаю, вы могли бы попробовать этот код:

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

вы также можете посмотреть следующие ссылки:

Надеется, что это полезно.