Python播放mp3音频文件


两个库 playsound和pydub

# 分别安装两个库
pip install playsound==1.2.2
pip install pydub
# playsound demo
from playsound import playsound

playsound("test.mp3")
# pydub demo
from pydub import AudioSegment
from pydub.playback import play

play(AudioSegment.from_mp3("test.mp3"))

各自问题

playsound 不能用最新版(1.3.0)容易出现问题

pydub 需要修改pydub.playback中的 _play_with_ffplay 函数

with NamedTemporaryFile("w+b", suffix=".wav") as f:
    f.close()
    ......

pydub 需要安装ffmpeg,功能更全面。单纯播放声音还是playsound更适合


July 30, 2023, 1:37 p.m. Collection documents