You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.10与Pygame兼容性问题:libmpg123-0.dll加载失败

问题:Pygame mixer加载MP3时提示libmpg123-0.dll缺失

错误信息

File "d:\Study\Python\musicplayer.py", line 32, in PlayMusic
    mixer.music.load(Playlist.get(ACTIVE))
pygame.error: Failed loading libmpg123-0.dll: The specified module could not be found.

问题详情

使用Pygame的mixer模块播放MP3音乐时触发上述错误,怀疑是Python 3.10与Pygame的版本兼容性问题,已尝试更换多个Pygame版本,但错误仍未解决。相关代码如下:

代码示例

import os
from tkinter import *
from tkinter import filedialog
from pygame import mixer
import pygame
# from PIL import Image, ImageTk
# to create a GUI window
root = Tk()
root.title('Melody Soul')
root.geometry('920x600+290+85')
root.configure(background='#212121')
root.resizable(False, False)
pygame.init()
mixer.init()
# root.mainloop()

# To create a function to opne a file

def AddMusic():
    path = filedialog.askdirectory()
    if path:
        os.chdir(path)
        songs = os.listdir(path)

        for song in songs:
            if song.endswith(".mp3"):
                Playlist.insert(END, song)
                
def PlayMusic():
    Music_Name = Playlist.get(ACTIVE)
    print(Music_Name[0:-4])
    mixer.music.load(Playlist.get(ACTIVE))
    mixer.music.play()

#icon
image_icon = PhotoImage(file='C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\Pins\\music.png')
root.iconphoto(False,image_icon)

Top = PhotoImage(file='C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\Pins\\312F6F46-F8F6-428F-BD78-A6E8644AAA42.png')
Label(root,image=Top,bg='#0f1a2b').pack()

#logo
logo = PhotoImage(file='C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\Pins\\photo-1505238680356-667803448bb6.png')
Label(root,image=logo,bg='#0f1a2b',bd=0).place(x=70,y=115)

# Buttons 
ButtonPlay = PhotoImage(file="C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\play-button.png")
Button(root, image=ButtonPlay, bg="#0f1a2b", bd=0,
       command=PlayMusic).place(x=40, y=40)

# ButtonResume = PhotoImage(file="C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\end.png")
# Button(root, image=ButtonResume, bg="#0f1a2b", bd=0,
#        command=mixer.music.unpause).place(x=40, y=40)

# ButtonPause = PhotoImage(file="C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\pause button.png")
# Button(root, image=ButtonPause, bg="#0f1a2b", bd=0,
# command=mixer.music.pause).place(x=40, y=40)

# Label 
Menu = PhotoImage(file="C:\\Users\\meetg\\OneDrive\\Pictures\\Saved Pictures\\Pins\\IMG_0778.png")
Label(root, image=Menu, bg="#0f1a2b").pack(padx=10, pady=50, side=RIGHT)

Frame_Music = Frame(root, bd=2, relief=RIDGE)
Frame_Music.place(x=330, y=350, width=560, height=200)

Button(root, text="Open Folder", width=15, height=2, font=("times new roman",
       12, "bold"), fg="Black", bg="#21b3de", command=AddMusic).place(x=330, y=300)

Scroll = Scrollbar(Frame_Music)
Playlist = Listbox(Frame_Music, width=100, font=("Times new roman", 10), bg="#333333", fg="grey", selectbackground="lightblue", cursor="hand2", bd=0, yscrollcommand=Scroll.set)
Scroll.config(command=Playlist.yview)
Scroll.pack(side=RIGHT, fill=Y)
Playlist.pack(side=LEFT, fill=BOTH)
root.mainloop()

解决方案

  • 手动补全缺失的dll文件

    1. 下载和系统位数匹配的libmpg123-0.dll文件
    2. 把文件放到Python安装目录的Scripts文件夹,或者系统的System32(64位系统)/SysWOW64(32位系统)文件夹里
  • 换用pygame社区版(pygame-ce)
    卸载原版pygame,运行pip install pygame-ce安装社区版,这个版本修复了不少兼容性问题,大概率能解决dll缺失的问题

  • 修正音乐文件路径
    当前代码加载音乐时用的是文件名,可能存在路径识别问题,修改PlayMusic函数,拼接完整文件路径:

    def PlayMusic():
        Music_Name = Playlist.get(ACTIVE)
        full_path = os.path.join(os.getcwd(), Music_Name)
        print(Music_Name[0:-4])
        mixer.music.load(full_path)
        mixer.music.play()
    
  • 重装适配版本的pygame
    彻底卸载现有pygame,然后安装适配Python3.10的版本,执行命令:

    pip uninstall pygame -y
    pip install pygame==2.1.2
    

内容的提问来源于stack exchange,提问作者Meet Gajjar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:25:21