Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyGame排队音乐问题,不等待第一首歌_Python_Pygame_Queueing - Fatal编程技术网

Python PyGame排队音乐问题,不等待第一首歌

Python PyGame排队音乐问题,不等待第一首歌,python,pygame,queueing,Python,Pygame,Queueing,我正在播放从所选音乐文件目录生成的列表中的ogg声音文件。由于某些原因,第一首歌被跳过,并从第二首歌开始播放。出于某种原因,它偶尔会播放第一首歌曲的一瞬间,这让我相信,在我如何尝试将歌曲从循环列表中排队时存在问题,但我似乎无法修复它 import pygame import sys import os from pygame.locals import * surface = pygame.display.set_mode((640, 480)) musicDir = "/Users/user

我正在播放从所选音乐文件目录生成的列表中的ogg声音文件。由于某些原因,第一首歌被跳过,并从第二首歌开始播放。出于某种原因,它偶尔会播放第一首歌曲的一瞬间,这让我相信,在我如何尝试将歌曲从循环列表中排队时存在问题,但我似乎无法修复它

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

看起来您正在加载并播放一首歌曲,然后将其排队,但在循环的下一次迭代中加载并播放第二首歌曲,然后再次将其排队

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
然后的
循环进入下一个迭代,你会做完全相同的事情,但是下一首歌

试着这样做:

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