2013-06-05 28 views
0

我正在播放從我選擇的音樂文件目錄生成的列表中的ogg聲音文件。出於某種原因,第一首歌曲會被跳過,並從第二首歌曲開始播放。由於某種原因,它偶爾會播放第一首歌曲的一秒鐘,這導致我相信在試圖從循環列表中排列歌曲時存在一個問題,但似乎無法修復它。PyGame排隊音樂問題,不等待第一首歌

import pygame 
import sys 
import os 
from pygame.locals import * 
surface = pygame.display.set_mode((640, 480)) 
musicDir = "/Users/user/Desktop/Dat Sound/Music/" 
x = os.listdir(musicDir) 
del(x[0]) # Deleting first element because it's DS Store file (Mac) 
print x # The list is ['Bonfire.ogg', 'Voodoo Child.ogg'] 
n = '' 
count = 1 
pygame.mixer.init() 
for i in range(len(x)): 
    n = musicDir + str(x[i]) 
    print n 
    pygame.mixer.music.load(n) 
    pygame.mixer.music.play() 
    pygame.mixer.music.queue(musicDir + str(x[i])) 
    # I'm queueing the next song in the list of songs from the folder + its location 
    print pygame.mixer.music.get_busy() 
    # get_busy returns true for both files but only the second is playing 
running = True 
while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 

回答

1

它看起來像你加載和播放一首歌曲,然後排隊,但隨後加載和播放第二首歌曲在循環的下一次迭代,然後再排隊吧...

n = musicDir + str(x[i]) 
pygame.mixer.music.load(n) # so you load the song... 
pygame.mixer.music.play() # then you play it.... 
pygame.mixer.music.queue(musicDir + str(x[i])) # i hasn't changed, this is the same song 
               # you just loaded and started playing 

然後for循環進入下一個迭代,你做完全相同的東西,但與下一首歌曲。

嘗試這樣:

n = musicDir + str[0] # let's load and play the first song 
pygame.mixer.music.load(n) 
pygame.mixer.music.play() 
for song in x: 
    pygame.mixer.music.queue(musicDir + str(song)) # loop over and queue the rest