Why do I get the error, "pygame.error: Couldn't read first 12 bytes of audio data" when I drop a mp3 file on to the window?

Why do I get the error, "pygame.error: Couldn't read first 12 bytes of audio data" when I drop a mp3 file on to the window?

所以,我一直在创建一个音乐播放器,而且我正在制作它。我是 pygame 的初学者。我不断收到错误消息:“pygame.error:无法读取音频数据的前 12 个字节。”当我将一个 mp3 文件放到我的 window 上时。这是我的代码:

import pygame
import sys
import os

# Created this function because it seems like rect collidepoint isn't working.

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles



pygame.init()
pygame.mixer.init()

background = pygame.display.set_mode(size=(350, 575))
back_color = (0, 175, 175)
background.fill(back_color)


while True:
    pygame.display.set_caption("Music Player " + str(pygame.mouse.get_pos()))  # Taking the pos for a good idea where to place sprites.
    ev = pygame.event.get()
    background.fill(back_color)
    for event in ev:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.DROPFILE:
            if os.path.isdir(event.file):
                queue = getListOfFiles(event.file)
            if os.path.isfile(event.file):
                queue = event.file
            if not pygame.mixer.get_busy():
                for x in queue:
                    pygame.mixer.music.load(x)
                    pygame.mixer.music.set_volume(0.7)
                    if not pygame.mixer.music.get_busy():
                        pygame.mixer.music.play()
            print(queue)
    pygame.display.flip()

编辑:有人,请解释为什么会发生这个错误。

这是完整的错误信息:

Traceback (most recent call last):
  File "/Users/daksh/Desktop/Personal/IDLE Script and Data/PycharmProjects/AdvancedPrograms/Music Player.py", line 105, in <module>
    pygame.mixer.music.load(x)
pygame.error: Couldn't read first 12 bytes of audio data

我放在 pygame window 上的文件是一个普通的 mp3 文件。

我想通了。只是忘记将变量“queue”设为一个列表,因为它正在被迭代。 解决代码:

import pygame
import sys
import os

# Created this function because it seems like rect collidepoint isn't working.

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles



pygame.init()
pygame.mixer.init()

background = pygame.display.set_mode(size=(350, 575))
back_color = (0, 175, 175)
background.fill(back_color)


while True:
    pygame.display.set_caption("Music Player " + str(pygame.mouse.get_pos()))  # Taking the pos for a good idea where to place sprites.
    ev = pygame.event.get()
    background.fill(back_color)
    for event in ev:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.DROPFILE:
            if os.path.isdir(event.file):
                queue = getListOfFiles(event.file)
            if os.path.isfile(event.file):
                queue = [event.file] # Changed HERE (put square brackets around queue)
            if not pygame.mixer.get_busy():
                for x in queue:
                    pygame.mixer.music.load(x)
                    pygame.mixer.music.set_volume(0.7)
                    if not pygame.mixer.music.get_busy():
                        pygame.mixer.music.play()
            print(queue)
    pygame.display.flip()