Python MP3播放器libmpg123-0.dll加载失败修复及树莓派配件推荐
Python MP3播放器libmpg123-0.dll缺失错误修复
问题描述
运行MP3播放器代码时出现以下错误:
File "d:\RASPMP3\main.py", line 38, in play_music pygame.mixer.music.load(os.path.join(root.drectory, current_song)) pygame.error: Failed loading libmpg123-0.dll: The specified module could not be found
已尝试重装pygame、Python,Github Copilot建议无效,使用VSCode环境。
附完整代码:
from tkinter import * from tkinter import filedialog import pygame import os root = Tk() root.title("RASPMP3") root.geometry("600x400") pygame.mixer.init() menubar = Menu(root) root.config(menu=menubar) songs = [] current_song = "" paused = False def load_music(): global current_song root.directory = filedialog.askdirectory() for song in os.listdir(root.directory): name, ext = os.path.splitext(song) if ext == ".mp3": songs.append(song) for song in songs: songList.insert(END, song) songList.selection_set(0) current_song = songs[songList.curselection()[0]] def play_music(): global current_song, paused if not paused: pygame.mixer.music.load(os.path.join(root.directory, current_song)) pygame.mixer.music.play() else: pygame.mixer.music.unpause() paused = False def pause_music(): global paused pygame.mixer.music.pause() paused = True def next_music(): global current_song, paused try: songList.selection_clear(0, END) next_index = songs.index(current_song) + 1 if next_index < len(songs): songList.selection_set(next_index) current_song = songs[songList.curselection()[0]] play_music() else: # Handle the case when the current song is the last in the list songList.selection_set(0) current_song = songs[0] play_music() except IndexError: pass def previous_music(): global current_song, paused try: songList.selection_clear(0, END) prev_index = songs.index(current_song) - 1 if prev_index >= 0: songList.selection_set(prev_index) current_song = songs[songList.curselection()[0]] play_music() else: # Handle the case when the current song is the first in the list songList.selection_set(len(songs) - 1) current_song = songs[-1] play_music() except IndexError: pass organize_menu = Menu(menubar, tearoff=False) organize_menu.add_command(label="Open Folder", command=load_music) menubar.add_cascade(label="Songs", menu=organize_menu) songList = Listbox(root, bg="black", fg="white", width=100, height=15) songList.pack() control_frame = Frame(root) control_frame.pack() play_btn_img = PhotoImage(file="play.png") pause_btn_img = PhotoImage(file="pause.png") next_btn_img = PhotoImage(file="next.png") previous_btn_img = PhotoImage(file="previous.png") play_btn = Button(control_frame, image=play_btn_img, borderwidth=0, command=play_music) pause_btn = Button(control_frame, image=pause_btn_img, borderwidth=0, command=pause_music) next_btn = Button(control_frame, image=next_btn_img, borderwidth=0, command=next_music) previous_btn = Button(control_frame, image=previous_btn_img, borderwidth=0, command=previous_music) play_btn.grid(row=0, column=1, padx=7, pady=10) pause_btn.grid(row=0, column=2, padx=7, pady=10) next_btn.grid(row=0, column=3, padx=7, pady=10) previous_btn.grid(row=0, column=0, padx=7, pady=10) root.mainloop()
修复方案
- 手动补充依赖库:
- 下载对应系统位数(32/64位)的
libmpg123-0.dll文件 - 将文件放入Python安装目录的
Scripts文件夹,或系统System32(64位)/SysWOW64(32位)文件夹
- 下载对应系统位数(32/64位)的
- 重新安装带完整依赖的pygame:
卸载现有pygame后,执行pip install pygame --pre安装预发布版本,或从pygame官方渠道下载包含所有依赖的离线安装包 - 替换音频播放库:
若上述方法无效,可改用playsound或pydub替代pygame.mixer,其中pydub需提前安装ffmpeg,代码修改量较小
树莓派3 MP3播放器配件推荐(新西兰可购)
屏幕
- 3.5英寸LCD触摸屏:PB Tech、Spark等本地电子商城均有售卖,适配树莓派3,支持触控,分辨率多为480x320,直接插GPIO接口即可使用
- 2.8英寸TFT显示屏:价格更低、功耗小,适合无需触控的场景,可在PB Tech、Jaycar Electronics购买
控制配件
- 四按键模块:Jaycar Electronics有售现成4位按键模块,带GPIO接口,直接接线就能实现播放/暂停、上一曲/下一曲功能
- 旋转编码器(类iPod滚轮):PB Tech和Spark均有售卖,支持旋转+按压操作,可模拟滚轮选曲+确认功能,接线简单
- 复古实体滚轮:部分本地创客商店(如惠灵顿Maker Space周边店铺)有类似iPod的实体滚轮配件,适合追求外观还原的项目
内容的提问来源于stack exchange,提问作者Charlie
相关产品推荐
相关产品推荐

